use of org.springframework.jdbc.UncategorizedSQLException in project spring by mybatis.
the class MyBatisExceptionTranslator method translateExceptionIfPossible.
/**
* {@inheritDoc}
*/
@Override
public DataAccessException translateExceptionIfPossible(RuntimeException e) {
if (e instanceof PersistenceException) {
// recursion has a risk of infinite loop so better make another if
if (e.getCause() instanceof PersistenceException) {
e = (PersistenceException) e.getCause();
}
if (e.getCause() instanceof SQLException) {
this.initExceptionTranslator();
String task = e.getMessage() + "\n";
SQLException se = (SQLException) e.getCause();
DataAccessException dae = this.exceptionTranslator.translate(task, null, se);
return dae != null ? dae : new UncategorizedSQLException(task, null, se);
} else if (e.getCause() instanceof TransactionException) {
throw (TransactionException) e.getCause();
}
return new MyBatisSystemException(e);
}
return null;
}
use of org.springframework.jdbc.UncategorizedSQLException in project spring-batch by spring-projects.
the class HibernateFailureJobFunctionalTests method testLaunchJob.
@Test
public void testLaunchJob() throws Exception {
validatePreConditions();
JobParameters params = new JobParametersBuilder().addString("key", "failureJob").toJobParameters();
writer.setFailOnFlush(2);
try {
jobLauncherTestUtils.launchJob(params);
} catch (HibernateJdbcException e) {
// RepeatContext:
throw e;
} catch (UncategorizedSQLException e) {
// assertEquals(1, writer.getErrors().size());
throw e;
}
int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "CUSTOMER");
assertEquals(4, after);
validatePostConditions();
}
use of org.springframework.jdbc.UncategorizedSQLException in project springframework-source-5.1.x by wb02125055.
the class AbstractFallbackSQLExceptionTranslator method translate.
/**
* Pre-checks the arguments, calls {@link #doTranslate}, and invokes the
* {@link #getFallbackTranslator() fallback translator} if necessary.
*/
@Override
@NonNull
public DataAccessException translate(String task, @Nullable String sql, SQLException ex) {
Assert.notNull(ex, "Cannot translate a null SQLException");
DataAccessException dae = doTranslate(task, sql, ex);
if (dae != null) {
// Specific exception match found.
return dae;
}
// Looking for a fallback...
SQLExceptionTranslator fallback = getFallbackTranslator();
if (fallback != null) {
dae = fallback.translate(task, sql, ex);
if (dae != null) {
// Fallback exception match found.
return dae;
}
}
// We couldn't identify it more precisely.
return new UncategorizedSQLException(task, sql, ex);
}
use of org.springframework.jdbc.UncategorizedSQLException in project springframework-source-5.1.x by wb02125055.
the class DataSourceTransactionManagerTests method doTestTransactionCommitRestoringAutoCommit.
private void doTestTransactionCommitRestoringAutoCommit(boolean autoCommit, boolean lazyConnection, final boolean createStatement) throws Exception {
if (lazyConnection) {
given(con.getAutoCommit()).willReturn(autoCommit);
given(con.getTransactionIsolation()).willReturn(Connection.TRANSACTION_READ_COMMITTED);
given(con.getWarnings()).willThrow(new SQLException());
}
if (!lazyConnection || createStatement) {
given(con.getAutoCommit()).willReturn(autoCommit);
}
final DataSource dsToUse = (lazyConnection ? new LazyConnectionDataSourceProxy(ds) : ds);
tm = new DataSourceTransactionManager(dsToUse);
TransactionTemplate tt = new TransactionTemplate(tm);
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
assertTrue("Synchronization active", TransactionSynchronizationManager.isSynchronizationActive());
assertTrue("Is new transaction", status.isNewTransaction());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
Connection tCon = DataSourceUtils.getConnection(dsToUse);
try {
if (createStatement) {
tCon.createStatement();
} else {
tCon.getWarnings();
tCon.clearWarnings();
}
} catch (SQLException ex) {
throw new UncategorizedSQLException("", "", ex);
}
}
});
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
if (autoCommit && (!lazyConnection || createStatement)) {
InOrder ordered = inOrder(con);
ordered.verify(con).setAutoCommit(false);
ordered.verify(con).commit();
ordered.verify(con).setAutoCommit(true);
}
if (createStatement) {
verify(con, times(2)).close();
} else {
verify(con).close();
}
}
use of org.springframework.jdbc.UncategorizedSQLException in project springframework-source-5.1.x by wb02125055.
the class DataSourceTransactionManagerTests method testTransactionAwareDataSourceProxyWithSuspension.
@Test
public void testTransactionAwareDataSourceProxyWithSuspension() throws Exception {
given(con.getAutoCommit()).willReturn(true);
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW);
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
assertEquals(con, DataSourceUtils.getConnection(ds));
final TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
try {
assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection());
// should be ignored
dsProxy.getConnection().close();
} catch (SQLException ex) {
throw new UncategorizedSQLException("", "", ex);
}
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
assertEquals(con, DataSourceUtils.getConnection(ds));
try {
assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection());
// should be ignored
dsProxy.getConnection().close();
} catch (SQLException ex) {
throw new UncategorizedSQLException("", "", ex);
}
}
});
try {
assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection());
// should be ignored
dsProxy.getConnection().close();
} catch (SQLException ex) {
throw new UncategorizedSQLException("", "", ex);
}
}
});
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
InOrder ordered = inOrder(con);
ordered.verify(con).setAutoCommit(false);
ordered.verify(con).commit();
ordered.verify(con).setAutoCommit(true);
verify(con, times(2)).close();
}
Aggregations