Search in sources :

Example 1 with PersistenceManagerDisposer

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);
}
Also used : PersistenceManagerDisposer(org.datanucleus.tests.jta.util.PersistenceManagerDisposer) UserTransaction(javax.transaction.UserTransaction) Synchronization(javax.transaction.Synchronization) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) NamingException(javax.naming.NamingException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) HeuristicMixedException(javax.transaction.HeuristicMixedException)

Example 2 with PersistenceManagerDisposer

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);
    }
}
Also used : PersistenceManagerDisposer(org.datanucleus.tests.jta.util.PersistenceManagerDisposer) UserTransaction(javax.transaction.UserTransaction) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) Account(org.jpox.samples.models.company.Account) PersistenceManager(javax.jdo.PersistenceManager)

Aggregations

UserTransaction (javax.transaction.UserTransaction)2 PersistenceManagerDisposer (org.datanucleus.tests.jta.util.PersistenceManagerDisposer)2 PersistenceManager (javax.jdo.PersistenceManager)1 NamingException (javax.naming.NamingException)1 HeuristicMixedException (javax.transaction.HeuristicMixedException)1 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)1 NotSupportedException (javax.transaction.NotSupportedException)1 RollbackException (javax.transaction.RollbackException)1 Synchronization (javax.transaction.Synchronization)1 SystemException (javax.transaction.SystemException)1 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)1 Account (org.jpox.samples.models.company.Account)1 LoginAccount (org.jpox.samples.one_one.unidir.LoginAccount)1