Search in sources :

Example 1 with BadSqlGrammarException

use of cn.taketoday.jdbc.BadSqlGrammarException in project today-infrastructure by TAKETODAY.

the class CustomSQLExceptionTranslatorRegistrarTests method customErrorCodeTranslation.

@Test
@SuppressWarnings("resource")
public void customErrorCodeTranslation() {
    new ClassPathXmlApplicationContext("test-custom-translators-context.xml", CustomSQLExceptionTranslatorRegistrarTests.class);
    SQLErrorCodes codes = SQLErrorCodesFactory.getInstance().getErrorCodes("H2");
    SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator();
    sext.setSqlErrorCodes(codes);
    DataAccessException exFor4200 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 42000));
    assertThat(exFor4200).as("Should have been translated").isNotNull();
    assertThat(BadSqlGrammarException.class.isAssignableFrom(exFor4200.getClass())).as("Should have been instance of BadSqlGrammarException").isTrue();
    DataAccessException exFor2 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 2));
    assertThat(exFor2).as("Should have been translated").isNotNull();
    assertThat(TransientDataAccessResourceException.class.isAssignableFrom(exFor2.getClass())).as("Should have been instance of TransientDataAccessResourceException").isTrue();
    DataAccessException exFor3 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 3));
    assertThat(exFor3).as("Should not have been translated").isNull();
}
Also used : BadSqlGrammarException(cn.taketoday.jdbc.BadSqlGrammarException) TransientDataAccessResourceException(cn.taketoday.dao.TransientDataAccessResourceException) ClassPathXmlApplicationContext(cn.taketoday.context.support.ClassPathXmlApplicationContext) SQLException(java.sql.SQLException) DataAccessException(cn.taketoday.dao.DataAccessException) Test(org.junit.jupiter.api.Test)

Example 2 with BadSqlGrammarException

use of cn.taketoday.jdbc.BadSqlGrammarException in project today-infrastructure by TAKETODAY.

the class SQLErrorCodeSQLExceptionTranslatorTests method batchExceptionTranslation.

@Test
public void batchExceptionTranslation() {
    SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
    SQLException badSqlEx = new SQLException("", "", 1);
    BatchUpdateException batchUpdateEx = new BatchUpdateException();
    batchUpdateEx.setNextException(badSqlEx);
    BadSqlGrammarException bsgex = (BadSqlGrammarException) sext.translate("task", "SQL", batchUpdateEx);
    assertThat(bsgex.getSql()).isEqualTo("SQL");
    assertThat((Object) bsgex.getSQLException()).isEqualTo(badSqlEx);
}
Also used : BadSqlGrammarException(cn.taketoday.jdbc.BadSqlGrammarException) SQLException(java.sql.SQLException) BatchUpdateException(java.sql.BatchUpdateException) Test(org.junit.jupiter.api.Test)

Example 3 with BadSqlGrammarException

