public void testRollingBackSynchronization() throws Exception {
Thread.currentThread().setName("testRollingBackSynchronization");
if (log.isDebugEnabled()) { log.debug("*** getting TM"); }
final BitronixTransactionManager tm = TransactionManagerServices.getTransactionManager();
if (log.isDebugEnabled()) { log.debug("*** before begin"); }
tm.begin();
if (log.isDebugEnabled()) { log.debug("*** after begin"); }
tm.getTransaction().registerSynchronization(new Synchronization() {
public void beforeCompletion() {
try {
if (log.isDebugEnabled()) { log.debug("**** before setRollbackOnly"); }
tm.setRollbackOnly();
if (log.isDebugEnabled()) { log.debug("**** after setRollbackOnly"); }
} catch (SystemException ex) {
throw new RuntimeException("could not setRollbackOnly", ex);
}
}
public void afterCompletion(int status) {
}
});
if (log.isDebugEnabled()) { log.debug("*** after registerSynchronization"); }
if (log.isDebugEnabled()) { log.debug("*** getting connection from DS1"); }
Connection connection1 = poolingDataSource1.getConnection();
connection1.createStatement();
if (log.isDebugEnabled()) { log.debug("*** getting connection from DS2"); }
Connection connection2 = poolingDataSource2.getConnection();
connection2.createStatement();
if (log.isDebugEnabled()) { log.debug("*** closing connection 1"); }
connection1.close();
if (log.isDebugEnabled()) { log.debug("*** closing connection 2"); }
connection2.close();
if (log.isDebugEnabled()) { log.debug("*** committing"); }
try {
tm.commit();
fail("transaction should not have been able to commit as it has been marked as rollback only");
} catch (RollbackException ex) {
assertEquals("transaction was marked as rollback only and has been rolled back", ex.getMessage());
}
if (log.isDebugEnabled()) { log.debug("*** TX is done"); }