use of org.springframework.transaction.TransactionStatus in project spring-framework by spring-projects.
the class DataSourceTransactionManagerTests method testParticipatingTransactionWithTransactionStartedFromSynch.
@Test
public void testParticipatingTransactionWithTransactionStartedFromSynch() throws Exception {
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
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 TransactionSynchronizationAdapter() {
});
}
};
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
TransactionSynchronizationManager.registerSynchronization(synch);
}
});
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
assertTrue(synch.beforeCommitCalled);
assertTrue(synch.beforeCompletionCalled);
assertTrue(synch.afterCommitCalled);
assertTrue(synch.afterCompletionCalled);
assertTrue(synch.afterCompletionException instanceof IllegalStateException);
verify(con, times(2)).commit();
verify(con, times(2)).close();
}
use of org.springframework.transaction.TransactionStatus in project spring-framework by spring-projects.
the class DataSourceTransactionManagerTests method testPropagationNotSupportedWithExistingTransaction.
@Test
public void testPropagationNotSupportedWithExistingTransaction() throws Exception {
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertTrue("Is new transaction", status.isNewTransaction());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
assertTrue("Synchronization active", TransactionSynchronizationManager.isSynchronizationActive());
assertTrue("Isn't new transaction", !status.isNewTransaction());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
status.setRollbackOnly();
}
});
assertTrue("Is new transaction", status.isNewTransaction());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
}
});
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
verify(con).commit();
verify(con).close();
}
use of org.springframework.transaction.TransactionStatus in project spring-framework by spring-projects.
the class DataSourceTransactionManagerTests method testTransactionWithPropagationNotSupported.
@Test
public void testTransactionWithPropagationNotSupported() throws Exception {
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
assertTrue("Is not new transaction", !status.isNewTransaction());
}
});
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
}
use of org.springframework.transaction.TransactionStatus in project spring-framework by spring-projects.
the class DataSourceTransactionManagerTests method testParticipatingTransactionWithIncompatibleIsolationLevel.
@Test
public void testParticipatingTransactionWithIncompatibleIsolationLevel() throws Exception {
tm.setValidateExistingTransaction(true);
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
try {
final TransactionTemplate tt = new TransactionTemplate(tm);
final TransactionTemplate tt2 = new TransactionTemplate(tm);
tt2.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertFalse("Is not rollback-only", status.isRollbackOnly());
tt2.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
status.setRollbackOnly();
}
});
assertTrue("Is rollback-only", status.isRollbackOnly());
}
});
fail("Should have thrown IllegalTransactionStateException");
} catch (IllegalTransactionStateException ex) {
// expected
}
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
verify(con).rollback();
verify(con).close();
}
use of org.springframework.transaction.TransactionStatus in project spring-framework by spring-projects.
the class DataSourceTransactionManagerTests method testTransactionWithPropagationSupports.
@Test
public void testTransactionWithPropagationSupports() throws Exception {
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
assertTrue("Is not new transaction", !status.isNewTransaction());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
}
});
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
}
Aggregations