use of org.datanucleus.tests.jta.util.PersistenceManagerDisposer in project tests by datanucleus.
the class GeneralTest method testExceptionDuringBeforeCompletion.
/**
* Verify that any exceptions thrown during JTATransactionImpl.beforeCompletion() are propagated properly, either
* caused by flushing, or by invoking user code in a user-provided Synchronization.beforeCompletion() callback.
*/
public void testExceptionDuringBeforeCompletion() throws NotSupportedException, SystemException, NamingException, RollbackException, HeuristicMixedException, HeuristicRollbackException {
pm = pmf.getPersistenceManager();
new PersistenceManagerDisposer(pm);
final String msg = "This was expected.";
pm.currentTransaction().setSynchronization(new Synchronization() {
public void beforeCompletion() {
// throw the exception that we want to see during ut.commit()
throw new RuntimeException(msg);
}
public void afterCompletion(int arg0) {
}
});
boolean caughtExpectedException = false;
UserTransaction ut = getUserTransaction();
ut.begin();
try {
// access the currentTransaction so it joins UserTransaction
pm.currentTransaction().isActive();
ut.commit();
} catch (Exception e) {
if (e instanceof RollbackException) {
// JBoss 4.2.3 and JOTM 2.1.4 throw a RollbackException that has no clue about our original exception
caughtExpectedException = true;
} else if (cargoContainerId != null && cargoContainerId.equals("jboss4x")) {
if (!e.getMessage().contains(msg)) {
e.printStackTrace();
throw new RuntimeException("With jboss4.0.3 we expect the exception caught to contain the message of our thrown exception", e);
} else {
caughtExpectedException = true;
}
}
}
assertTrue("Exception thrown during beforeCompletion() wasn't propagated properly", caughtExpectedException);
}
use of org.datanucleus.tests.jta.util.PersistenceManagerDisposer in project tests by datanucleus.
the class GeneralTest method testCloseOnTxnCompletion.
public void testCloseOnTxnCompletion() throws Exception {
try {
PersistenceManager pm = null;
pm = pmf.getPersistenceManager();
new PersistenceManagerDisposer(pm);
pm.currentTransaction().begin();
Account accnt = new Account();
accnt.setUsername("jpox");
pm.makePersistent(accnt);
Object oid = pm.getObjectId(accnt);
pm.currentTransaction().commit();
assertTrue("The PersistenceManager is still open", pm.isClosed());
pm = pmf.getPersistenceManager();
new PersistenceManagerDisposer(pm);
pm.currentTransaction().begin();
accnt = (Account) pm.getObjectById(oid);
pm.deletePersistent(accnt);
pm.currentTransaction().commit();
assertTrue("The PersistenceManager is still open", pm.isClosed());
pm = pmf.getPersistenceManager();
new PersistenceManagerDisposer(pm);
pm.currentTransaction().begin();
accnt = new Account();
accnt.setUsername("jpox");
pm.makePersistent(accnt);
pm.currentTransaction().rollback();
assertTrue("The PersistenceManager is still open", pm.isClosed());
UserTransaction ut = getUserTransaction();
ut.begin();
pm = pmf.getPersistenceManager();
new PersistenceManagerDisposer(pm);
accnt = new Account();
accnt.setUsername("jpox");
pm.makePersistent(accnt);
oid = pm.getObjectId(accnt);
ut.commit();
assertTrue("The PersistenceManager is still open", pm.isClosed());
ut.begin();
pm = pmf.getPersistenceManager();
new PersistenceManagerDisposer(pm);
accnt = (Account) pm.getObjectById(oid);
pm.deletePersistent(accnt);
ut.commit();
assertTrue("The PersistenceManager is still open", pm.isClosed());
ut.begin();
pm = pmf.getPersistenceManager();
new PersistenceManagerDisposer(pm);
accnt = new Account();
accnt.setUsername("jpox");
pm.makePersistent(accnt);
oid = pm.getObjectId(accnt);
ut.rollback();
assertTrue("The PersistenceManager is still open", pm.isClosed());
} finally {
clean(Account.class);
}
}
Aggregations