use of org.springframework.transaction.jta.JtaTransactionManager in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNewAndExistingWithBeginException.
@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndExistingWithBeginException() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
TransactionManager tm = mock(TransactionManager.class);
Transaction tx = mock(Transaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
given(tm.suspend()).willReturn(tx);
willThrow(new SystemException()).given(ut).begin();
JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
try {
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
}
});
fail("Should have thrown CannotCreateTransactionException");
} catch (CannotCreateTransactionException ex) {
// expected
}
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
verify(tm).resume(tx);
}
use of org.springframework.transaction.jta.JtaTransactionManager in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithRollbackAndSynchronizationNever.
@Test
public void jtaTransactionManagerWithRollbackAndSynchronizationNever() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE);
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
ptm.setTransactionSynchronizationName("SYNCHRONIZATION_NEVER");
tt.setTimeout(10);
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).setTransactionTimeout(10);
verify(ut).begin();
verify(ut, atLeastOnce()).getStatus();
verify(ut).rollback();
}
use of org.springframework.transaction.jta.JtaTransactionManager in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithExistingTransactionAndJtaSynchronization.
@Test
public void jtaTransactionManagerWithExistingTransactionAndJtaSynchronization() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
TransactionManager tm = mock(TransactionManager.class);
MockJtaTransaction tx = new MockJtaTransaction();
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
given(tm.getTransaction()).willReturn(tx);
final TransactionSynchronization synch = mock(TransactionSynchronization.class);
JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
TransactionTemplate tt = new TransactionTemplate(ptm);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
TransactionSynchronizationManager.registerSynchronization(synch);
status.setRollbackOnly();
}
});
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
assertNotNull(tx.getSynchronization());
tx.getSynchronization().beforeCompletion();
tx.getSynchronization().afterCompletion(Status.STATUS_ROLLEDBACK);
verify(ut).setRollbackOnly();
verify(synch).beforeCompletion();
verify(synch).afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
}
use of org.springframework.transaction.jta.JtaTransactionManager in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithExistingTransactionAndException.
@Test
public void jtaTransactionManagerWithExistingTransactionAndException() 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);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
try {
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
TransactionSynchronizationManager.registerSynchronization(synch);
throw new IllegalStateException("I want a rollback");
}
});
fail("Should have thrown IllegalStateException");
} catch (IllegalStateException ex) {
// expected
}
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 jtaTransactionManagerWithSystemExceptionOnIsExisting.
@Test
public void jtaTransactionManagerWithSystemExceptionOnIsExisting() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willThrow(new SystemException("system exception"));
try {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
}
});
fail("Should have thrown TransactionSystemException");
} catch (TransactionSystemException ex) {
// expected
}
}
Aggregations