Search in sources :

Example 1 with ConcurrencyFailureException

use of cn.taketoday.dao.ConcurrencyFailureException in project today-infrastructure by TAKETODAY.

the class JdbcTransactionManagerTests method testTransactionWithDataAccessExceptionOnRollback.

@Test
public void testTransactionWithDataAccessExceptionOnRollback() throws Exception {
    given(con.getAutoCommit()).willReturn(true);
    willThrow(new SQLException("Cannot rollback")).given(con).rollback();
    tm.setExceptionTranslator((task, sql, ex) -> new ConcurrencyFailureException(task));
    TransactionTemplate tt = new TransactionTemplate(tm);
    assertThatExceptionOfType(ConcurrencyFailureException.class).isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            status.setRollbackOnly();
        }
    }));
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    InOrder ordered = inOrder(con);
    ordered.verify(con).setAutoCommit(false);
    ordered.verify(con).rollback();
    ordered.verify(con).setAutoCommit(true);
    verify(con).close();
}
Also used : InOrder(org.mockito.InOrder) SQLException(java.sql.SQLException) UncategorizedSQLException(cn.taketoday.jdbc.UncategorizedSQLException) ConcurrencyFailureException(cn.taketoday.dao.ConcurrencyFailureException) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) TransactionStatus(cn.taketoday.transaction.TransactionStatus) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with ConcurrencyFailureException

use of cn.taketoday.dao.ConcurrencyFailureException 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 3 with ConcurrencyFailureException

use of cn.taketoday.dao.ConcurrencyFailureException in project today-framework by TAKETODAY.

the class JdbcTransactionManagerTests method testTransactionWithDataAccessExceptionOnCommit.

@Test
public void testTransactionWithDataAccessExceptionOnCommit() throws Exception {
    willThrow(new SQLException("Cannot commit")).given(con).commit();
    tm.setExceptionTranslator((task, sql, ex) -> new ConcurrencyFailureException(task));
    TransactionTemplate tt = new TransactionTemplate(tm);
    assertThatExceptionOfType(ConcurrencyFailureException.class).isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
        // something transactional
        }
    }));
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    verify(con).close();
}
Also used : SQLException(java.sql.SQLException) UncategorizedSQLException(cn.taketoday.jdbc.UncategorizedSQLException) ConcurrencyFailureException(cn.taketoday.dao.ConcurrencyFailureException) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) TransactionStatus(cn.taketoday.transaction.TransactionStatus) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with ConcurrencyFailureException

use of cn.taketoday.dao.ConcurrencyFailureException in project today-framework by TAKETODAY.

the class JdbcTransactionManagerTests method testTransactionWithDataAccessExceptionOnRollback.

@Test
public void testTransactionWithDataAccessExceptionOnRollback() throws Exception {
    given(con.getAutoCommit()).willReturn(true);
    willThrow(new SQLException("Cannot rollback")).given(con).rollback();
    tm.setExceptionTranslator((task, sql, ex) -> new ConcurrencyFailureException(task));
    TransactionTemplate tt = new TransactionTemplate(tm);
    assertThatExceptionOfType(ConcurrencyFailureException.class).isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            status.setRollbackOnly();
        }
    }));
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    InOrder ordered = inOrder(con);
    ordered.verify(con).setAutoCommit(false);
    ordered.verify(con).rollback();
    ordered.verify(con).setAutoCommit(true);
    verify(con).close();
}
Also used : InOrder(org.mockito.InOrder) SQLException(java.sql.SQLException) UncategorizedSQLException(cn.taketoday.jdbc.UncategorizedSQLException) ConcurrencyFailureException(cn.taketoday.dao.ConcurrencyFailureException) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) TransactionStatus(cn.taketoday.transaction.TransactionStatus) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with ConcurrencyFailureException

use of cn.taketoday.dao.ConcurrencyFailureException in project today-framework 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)

Aggregations

ConcurrencyFailureException (cn.taketoday.dao.ConcurrencyFailureException)6 SQLException (java.sql.SQLException)6 Test (org.junit.jupiter.api.Test)6 UncategorizedSQLException (cn.taketoday.jdbc.UncategorizedSQLException)4 TransactionStatus (cn.taketoday.transaction.TransactionStatus)4 TransactionCallbackWithoutResult (cn.taketoday.transaction.support.TransactionCallbackWithoutResult)4 TransactionTemplate (cn.taketoday.transaction.support.TransactionTemplate)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 DataAccessResourceFailureException (cn.taketoday.dao.DataAccessResourceFailureException)2 DataIntegrityViolationException (cn.taketoday.dao.DataIntegrityViolationException)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 TransientDataAccessResourceException (cn.taketoday.dao.TransientDataAccessResourceException)2 BadSqlGrammarException (cn.taketoday.jdbc.BadSqlGrammarException)2 InOrder (org.mockito.InOrder)2