String testName = getTestName();
// Setup the JtaTransactionmanager
Environment env = createEnvironment(context);
Object tm = env.get( EnvironmentName.TRANSACTION_MANAGER );
TransactionManager txm = new JtaTransactionManager( env.get( EnvironmentName.TRANSACTION ),
env.get( EnvironmentName.TRANSACTION_SYNCHRONIZATION_REGISTRY ),
tm );
// Create linked transactionTestObjects
TransactionTestObject mainObject = new TransactionTestObject();
mainObject.setName("main" + testName);
TransactionTestObject subObject = new TransactionTestObject();
subObject.setName("sub" + testName);
mainObject.setSubObject(subObject);
// Commit the mainObject after "commiting" the subObject
EntityManager em = emf.createEntityManager();
try {
// Begin the real trasnaction
boolean txOwner = txm.begin();
// Do the "sub" transaction
// - the txm doesn't really commit,
// because we keep track of who's the tx owner.
boolean notTxOwner = txm.begin();
em.persist(mainObject);
txm.commit(notTxOwner);
// Finish the transaction off
em.persist(subObject);
txm.commit(txOwner);
}
catch( Throwable t ) {
fail( "No exception should have been thrown: " + t.getMessage() );
}
}