Search in sources :

Example 1 with RecoverableDataAccessException

use of org.springframework.dao.RecoverableDataAccessException in project spring-framework by spring-projects.

the class SQLExceptionSubclassTranslatorTests method errorCodeTranslation.

@Test
public void errorCodeTranslation() {
    SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
    SQLException dataIntegrityViolationEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 0);
    DataIntegrityViolationException divex = (DataIntegrityViolationException) sext.translate("task", "SQL", dataIntegrityViolationEx);
    assertEquals(dataIntegrityViolationEx, divex.getCause());
    SQLException featureNotSupEx = SQLExceptionSubclassFactory.newSQLFeatureNotSupportedException("", "", 0);
    InvalidDataAccessApiUsageException idaex = (InvalidDataAccessApiUsageException) sext.translate("task", "SQL", featureNotSupEx);
    assertEquals(featureNotSupEx, idaex.getCause());
    SQLException dataIntegrityViolationEx2 = SQLExceptionSubclassFactory.newSQLIntegrityConstraintViolationException("", "", 0);
    DataIntegrityViolationException divex2 = (DataIntegrityViolationException) sext.translate("task", "SQL", dataIntegrityViolationEx2);
    assertEquals(dataIntegrityViolationEx2, divex2.getCause());
    SQLException permissionDeniedEx = SQLExceptionSubclassFactory.newSQLInvalidAuthorizationSpecException("", "", 0);
    PermissionDeniedDataAccessException pdaex = (PermissionDeniedDataAccessException) sext.translate("task", "SQL", permissionDeniedEx);
    assertEquals(permissionDeniedEx, pdaex.getCause());
    SQLException dataAccessResourceEx = SQLExceptionSubclassFactory.newSQLNonTransientConnectionException("", "", 0);
    DataAccessResourceFailureException darex = (DataAccessResourceFailureException) sext.translate("task", "SQL", dataAccessResourceEx);
    assertEquals(dataAccessResourceEx, darex.getCause());
    SQLException badSqlEx2 = SQLExceptionSubclassFactory.newSQLSyntaxErrorException("", "", 0);
    BadSqlGrammarException bsgex2 = (BadSqlGrammarException) sext.translate("task", "SQL2", badSqlEx2);
    assertEquals("SQL2", bsgex2.getSql());
    assertEquals(badSqlEx2, bsgex2.getSQLException());
    SQLException tranRollbackEx = SQLExceptionSubclassFactory.newSQLTransactionRollbackException("", "", 0);
    ConcurrencyFailureException cfex = (ConcurrencyFailureException) sext.translate("task", "SQL", tranRollbackEx);
    assertEquals(tranRollbackEx, cfex.getCause());
    SQLException transientConnEx = SQLExceptionSubclassFactory.newSQLTransientConnectionException("", "", 0);
    TransientDataAccessResourceException tdarex = (TransientDataAccessResourceException) sext.translate("task", "SQL", transientConnEx);
    assertEquals(transientConnEx, tdarex.getCause());
    SQLException transientConnEx2 = SQLExceptionSubclassFactory.newSQLTimeoutException("", "", 0);
    QueryTimeoutException tdarex2 = (QueryTimeoutException) sext.translate("task", "SQL", transientConnEx2);
    assertEquals(transientConnEx2, tdarex2.getCause());
    SQLException recoverableEx = SQLExceptionSubclassFactory.newSQLRecoverableException("", "", 0);
    RecoverableDataAccessException rdaex2 = (RecoverableDataAccessException) sext.translate("task", "SQL", recoverableEx);
    assertEquals(recoverableEx, rdaex2.getCause());
    // Test classic error code translation. We should move there next if the exception we pass in is not one
    // of the new sub-classes.
    SQLException sexEct = new SQLException("", "", 1);
    BadSqlGrammarException bsgEct = (BadSqlGrammarException) sext.translate("task", "SQL-ECT", sexEct);
    assertEquals("SQL-ECT", bsgEct.getSql());
    assertEquals(sexEct, bsgEct.getSQLException());
    // Test fallback. We assume that no database will ever return this error code,
    // but 07xxx will be bad grammar picked up by the fallback SQLState translator
    SQLException sexFbt = new SQLException("", "07xxx", 666666666);
    BadSqlGrammarException bsgFbt = (BadSqlGrammarException) sext.translate("task", "SQL-FBT", sexFbt);
    assertEquals("SQL-FBT", bsgFbt.getSql());
    assertEquals(sexFbt, bsgFbt.getSQLException());
    // and 08xxx will be data resource failure (non-transient) picked up by the fallback SQLState translator
    SQLException sexFbt2 = new SQLException("", "08xxx", 666666666);
    DataAccessResourceFailureException darfFbt = (DataAccessResourceFailureException) sext.translate("task", "SQL-FBT2", sexFbt2);
    assertEquals(sexFbt2, darfFbt.getCause());
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) QueryTimeoutException(org.springframework.dao.QueryTimeoutException) TransientDataAccessResourceException(org.springframework.dao.TransientDataAccessResourceException) SQLException(java.sql.SQLException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) PermissionDeniedDataAccessException(org.springframework.dao.PermissionDeniedDataAccessException) RecoverableDataAccessException(org.springframework.dao.RecoverableDataAccessException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.Test)

Aggregations

SQLException (java.sql.SQLException)1 Test (org.junit.Test)1 ConcurrencyFailureException (org.springframework.dao.ConcurrencyFailureException)1 DataAccessResourceFailureException (org.springframework.dao.DataAccessResourceFailureException)1 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)1 InvalidDataAccessApiUsageException (org.springframework.dao.InvalidDataAccessApiUsageException)1 PermissionDeniedDataAccessException (org.springframework.dao.PermissionDeniedDataAccessException)1 QueryTimeoutException (org.springframework.dao.QueryTimeoutException)1 RecoverableDataAccessException (org.springframework.dao.RecoverableDataAccessException)1 TransientDataAccessResourceException (org.springframework.dao.TransientDataAccessResourceException)1 BadSqlGrammarException (org.springframework.jdbc.BadSqlGrammarException)1