Java Code Examples for java.sql.SQLTransientException

The following examples show how to use java.sql.SQLTransientException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * 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();
    }
}
 
Example 2
Source Project: morf   Source File: Oracle.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @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;
}
 
Example 3
Source Project: jqm   Source File: Helpers.java    License: Apache License 2.0 6 votes vote down vote up
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"));
}
 
Example 4
Source Project: sis   Source File: AuthorityFactories.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 5
/**
 * 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();
    }
}
 
Example 6
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);
   }
}
 
Example 7
/**
 * 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();
    }
}
 
Example 8
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);
    }
}
 
Example 9
/**
 * 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();
    }
}
 
Example 10
/**
 * 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();
    }
}
 
Example 11
/**
 * 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();
    }
}
 
Example 12
/**
 * 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);
}
 
Example 13
@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;
}
 
Example 14
/**
 * 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);
}
 
Example 15
/**
 * 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);
}
 
Example 16
/**
 * 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);
}
 
Example 17
/**
 * 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);
}
 
Example 18
/**
 * 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);
}
 
Example 19
/**
 * 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);
}
 
Example 20
/**
 * 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);
}
 
Example 21
/**
 * 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);
}
 
Example 22
/**
 * 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()));
    }
}
 
Example 23
/**
 * 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()));
    }
}
 
Example 24
/**
 * 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);
}
 
Example 25
/**
 * 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);
}
 
Example 26
/**
 * 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()));
    }
}
 
Example 27
/**
 * 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);
}
 
Example 28
/**
 * 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);
}
 
Example 29
/**
 * 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);
}
 
Example 30
/**
 * 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);
}