use of javax.transaction.UserTransaction in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithCommitAndSynchronizationNever.
@Test
public void jtaTransactionManagerWithCommitAndSynchronizationNever() 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);
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());
}
});
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
verify(ut).begin();
verify(ut).commit();
}
use of javax.transaction.UserTransaction in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithNotSupportedExceptionOnNestedBegin.
@Test
public void jtaTransactionManagerWithNotSupportedExceptionOnNestedBegin() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
willThrow(new NotSupportedException("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 jtaTransactionManagerWithIsolationLevel.
@Test
public void jtaTransactionManagerWithIsolationLevel() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);
try {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
}
});
fail("Should have thrown InvalidIsolationLevelException");
} catch (InvalidIsolationLevelException ex) {
// expected
}
}
use of javax.transaction.UserTransaction in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationNotSupported.
@Test
public void jtaTransactionManagerWithPropagationNotSupported() 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);
JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
status.setRollbackOnly();
}
});
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
verify(tm).resume(tx);
}
use of javax.transaction.UserTransaction in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithSystemExceptionOnBegin.
@Test
public void jtaTransactionManagerWithSystemExceptionOnBegin() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);
willThrow(new SystemException("system exception")).given(ut).begin();
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 CannotCreateTransactionException");
} catch (CannotCreateTransactionException ex) {
// expected
}
}
Aggregations