下面列出了怎么用java.sql.SQLTransientException的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Validate that the ordering of the returned Exceptions is correct
* using traditional while loop
*/
@Test
public void test12() {
SQLTransientException ex = new SQLTransientException("Exception 1", t1);
SQLTransientException ex1 = new SQLTransientException("Exception 2");
SQLTransientException ex2 = new SQLTransientException("Exception 3", t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
SQLException sqe = ex;
while (sqe != null) {
assertTrue(msgs[num++].equals(sqe.getMessage()));
Throwable c = sqe.getCause();
while (c != null) {
assertTrue(msgs[num++].equals(c.getMessage()));
c = c.getCause();
}
sqe = sqe.getNextException();
}
}
/**
* @see org.alfasoftware.morf.jdbc.DatabaseType#reclassifyException(java.lang.Exception)
*/
@Override
public Exception reclassifyException(Exception e) {
// Reclassify OracleXA exceptions
Optional<Integer> xaErrorCode = getErrorCodeFromOracleXAException(e);
if (xaErrorCode.isPresent()) {
// ORA-00060: Deadlock detected while waiting for resource
// ORA-02049: Distributed transaction waiting for lock
if (xaErrorCode.get() == 60 || xaErrorCode.get() == 2049) {
return new SQLTransientException(e.getMessage(), null, xaErrorCode.get(), e);
}
return new SQLException(e.getMessage(), null, xaErrorCode.get(), e);
}
// Reclassify any SQLExceptions which should be SQLTransientExceptions but are not. Specifically this handles BatchUpdateExceptions
if(e instanceof SQLException && !(e instanceof SQLTransientException)) {
int errorCode = ((SQLException) e).getErrorCode();
if(errorCode == 60 || errorCode == 2049) {
return new SQLTransientException(e.getMessage(), ((SQLException) e).getSQLState(), errorCode, e);
}
}
return e;
}
static boolean testDbFailure(Exception e)
{
return (e instanceof SQLTransientException) || (e.getCause() instanceof SQLTransientException)
|| (e.getCause() != null && e.getCause().getCause() instanceof SQLTransientException)
|| (e.getCause() != null && e.getCause().getCause() != null
&& e.getCause().getCause().getCause() instanceof SQLTransientException)
|| (e.getCause() != null && e.getCause().getCause() != null && e.getCause().getCause().getCause() != null
&& e.getCause().getCause().getCause().getCause() instanceof SQLTransientException)
|| (e.getCause() != null && e.getCause() instanceof SQLException
&& e.getMessage().equals("Failed to validate a newly established connection."))
|| (e.getCause() != null && e.getCause().getCause() != null && e.getCause().getCause() instanceof SocketException)
|| (e.getCause() != null && e.getCause().getMessage().equals("This connection has been closed"))
|| (e.getCause() != null && e.getCause() instanceof SQLNonTransientConnectionException)
|| (e.getCause() != null && e.getCause() instanceof SQLNonTransientException
&& e.getCause().getMessage().equals("connection exception: closed"));
}
/**
* Returns the fallback to use if the authority factory is not available. Unless the problem may be temporary,
* this method replaces the {@link EPSGFactory} instance by {@link EPSGFactoryFallback} in order to prevent
* the same exception to be thrown and logged on every calls to {@link CRS#forCode(String)}.
*/
static GeodeticAuthorityFactory fallback(final UnavailableFactoryException e) throws UnavailableFactoryException {
final boolean isTransient = (e.getCause() instanceof SQLTransientException);
final AuthorityFactory unavailable = e.getUnavailableFactory();
GeodeticAuthorityFactory factory;
final boolean alreadyDone;
synchronized (EPSG) {
factory = EPSG[0];
alreadyDone = (factory == EPSGFactoryFallback.INSTANCE);
if (!alreadyDone) { // May have been set in another thread (race condition).
if (unavailable != factory) {
throw e; // Exception did not come from a factory that we control.
}
factory = EPSGFactoryFallback.INSTANCE;
if (!isTransient) {
ALL.reload();
EPSG[0] = factory;
}
}
}
if (!alreadyDone) {
log(e, true);
}
return factory;
}
/**
* Validate that the ordering of the returned Exceptions is correct
* using traditional while loop
*/
@Test
public void test12() {
SQLTransientException ex = new SQLTransientException("Exception 1", t1);
SQLTransientException ex1 = new SQLTransientException("Exception 2");
SQLTransientException ex2 = new SQLTransientException("Exception 3", t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
SQLException sqe = ex;
while (sqe != null) {
assertTrue(msgs[num++].equals(sqe.getMessage()));
Throwable c = sqe.getCause();
while (c != null) {
assertTrue(msgs[num++].equals(c.getMessage()));
c = c.getCause();
}
sqe = sqe.getNextException();
}
}
CassandraPreparedStatement(CassandraConnection con,
String cql,
int rsType,
int rsConcurrency,
int rsHoldability
) throws SQLException
{
super(con,cql,rsType,rsConcurrency,rsHoldability);
if (LOG.isTraceEnabled()) LOG.trace("CQL: " + this.cql);
try
{
stmt = this.connection.getSession().prepare(cql);
this.statement = new BoundStatement(stmt);
batchStatements = Lists.newArrayList();
count = cql.length() - cql.replace("?", "").length();
}
catch (Exception e)
{
throw new SQLTransientException(e);
}
}
/**
* Validate that the ordering of the returned Exceptions is correct
* using traditional while loop
*/
@Test
public void test12() {
SQLTransientException ex = new SQLTransientException("Exception 1", t1);
SQLTransientException ex1 = new SQLTransientException("Exception 2");
SQLTransientException ex2 = new SQLTransientException("Exception 3", t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
SQLException sqe = ex;
while (sqe != null) {
assertTrue(msgs[num++].equals(sqe.getMessage()));
Throwable c = sqe.getCause();
while (c != null) {
assertTrue(msgs[num++].equals(c.getMessage()));
c = c.getCause();
}
sqe = sqe.getNextException();
}
}
private void doExecute() throws SQLException
{
if (LOG.isTraceEnabled()) LOG.trace("CQL: " + cql);
try
{
resetResults();
if (this.connection.debugMode) System.out.println("CQL: "+ cql);
if(this.statement.getFetchSize()==0)
// force paging to avoid timeout and node harm...
this.statement.setFetchSize(100);
this.statement.setConsistencyLevel(this.connection.defaultConsistencyLevel);
for(int i=0; i<this.statement.preparedStatement().getVariables().size(); i++){
// Set parameters to null if unset
if(!this.statement.isSet(i)){
this.statement.setToNull(i);
}
}
currentResultSet = new CassandraResultSet(this, this.connection.getSession().execute(this.statement));
}
catch (Exception e)
{
throw new SQLTransientException(e);
}
}
/**
* Validate that the ordering of the returned Exceptions is correct
* using traditional while loop
*/
@Test
public void test12() {
SQLTransientException ex = new SQLTransientException("Exception 1", t1);
SQLTransientException ex1 = new SQLTransientException("Exception 2");
SQLTransientException ex2 = new SQLTransientException("Exception 3", t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
SQLException sqe = ex;
while (sqe != null) {
assertTrue(msgs[num++].equals(sqe.getMessage()));
Throwable c = sqe.getCause();
while (c != null) {
assertTrue(msgs[num++].equals(c.getMessage()));
c = c.getCause();
}
sqe = sqe.getNextException();
}
}
/**
* Validate that the ordering of the returned Exceptions is correct
* using traditional while loop
*/
@Test
public void test12() {
SQLTransientException ex = new SQLTransientException("Exception 1", t1);
SQLTransientException ex1 = new SQLTransientException("Exception 2");
SQLTransientException ex2 = new SQLTransientException("Exception 3", t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
SQLException sqe = ex;
while (sqe != null) {
assertTrue(msgs[num++].equals(sqe.getMessage()));
Throwable c = sqe.getCause();
while (c != null) {
assertTrue(msgs[num++].equals(c.getMessage()));
c = c.getCause();
}
sqe = sqe.getNextException();
}
}
/**
* Validate that the ordering of the returned Exceptions is correct
* using traditional while loop
*/
@Test
public void test12() {
SQLTransientException ex = new SQLTransientException("Exception 1", t1);
SQLTransientException ex1 = new SQLTransientException("Exception 2");
SQLTransientException ex2 = new SQLTransientException("Exception 3", t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
SQLException sqe = ex;
while (sqe != null) {
assertTrue(msgs[num++].equals(sqe.getMessage()));
Throwable c = sqe.getCause();
while (c != null) {
assertTrue(msgs[num++].equals(c.getMessage()));
c = c.getCause();
}
sqe = sqe.getNextException();
}
}
/**
* Create SQLTransientException with message, SQLState, and error code
*/
@Test
public void test4() {
SQLTransientException ex = new SQLTransientException(reason, state, errorCode);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState().equals(state)
&& ex.getCause() == null
&& ex.getErrorCode() == errorCode);
}
@Override
protected DataAccessException doTranslate(String task, String sql, SQLException ex) {
if (ex instanceof SQLTransientException) {
if (ex instanceof SQLTransientConnectionException) {
return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex);
}
else if (ex instanceof SQLTransactionRollbackException) {
return new ConcurrencyFailureException(buildMessage(task, sql, ex), ex);
}
else if (ex instanceof SQLTimeoutException) {
return new QueryTimeoutException(buildMessage(task, sql, ex), ex);
}
}
else if (ex instanceof SQLNonTransientException) {
if (ex instanceof SQLNonTransientConnectionException) {
return new DataAccessResourceFailureException(buildMessage(task, sql, ex), ex);
}
else if (ex instanceof SQLDataException) {
return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
}
else if (ex instanceof SQLIntegrityConstraintViolationException) {
return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
}
else if (ex instanceof SQLInvalidAuthorizationSpecException) {
return new PermissionDeniedDataAccessException(buildMessage(task, sql, ex), ex);
}
else if (ex instanceof SQLSyntaxErrorException) {
return new BadSqlGrammarException(task, sql, ex);
}
else if (ex instanceof SQLFeatureNotSupportedException) {
return new InvalidDataAccessApiUsageException(buildMessage(task, sql, ex), ex);
}
}
else if (ex instanceof SQLRecoverableException) {
return new RecoverableDataAccessException(buildMessage(task, sql, ex), ex);
}
// Fallback to Spring's own SQL state translation...
return null;
}
/**
* Create SQLTransientException with message, SQLState, errorCode, and Throwable
*/
@Test
public void test5() {
SQLTransientException ex =
new SQLTransientException(reason, state, errorCode, t);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState().equals(state)
&& cause.equals(ex.getCause().toString())
&& ex.getErrorCode() == errorCode);
}
/**
* Create SQLTransientException with message
*/
@Test
public void test2() {
SQLTransientException ex = new SQLTransientException(reason);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState() == null
&& ex.getCause() == null
&& ex.getErrorCode() == 0);
}
/**
* Create SQLTransientException with message, SQLState, and error code
*/
@Test
public void test4() {
SQLTransientException ex = new SQLTransientException(reason, state, errorCode);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState().equals(state)
&& ex.getCause() == null
&& ex.getErrorCode() == errorCode);
}
/**
* Create SQLTransientException with message, SQLState, errorCode, and Throwable
*/
@Test
public void test5() {
SQLTransientException ex =
new SQLTransientException(reason, state, errorCode, t);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState().equals(state)
&& cause.equals(ex.getCause().toString())
&& ex.getErrorCode() == errorCode);
}
/**
* Create SQLTransientException with no-arg constructor
*/
@Test
public void test1() {
SQLTransientException ex = new SQLTransientException();
assertTrue(ex.getMessage() == null
&& ex.getSQLState() == null
&& ex.getCause() == null
&& ex.getErrorCode() == 0);
}
/**
* Create SQLTransientException with message, and Throwable
*/
@Test
public void test7() {
SQLTransientException ex = new SQLTransientException(reason, t);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState() == null
&& cause.equals(ex.getCause().toString())
&& ex.getErrorCode() == 0);
}
/**
* Serialize a SQLTransientException and make sure you can read it back properly
*/
@Test
public void test10() throws Exception {
SQLTransientException e =
new SQLTransientException(reason, state, errorCode, t);
SQLTransientException ex1 = createSerializedException(e);
assertTrue(reason.equals(ex1.getMessage())
&& ex1.getSQLState().equals(state)
&& cause.equals(ex1.getCause().toString())
&& ex1.getErrorCode() == errorCode);
}
/**
* Create SQLTransientException with Throwable
*/
@Test
public void test9() {
SQLTransientException ex = new SQLTransientException(t);
assertTrue(ex.getMessage().equals(cause)
&& ex.getSQLState() == null
&& cause.equals(ex.getCause().toString())
&& ex.getErrorCode() == 0);
}
/**
* Validate that the ordering of the returned Exceptions is correct
* using for-each loop
*/
@Test
public void test11() {
SQLTransientException ex = new SQLTransientException("Exception 1", t1);
SQLTransientException ex1 = new SQLTransientException("Exception 2");
SQLTransientException ex2 = new SQLTransientException("Exception 3", t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
for (Throwable e : ex) {
assertTrue(msgs[num++].equals(e.getMessage()));
}
}
/**
* Validate that the ordering of the returned Exceptions is correct
* using for-each loop
*/
@Test
public void test11() {
SQLTransientException ex = new SQLTransientException("Exception 1", t1);
SQLTransientException ex1 = new SQLTransientException("Exception 2");
SQLTransientException ex2 = new SQLTransientException("Exception 3", t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
for (Throwable e : ex) {
assertTrue(msgs[num++].equals(e.getMessage()));
}
}
/**
* Create SQLTransientException with message, and Throwable
*/
@Test
public void test7() {
SQLTransientException ex = new SQLTransientException(reason, t);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState() == null
&& cause.equals(ex.getCause().toString())
&& ex.getErrorCode() == 0);
}
/**
* Create SQLTransientException with message, SQLState, and Throwable
*/
@Test
public void test6() {
SQLTransientException ex = new SQLTransientException(reason, state, t);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState().equals(state)
&& cause.equals(ex.getCause().toString())
&& ex.getErrorCode() == 0);
}
/**
* Validate that the ordering of the returned Exceptions is correct
* using for-each loop
*/
@Test
public void test11() {
SQLTransientException ex = new SQLTransientException("Exception 1", t1);
SQLTransientException ex1 = new SQLTransientException("Exception 2");
SQLTransientException ex2 = new SQLTransientException("Exception 3", t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
for (Throwable e : ex) {
assertTrue(msgs[num++].equals(e.getMessage()));
}
}
/**
* Create SQLTransientException with Throwable
*/
@Test
public void test9() {
SQLTransientException ex = new SQLTransientException(t);
assertTrue(ex.getMessage().equals(cause)
&& ex.getSQLState() == null
&& cause.equals(ex.getCause().toString())
&& ex.getErrorCode() == 0);
}
/**
* Create SQLTransientException with message
*/
@Test
public void test2() {
SQLTransientException ex = new SQLTransientException(reason);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState() == null
&& ex.getCause() == null
&& ex.getErrorCode() == 0);
}
/**
* Create SQLTransientException with message, and SQLState
*/
@Test
public void test3() {
SQLTransientException ex = new SQLTransientException(reason, state);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState().equals(state)
&& ex.getCause() == null
&& ex.getErrorCode() == 0);
}
/**
* Create SQLTransientException with message, SQLState, and error code
*/
@Test
public void test4() {
SQLTransientException ex = new SQLTransientException(reason, state, errorCode);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState().equals(state)
&& ex.getCause() == null
&& ex.getErrorCode() == errorCode);
}