use of javax.transaction.UserTransaction in project spring-framework by spring-projects.
the class JndiJtaTransactionManagerTests method jtaTransactionManagerWithNotCacheUserTransaction.
@Test
public void jtaTransactionManagerWithNotCacheUserTransaction() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
UserTransaction ut2 = mock(UserTransaction.class);
given(ut2.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
JtaTransactionManager ptm = new JtaTransactionManager();
ptm.setJndiTemplate(new ExpectedLookupTemplate("java:comp/UserTransaction", ut));
ptm.setCacheUserTransaction(false);
ptm.afterPropertiesSet();
assertEquals(ut, ptm.getUserTransaction());
TransactionTemplate tt = new TransactionTemplate(ptm);
assertEquals(JtaTransactionManager.SYNCHRONIZATION_ALWAYS, ptm.getTransactionSynchronization());
assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
}
});
ptm.setJndiTemplate(new ExpectedLookupTemplate("java:comp/UserTransaction", ut2));
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
}
});
assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
verify(ut).begin();
verify(ut).commit();
verify(ut2).begin();
verify(ut2).commit();
}
use of javax.transaction.UserTransaction in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithHeuristicRollbackExceptionOnCommit.
@Test
public void jtaTransactionManagerWithHeuristicRollbackExceptionOnCommit() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
willThrow(new HeuristicRollbackException("heuristic exception")).given(ut).commit();
try {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCompletion(int status) {
assertTrue("Correct completion status", status == TransactionSynchronization.STATUS_UNKNOWN);
}
});
}
});
fail("Should have thrown HeuristicCompletionException");
} catch (HeuristicCompletionException ex) {
// expected
assertTrue(ex.getOutcomeState() == HeuristicCompletionException.STATE_ROLLED_BACK);
}
verify(ut).begin();
}
use of javax.transaction.UserTransaction in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithUnsupportedOperationExceptionOnNestedBegin.
@Test
public void jtaTransactionManagerWithUnsupportedOperationExceptionOnNestedBegin() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
willThrow(new UnsupportedOperationException("not supported")).given(ut).begin();
try {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
}
});
fail("Should have thrown NestedTransactionNotSupportedException");
} catch (NestedTransactionNotSupportedException ex) {
// expected
}
}
use of javax.transaction.UserTransaction in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithRollbackExceptionOnCommit.
@Test
public void jtaTransactionManagerWithRollbackExceptionOnCommit() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
willThrow(new RollbackException("unexpected rollback")).given(ut).commit();
try {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCompletion(int status) {
assertTrue("Correct completion status", status == TransactionSynchronization.STATUS_ROLLED_BACK);
}
});
}
});
fail("Should have thrown UnexpectedRollbackException");
} catch (UnexpectedRollbackException ex) {
// expected
}
verify(ut).begin();
}
use of javax.transaction.UserTransaction 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);
}
Aggregations