InitialContext ctx = new InitialContext();
TxSessionLocal tester = (TxSessionLocal) ctx.lookup("TxSessionBean/local");
TxSessionLocal asynchTester = (TxSessionLocal)((JBossProxy)tester).getAsynchronousProxy();
AsynchProvider ap = (AsynchProvider) asynchTester;
TransactionManager tx = (TransactionManager) ctx.lookup(TransactionManagerService.JNDI_NAME);
//Add some entries in different threads and commit
tx.begin();
tester.createFruit("apple", false);
tester.createFruit("pear", false);
tester.createFruit("tomato", false);
asynchTester.createVeg("Potato", false);
waitForProvider(ap);
asynchTester.createVeg("Turnip", false);
waitForProvider(ap);
asynchTester.createVeg("Carrot", false);
waitForProvider(ap);
tx.commit();
tx.begin();
Collection entries = tester.getEntries();
tx.commit();
if (entries.size() != 6) throw new RuntimeException("Wrong number of entries, should have been 6, have: " + entries.size());
//Cleanup synchronously
tx.begin();
tester.cleanAll();
tx.commit();
tx.begin();
entries = tester.getEntries();
tx.commit();
if (entries.size() != 0) throw new RuntimeException("Wrong number of entries, should have been 0, have: " + entries.size());
//Add some entries in different threads and rollback
tx.begin();
tester.createFruit("apple", false);
tester.createFruit("pear", false);
tester.createFruit("tomato", false);
asynchTester.createVeg("Potato", false);
waitForProvider(ap);
asynchTester.createVeg("Turnip", false);
waitForProvider(ap);
asynchTester.createVeg("Carrot", false);
waitForProvider(ap);
tx.rollback();
tx.begin();
entries = tester.getEntries();
tx.commit();
if (entries.size() != 0) throw new RuntimeException("Wrong number of entries, should have been 0, have: " + entries.size());
//Add some entries in different threads and rollback from within Tx
tx.begin();
tester.createFruit("apple", false);
tester.createFruit("pear", false);
tester.createFruit("tomato", true);
asynchTester.createVeg("Potato", false);
waitForProvider(ap);
asynchTester.createVeg("Turnip", false);
waitForProvider(ap);
asynchTester.createVeg("Carrot", true);
waitForProvider(ap);
boolean rollbackException = false;
try
{
tx.commit();
}
catch(RollbackException e)
{
rollbackException = true;
}
if (!rollbackException) throw new RuntimeException("RollbackException not picked up");
tx.begin();
entries = tester.getEntries();
tx.commit();
if (entries.size() != 0) throw new RuntimeException("Wrong number of entries, should have been 0, have: " + entries.size());