use of cn.taketoday.dao.TransientDataAccessResourceException 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();
}
use of cn.taketoday.dao.TransientDataAccessResourceException 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);
}
use of cn.taketoday.dao.TransientDataAccessResourceException in project today-infrastructure by TAKETODAY.
the class SqlSessionUtils method registerSessionHolder.
/**
* Register session holder if synchronization is active (i.e. a Framework TX is active).
*
* Note: The DataSource used by the Environment should be synchronized with the transaction either through
* DataSourceTxMgr or another tx synchronization. Further assume that if an exception is thrown, whatever started the
* transaction will handle closing / rolling back the Connection associated with the SqlSession.
*
* @param sessionFactory sqlSessionFactory used for registration.
* @param executorType executorType used for registration.
* @param exceptionTranslator persistenceExceptionTranslator used for registration.
* @param session sqlSession used for registration.
*/
private static void registerSessionHolder(SqlSessionFactory sessionFactory, ExecutorType executorType, @Nullable PersistenceExceptionTranslator exceptionTranslator, SqlSession session) {
SynchronizationInfo info = TransactionSynchronizationManager.getSynchronizationInfo();
if (info.isSynchronizationActive()) {
Environment environment = sessionFactory.getConfiguration().getEnvironment();
if (environment.getTransactionFactory() instanceof ManagedTransactionFactory) {
if (debugEnabled) {
log.debug("Registering transaction synchronization for SqlSession [{}]", session);
}
SqlSessionHolder holder = new SqlSessionHolder(session, executorType, exceptionTranslator);
info.bindResource(sessionFactory, holder);
info.registerSynchronization(new SqlSessionSynchronization(holder, sessionFactory));
holder.setSynchronizedWithTransaction(true);
holder.requested();
} else {
if (info.getResource(environment.getDataSource()) == null) {
if (debugEnabled) {
log.debug("SqlSession [{}] was not registered for synchronization because DataSource is not transactional", session);
}
} else {
throw new TransientDataAccessResourceException("SqlSessionFactory must be using a ManagedTransactionFactory in order to use transaction synchronization");
}
}
} else if (debugEnabled) {
log.debug("SqlSession [{}] was not registered for synchronization because synchronization is not active", session);
}
}
use of cn.taketoday.dao.TransientDataAccessResourceException in project today-infrastructure by TAKETODAY.
the class MapperFactoryBeanTest method testNonTodayTxMgrWithTx.
// active transaction using the DataSource, but without a TodayTransactionFactory
// this should error
@Test
void testNonTodayTxMgrWithTx() throws Exception {
Environment original = sqlSessionFactory.getConfiguration().getEnvironment();
Environment nonToday = new Environment("non-today", new JdbcTransactionFactory(), dataSource);
sqlSessionFactory.getConfiguration().setEnvironment(nonToday);
TransactionStatus status = null;
try {
status = txManager.getTransaction(new DefaultTransactionDefinition());
find();
fail("should not be able to get an SqlSession using non-Today tx manager when there is an active Today tx");
} catch (TransientDataAccessResourceException e) {
assertThat(e.getMessage()).isEqualTo("SqlSessionFactory must be using a ManagedTransactionFactory in order to use" + " transaction synchronization");
} finally {
// rollback required to close connection
txManager.rollback(status);
sqlSessionFactory.getConfiguration().setEnvironment(original);
}
}
use of cn.taketoday.dao.TransientDataAccessResourceException in project today-framework by TAKETODAY.
the class SqlSessionUtils method registerSessionHolder.
/**
* Register session holder if synchronization is active (i.e. a Framework TX is active).
*
* Note: The DataSource used by the Environment should be synchronized with the transaction either through
* DataSourceTxMgr or another tx synchronization. Further assume that if an exception is thrown, whatever started the
* transaction will handle closing / rolling back the Connection associated with the SqlSession.
*
* @param sessionFactory sqlSessionFactory used for registration.
* @param executorType executorType used for registration.
* @param exceptionTranslator persistenceExceptionTranslator used for registration.
* @param session sqlSession used for registration.
*/
private static void registerSessionHolder(SqlSessionFactory sessionFactory, ExecutorType executorType, @Nullable PersistenceExceptionTranslator exceptionTranslator, SqlSession session) {
SynchronizationInfo info = TransactionSynchronizationManager.getSynchronizationInfo();
if (info.isSynchronizationActive()) {
Environment environment = sessionFactory.getConfiguration().getEnvironment();
if (environment.getTransactionFactory() instanceof ManagedTransactionFactory) {
if (debugEnabled) {
log.debug("Registering transaction synchronization for SqlSession [{}]", session);
}
SqlSessionHolder holder = new SqlSessionHolder(session, executorType, exceptionTranslator);
info.bindResource(sessionFactory, holder);
info.registerSynchronization(new SqlSessionSynchronization(holder, sessionFactory));
holder.setSynchronizedWithTransaction(true);
holder.requested();
} else {
if (info.getResource(environment.getDataSource()) == null) {
if (debugEnabled) {
log.debug("SqlSession [{}] was not registered for synchronization because DataSource is not transactional", session);
}
} else {
throw new TransientDataAccessResourceException("SqlSessionFactory must be using a ManagedTransactionFactory in order to use transaction synchronization");
}
}
} else if (debugEnabled) {
log.debug("SqlSession [{}] was not registered for synchronization because synchronization is not active", session);
}
}
Aggregations