use of org.springframework.transaction.jta.JtaTransactionManager in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithExistingAndPropagationSupports.
@Test
public void jtaTransactionManagerWithExistingAndPropagationSupports() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
final TransactionSynchronization synch = mock(TransactionSynchronization.class);
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
TransactionSynchronizationManager.registerSynchronization(synch);
status.setRollbackOnly();
}
});
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
verify(ut).setRollbackOnly();
verify(synch).beforeCompletion();
verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
use of org.springframework.transaction.jta.JtaTransactionManager in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithDoubleCommit.
@Test
public void jtaTransactionManagerWithDoubleCommit() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
JtaTransactionManager ptm = newJtaTransactionManager(ut);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
TransactionStatus status = ptm.getTransaction(new DefaultTransactionDefinition());
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
// first commit
ptm.commit(status);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
try {
// second commit attempt
ptm.commit(status);
fail("Should have thrown IllegalTransactionStateException");
} catch (IllegalTransactionStateException ex) {
// expected
}
verify(ut).begin();
verify(ut).commit();
}
use of org.springframework.transaction.jta.JtaTransactionManager in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNewWithinSupports.
@Test
public void jtaTransactionManagerWithPropagationRequiresNewWithinSupports() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
final JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
TransactionTemplate tt2 = new TransactionTemplate(ptm);
tt2.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
tt2.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
}
});
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
}
});
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
verify(ut).begin();
verify(ut).commit();
}
use of org.springframework.transaction.jta.JtaTransactionManager in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithSystemExceptionOnRollbackOnly.
@Test
public void jtaTransactionManagerWithSystemExceptionOnRollbackOnly() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
willThrow(new SystemException("system exception")).given(ut).setRollbackOnly();
try {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
status.setRollbackOnly();
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCompletion(int status) {
assertTrue("Correct completion status", status == TransactionSynchronization.STATUS_UNKNOWN);
}
});
}
});
fail("Should have thrown TransactionSystemException");
} catch (TransactionSystemException ex) {
// expected
}
}
use of org.springframework.transaction.jta.JtaTransactionManager in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithExistingTransactionAndSynchronizationNever.
@Test
public void jtaTransactionManagerWithExistingTransactionAndSynchronizationNever() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_NEVER);
ptm.afterPropertiesSet();
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
status.setRollbackOnly();
}
});
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
verify(ut).setRollbackOnly();
}
Aggregations