use of cn.taketoday.jdbc.BadSqlGrammarException in project today-infrastructure by TAKETODAY.

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);
    assertThat(divex.getCause()).isEqualTo(dataIntegrityViolationEx);
    SQLException featureNotSupEx = SQLExceptionSubclassFactory.newSQLFeatureNotSupportedException("", "", 0);
    InvalidDataAccessApiUsageException idaex = (InvalidDataAccessApiUsageException) sext.translate("task", "SQL", featureNotSupEx);
    assertThat(idaex.getCause()).isEqualTo(featureNotSupEx);
    SQLException dataIntegrityViolationEx2 = SQLExceptionSubclassFactory.newSQLIntegrityConstraintViolationException("", "", 0);
    DataIntegrityViolationException divex2 = (DataIntegrityViolationException) sext.translate("task", "SQL", dataIntegrityViolationEx2);
    assertThat(divex2.getCause()).isEqualTo(dataIntegrityViolationEx2);
    SQLException permissionDeniedEx = SQLExceptionSubclassFactory.newSQLInvalidAuthorizationSpecException("", "", 0);
    PermissionDeniedDataAccessException pdaex = (PermissionDeniedDataAccessException) sext.translate("task", "SQL", permissionDeniedEx);
    assertThat(pdaex.getCause()).isEqualTo(permissionDeniedEx);
    SQLException dataAccessResourceEx = SQLExceptionSubclassFactory.newSQLNonTransientConnectionException("", "", 0);
    DataAccessResourceFailureException darex = (DataAccessResourceFailureException) sext.translate("task", "SQL", dataAccessResourceEx);
    assertThat(darex.getCause()).isEqualTo(dataAccessResourceEx);
    SQLException badSqlEx2 = SQLExceptionSubclassFactory.newSQLSyntaxErrorException("", "", 0);
    BadSqlGrammarException bsgex2 = (BadSqlGrammarException) sext.translate("task", "SQL2", badSqlEx2);
    assertThat(bsgex2.getSql()).isEqualTo("SQL2");
    assertThat((Object) bsgex2.getSQLException()).isEqualTo(badSqlEx2);
    SQLException tranRollbackEx = SQLExceptionSubclassFactory.newSQLTransactionRollbackException("", "", 0);
    ConcurrencyFailureException cfex = (ConcurrencyFailureException) sext.translate("task", "SQL", tranRollbackEx);
    assertThat(cfex.getCause()).isEqualTo(tranRollbackEx);
    SQLException transientConnEx = SQLExceptionSubclassFactory.newSQLTransientConnectionException("", "", 0);
    TransientDataAccessResourceException tdarex = (TransientDataAccessResourceException) sext.translate("task", "SQL", transientConnEx);
    assertThat(tdarex.getCause()).isEqualTo(transientConnEx);
    SQLException transientConnEx2 = SQLExceptionSubclassFactory.newSQLTimeoutException("", "", 0);
    QueryTimeoutException tdarex2 = (QueryTimeoutException) sext.translate("task", "SQL", transientConnEx2);
    assertThat(tdarex2.getCause()).isEqualTo(transientConnEx2);
    SQLException recoverableEx = SQLExceptionSubclassFactory.newSQLRecoverableException("", "", 0);
    RecoverableDataAccessException rdaex2 = (RecoverableDataAccessException) sext.translate("task", "SQL", recoverableEx);
    assertThat(rdaex2.getCause()).isEqualTo(recoverableEx);
    // 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);
    assertThat(bsgEct.getSql()).isEqualTo("SQL-ECT");
    assertThat((Object) bsgEct.getSQLException()).isEqualTo(sexEct);
    // 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);
    assertThat(bsgFbt.getSql()).isEqualTo("SQL-FBT");
    assertThat((Object) bsgFbt.getSQLException()).isEqualTo(sexFbt);
    // 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);
    assertThat(darfFbt.getCause()).isEqualTo(sexFbt2);
}
Also used : BadSqlGrammarException(cn.taketoday.jdbc.BadSqlGrammarException) TransientDataAccessResourceException(cn.taketoday.dao.TransientDataAccessResourceException) SQLException(java.sql.SQLException) DataAccessResourceFailureException(cn.taketoday.dao.DataAccessResourceFailureException) PermissionDeniedDataAccessException(cn.taketoday.dao.PermissionDeniedDataAccessException) DataIntegrityViolationException(cn.taketoday.dao.DataIntegrityViolationException) QueryTimeoutException(cn.taketoday.dao.QueryTimeoutException) InvalidDataAccessApiUsageException(cn.taketoday.dao.InvalidDataAccessApiUsageException) ConcurrencyFailureException(cn.taketoday.dao.ConcurrencyFailureException) RecoverableDataAccessException(cn.taketoday.dao.RecoverableDataAccessException) Test(org.junit.jupiter.api.Test)

Example 4 with BadSqlGrammarException

use of cn.taketoday.jdbc.BadSqlGrammarException in project today-framework by TAKETODAY.

the class SQLErrorCodeSQLExceptionTranslatorTests method errorCodeTranslation.

