use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.
the class TransactionSupportTests method transactionTemplateWithRollbackException.
@SuppressWarnings("serial")
@Test
public void transactionTemplateWithRollbackException() {
final TransactionSystemException tex = new TransactionSystemException("system exception");
TestTransactionManager tm = new TestTransactionManager(false, true) {
@Override
protected void doRollback(DefaultTransactionStatus status) {
super.doRollback(status);
throw tex;
}
};
TransactionTemplate template = new TransactionTemplate(tm);
final RuntimeException ex = new RuntimeException("Some application exception");
try {
template.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
throw ex;
}
});
fail("Should have propagated RuntimeException");
} catch (RuntimeException caught) {
// expected
assertTrue("Correct exception", caught == tex);
assertTrue("triggered begin", tm.begin);
assertTrue("no commit", !tm.commit);
assertTrue("triggered rollback", tm.rollback);
assertTrue("no rollbackOnly", !tm.rollbackOnly);
}
}
use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.
the class TransactionSupportTests method transactionTemplateWithCallbackPreference.
@Test
public void transactionTemplateWithCallbackPreference() {
MockCallbackPreferringTransactionManager ptm = new MockCallbackPreferringTransactionManager();
TransactionTemplate template = new TransactionTemplate(ptm);
template.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
}
});
assertSame(template, ptm.getDefinition());
assertFalse(ptm.getStatus().isRollbackOnly());
}
use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.
the class TransactionSupportTests method transactionTemplateWithError.
@Test
public void transactionTemplateWithError() {
TestTransactionManager tm = new TestTransactionManager(false, true);
TransactionTemplate template = new TransactionTemplate(tm);
try {
template.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
throw new Error("Some application error");
}
});
fail("Should have propagated Error");
} catch (Error err) {
// expected
assertTrue("triggered begin", tm.begin);
assertTrue("no commit", !tm.commit);
assertTrue("triggered rollback", tm.rollback);
assertTrue("no rollbackOnly", !tm.rollbackOnly);
}
}
use of org.springframework.transaction.support.TransactionCallbackWithoutResult 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 org.springframework.transaction.support.TransactionCallbackWithoutResult 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
}
}
Aggregations