use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.
the class JdbcTransactionManagerTests method testParticipatingTransactionWithTransactionStartedFromSynch.
@Test
public void testParticipatingTransactionWithTransactionStartedFromSynch() throws Exception {
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
final TestTransactionSynchronization synch = new TestTransactionSynchronization(ds, TransactionSynchronization.STATUS_COMMITTED) {
@Override
protected void doAfterCompletion(int status) {
super.doAfterCompletion(status);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
}
});
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
});
}
};
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
TransactionSynchronizationManager.registerSynchronization(synch);
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(synch.beforeCommitCalled).isTrue();
assertThat(synch.beforeCompletionCalled).isTrue();
assertThat(synch.afterCommitCalled).isTrue();
assertThat(synch.afterCompletionCalled).isTrue();
boolean condition3 = synch.afterCompletionException instanceof IllegalStateException;
assertThat(condition3).isTrue();
verify(con, times(2)).commit();
verify(con, times(2)).close();
}
use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.
the class JdbcTransactionManagerTests method transactionWithTimeout.
@ParameterizedTest(name = "transaction with {0} second timeout")
@ValueSource(ints = { 1, 10 })
@EnabledForTestGroups(LONG_RUNNING)
public void transactionWithTimeout(int timeout) throws Exception {
PreparedStatement ps = mock(PreparedStatement.class);
given(con.getAutoCommit()).willReturn(true);
given(con.prepareStatement("some SQL statement")).willReturn(ps);
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setTimeout(timeout);
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition1).as("Hasn't thread connection").isTrue();
try {
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
try {
Thread.sleep(1500);
} catch (InterruptedException ex) {
}
try {
Connection con = DataSourceUtils.getConnection(ds);
PreparedStatement ps = con.prepareStatement("some SQL statement");
DataSourceUtils.applyTransactionTimeout(ps, ds);
} catch (SQLException ex) {
throw new DataAccessResourceFailureException("", ex);
}
}
});
if (timeout <= 1) {
fail("Should have thrown TransactionTimedOutException");
}
} catch (TransactionTimedOutException ex) {
if (timeout <= 1) {
// expected
} else {
throw ex;
}
}
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
if (timeout > 1) {
verify(ps).setQueryTimeout(timeout - 1);
verify(con).commit();
} else {
verify(con).rollback();
}
InOrder ordered = inOrder(con);
ordered.verify(con).setAutoCommit(false);
ordered.verify(con).setAutoCommit(true);
verify(con).close();
}
use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.
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();
}
use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.
the class JdbcTransactionManagerTests method doTestTransactionRollbackRestoringAutoCommit.
private void doTestTransactionRollbackRestoringAutoCommit(boolean autoCommit, boolean lazyConnection, final boolean createStatement) throws Exception {
if (lazyConnection) {
given(con.getAutoCommit()).willReturn(autoCommit);
given(con.getTransactionIsolation()).willReturn(Connection.TRANSACTION_READ_COMMITTED);
}
if (!lazyConnection || createStatement) {
given(con.getAutoCommit()).willReturn(autoCommit);
}
final DataSource dsToUse = (lazyConnection ? new LazyConnectionDataSourceProxy(ds) : ds);
tm = new JdbcTransactionManager(dsToUse);
TransactionTemplate tt = new TransactionTemplate(tm);
boolean condition3 = !TransactionSynchronizationManager.hasResource(dsToUse);
assertThat(condition3).as("Hasn't thread connection").isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).as("Synchronization not active").isTrue();
final RuntimeException ex = new RuntimeException("Application exception");
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertThat(TransactionSynchronizationManager.hasResource(dsToUse)).as("Has thread connection").isTrue();
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization active").isTrue();
assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
Connection con = DataSourceUtils.getConnection(dsToUse);
if (createStatement) {
try {
con.createStatement();
} catch (SQLException ex) {
throw new UncategorizedSQLException("", "", ex);
}
}
throw ex;
}
})).isEqualTo(ex);
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition1).as("Hasn't thread connection").isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).as("Synchronization not active").isTrue();
if (autoCommit && (!lazyConnection || createStatement)) {
InOrder ordered = inOrder(con);
ordered.verify(con).setAutoCommit(false);
ordered.verify(con).rollback();
ordered.verify(con).setAutoCommit(true);
}
if (createStatement) {
verify(con, times(2)).close();
} else {
verify(con).close();
}
}
use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.
the class JdbcTransactionManagerTests method testTransactionWithExceptionOnBegin.
/**
* Test behavior if the first operation on a connection (getAutoCommit) throws SQLException.
*/
@Test
public void testTransactionWithExceptionOnBegin() throws Exception {
willThrow(new SQLException("Cannot begin")).given(con).getAutoCommit();
TransactionTemplate tt = new TransactionTemplate(tm);
assertThatExceptionOfType(CannotCreateTransactionException.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();
}
Aggregations