The TransactionTestCase gets the UserTransaction service and registers a transactional user object (i.e. one that implements Synchronization ) with the TransactionManager service. It then verifies that modifications on the user object are transactional.
This functionality is only available in the context of AS7.
Transactional txObj = new Transactional();
ServiceReference userTxRef = context.getServiceReference(UserTransaction.class.getName());
assertNotNull("UserTransaction service not null", userTxRef);
UserTransaction userTx = (UserTransaction)context.getService(userTxRef);
assertNotNull("UserTransaction not null", userTx);
userTx.begin();
try
{
ServiceReference tmRef = context.getServiceReference(TransactionManager.class.getName());
assertNotNull("TransactionManager service not null", tmRef);
TransactionManager tm = (TransactionManager)context.getService(tmRef);
assertNotNull("TransactionManager not null", tm);
Transaction tx = tm.getTransaction();
assertNotNull("Transaction not null", tx);
tx.registerSynchronization(txObj);
txObj.setMessage("Donate $1.000.000");
assertNull("Uncommited message null", txObj.getMessage());
userTx.commit();
}
catch (Exception e)
{
userTx.setRollbackOnly();
}
assertEquals("Donate $1.000.000", txObj.getMessage());class Transactional implements Synchronization
{
public void afterCompletion(int status)
{
if (status == Status.STATUS_COMMITTED)
message = volatileMessage;
}
...
}