@Test
public void errorCodeTranslation() {
    SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
    SQLException badSqlEx = new SQLException("", "", 1);
    BadSqlGrammarException bsgex = (BadSqlGrammarException) sext.translate("task", "SQL", badSqlEx);
    assertThat(bsgex.getSql()).isEqualTo("SQL");
    assertThat((Object) bsgex.getSQLException()).isEqualTo(badSqlEx);
    SQLException invResEx = new SQLException("", "", 4);
    InvalidResultSetAccessException irsex = (InvalidResultSetAccessException) sext.translate("task", "SQL", invResEx);
    assertThat(irsex.getSql()).isEqualTo("SQL");
    assertThat((Object) irsex.getSQLException()).isEqualTo(invResEx);
    checkTranslation(sext, 5, DataAccessResourceFailureException.class);
    checkTranslation(sext, 6, DataIntegrityViolationException.class);
    checkTranslation(sext, 7, CannotAcquireLockException.class);
    checkTranslation(sext, 8, DeadlockLoserDataAccessException.class);
    checkTranslation(sext, 9, CannotSerializeTransactionException.class);
    checkTranslation(sext, 10, DuplicateKeyException.class);
    SQLException dupKeyEx = new SQLException("", "", 10);
    DataAccessException dksex = sext.translate("task", "SQL", dupKeyEx);
    assertThat(DataIntegrityViolationException.class.isInstance(dksex)).as("Not instance of DataIntegrityViolationException").isTrue();
    // 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 sex = new SQLException("", "07xxx", 666666666);
    BadSqlGrammarException bsgex2 = (BadSqlGrammarException) sext.translate("task", "SQL2", sex);
    assertThat(bsgex2.getSql()).isEqualTo("SQL2");
    assertThat((Object) bsgex2.getSQLException()).isEqualTo(sex);
}
Also used : BadSqlGrammarException(cn.taketoday.jdbc.BadSqlGrammarException) SQLException(java.sql.SQLException) InvalidResultSetAccessException(cn.taketoday.jdbc.InvalidResultSetAccessException) DeadlockLoserDataAccessException(cn.taketoday.dao.DeadlockLoserDataAccessException) DataAccessException(cn.taketoday.dao.DataAccessException) DataIntegrityViolationException(cn.taketoday.dao.DataIntegrityViolationException) Test(org.junit.jupiter.api.Test)

Example 5 with BadSqlGrammarException

use of cn.taketoday.jdbc.BadSqlGrammarException in project today-framework by TAKETODAY.

the class SQLErrorCodeSQLExceptionTranslatorTests method batchExceptionTranslation.

@Test
public void batchExceptionTranslation() {
    SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
    SQLException badSqlEx = new SQLException("", "", 1);
    BatchUpdateException batchUpdateEx = new BatchUpdateException();
    batchUpdateEx.setNextException(badSqlEx);
    BadSqlGrammarException bsgex = (BadSqlGrammarException) sext.translate("task", "SQL", batchUpdateEx);
    assertThat(bsgex.getSql()).isEqualTo("SQL");
    assertThat((Object) bsgex.getSQLException()).isEqualTo(badSqlEx);
}
Also used : BadSqlGrammarException(cn.taketoday.jdbc.BadSqlGrammarException) SQLException(java.sql.SQLException) BatchUpdateException(java.sql.BatchUpdateException) Test(org.junit.jupiter.api.Test)

Aggregations

BadSqlGrammarException (cn.taketoday.jdbc.BadSqlGrammarException)8 SQLException (java.sql.SQLException)8 Test (org.junit.jupiter.api.Test)8 DataAccessException (cn.taketoday.dao.DataAccessException)4 DataIntegrityViolationException (cn.taketoday.dao.DataIntegrityViolationException)4 TransientDataAccessResourceException (cn.taketoday.dao.TransientDataAccessResourceException)4 ClassPathXmlApplicationContext (cn.taketoday.context.support.ClassPathXmlApplicationContext)2 ConcurrencyFailureException (cn.taketoday.dao.ConcurrencyFailureException)2 DataAccessResourceFailureException (cn.taketoday.dao.DataAccessResourceFailureException)2 DeadlockLoserDataAccessException (cn.taketoday.dao.DeadlockLoserDataAccessException)2 InvalidDataAccessApiUsageException (cn.taketoday.dao.InvalidDataAccessApiUsageException)2 PermissionDeniedDataAccessException (cn.taketoday.dao.PermissionDeniedDataAccessException)2 QueryTimeoutException (cn.taketoday.dao.QueryTimeoutException)2 RecoverableDataAccessException (cn.taketoday.dao.RecoverableDataAccessException)2 InvalidResultSetAccessException (cn.taketoday.jdbc.InvalidResultSetAccessException)2 BatchUpdateException (java.sql.BatchUpdateException)2