Search in sources :

Example 16 with HeuristicRollbackException

use of javax.transaction.HeuristicRollbackException 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 17 with HeuristicRollbackException

use of javax.transaction.HeuristicRollbackException in project tests by datanucleus.

the class GeneralTest method testFailedCommit.

/**
 * Test expected behaviour upon failed commit with JTA
 * (copied from test.jdo.general/org.datanucleus.tests.TransactionTest.java)
 * @throws NamingException
 * @throws SystemException
 * @throws NotSupportedException
 * @throws HeuristicRollbackException
 * @throws HeuristicMixedException
 * @throws RollbackException
 * @throws IllegalStateException
 * @throws SecurityException
 */
public void testFailedCommit() throws NamingException, NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        tx.setOptimistic(true);
        UserTransaction ut = getUserTransaction();
        LoginAccount acct = new LoginAccount("Fred", "Flintstone", "fred", "yabbadabbadoo");
        Login login = acct.getLogin();
        try {
            ut.begin();
            pm.makePersistent(acct);
            ut.commit();
            // provoke FK violation
            ut.begin();
            pm.deletePersistent(login);
            boolean exceptionCaught = false;
            try {
                ut.commit();
                assertTrue("Should have caught exception during commit due to FK violation", false);
            }// e.g. JBoss wraps the NucleusDataStoreException
             catch (Exception e) {
                // Expected
                if (isRollbackDueToDatastoreException(e)) {
                    exceptionCaught = true;
                }
            }
            assertTrue("No exception was thrown during commit so couldnt test autoRollback", exceptionCaught);
            // receiving a RollbackException in commmit() by definition means the UT was rolled back
            if (ut.getStatus() == Status.STATUS_NO_TRANSACTION && pm.currentTransaction().isActive() && ut.getClass().getName().startsWith("org.objectweb.jotm")) {
                // see http://www.jpox.org/servlet/jira/browse/NUCCORE-224
                fail("JOTM bug: when an exception is thrown during Synchronization.beforeCompletion(), the UserTransaction's status is " + "STATUS_NO_TRANSCTION, but there was no callback to Synchronization.afterCompletion()");
            }
            assertFalse(pm.currentTransaction().isActive());
            // now verify that we can still commit, i.e. tx was rolled back properly and is not active anymore
            ut.begin();
            LoginAccount acct2 = new LoginAccount("Wilma", "Flintstone", "wilma", "pebbles");
            pm.makePersistent(acct2);
            ut.commit();
            assertFalse(pm.currentTransaction().isActive());
        } finally {
            try {
                if (ut.getStatus() != Status.STATUS_NO_TRANSACTION) {
                    ut.rollback();
                }
                pm.close();
            } catch (Exception e) {
                // eat exception so test will fail with the underlying exception
                TestHelper.LOG.error("failure during finally block", e);
            }
        }
    } finally {
        try {
            clean(LoginAccount.class);
        } catch (Exception e) {
            // eat exception so test will fail with the underlying exception
            TestHelper.LOG.error("clean(LoginAccount.class) failed", e);
        }
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) UserTransaction(javax.transaction.UserTransaction) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) Login(org.jpox.samples.one_one.unidir.Login) 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 18 with HeuristicRollbackException

use of javax.transaction.HeuristicRollbackException in project mule by mulesoft.

the class XaTransaction method doCommit.

protected synchronized void doCommit() throws TransactionException {
    try {
        /*
       * JTA spec quotes (parts highlighted by AP), the same applies to both TransactionManager and UserTransaction:
       * 
       * 3.2.2 Completing a Transaction The TransactionManager.commit method completes the transaction currently associated with
       * the calling thread.
       ****
       * 
       * After the commit method returns, the calling thread is not associated with a transaction.
       ****
       * 
       * If the commit method is called when the thread is not associated with any transaction context, the TM throws an
       * exception. In some implementations, the commit operation is restricted to the transaction originator only. If the calling
       * thread is not allowed to commit the transaction, the TM throws an exception. The TransactionManager.rollback method rolls
       * back the transaction associated with the current thread.
       ****
       * After the rollback method completes, the thread is associated with no transaction.
       ****
       * 
       * And the following block about Transaction (note there's no thread-tx disassociation clause)
       * 
       * 3.3.3 Transaction Completion The Transaction.commit and Transaction.rollback methods allow the target object to be
       * comitted or rolled back. The calling thread is not required to have the same transaction associated with the thread. If
       * the calling thread is not allowed to commit the transaction, the transaction manager throws an exception.
       * 
       * 
       * So what it meant was that one can't use Transaction.commit()/rollback(), as it doesn't properly disassociate the thread
       * of execution from the current transaction. There's no JTA API-way to do that after the call, so the thread's transaction
       * is subject to manual recovery process. Instead TransactionManager or UserTransaction must be used.
       */
        delistResources();
        txManager.commit();
    } catch (RollbackException | HeuristicRollbackException e) {
        throw new TransactionRollbackException(CoreMessages.transactionMarkedForRollback(), e);
    } catch (Exception e) {
        throw new IllegalTransactionStateException(CoreMessages.transactionCommitFailed(), e);
    } finally {
        /*
       * MUST nullify XA ref here, otherwise Transaction.getStatus() doesn't match javax.transaction.Transaction.getStatus(). Must
       * return STATUS_NO_TRANSACTION and not STATUS_COMMITTED.
       * 
       * TransactionCoordination unbinds the association immediately on this method's exit.
       */
        this.transaction = null;
        closeResources();
    }
}
Also used : HeuristicRollbackException(javax.transaction.HeuristicRollbackException) IllegalTransactionStateException(org.mule.runtime.core.privileged.transaction.xa.IllegalTransactionStateException) TransactionRollbackException(org.mule.runtime.core.api.transaction.TransactionRollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) TransactionRollbackException(org.mule.runtime.core.api.transaction.TransactionRollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) InvalidTransactionException(javax.transaction.InvalidTransactionException) TransactionException(org.mule.runtime.api.tx.TransactionException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) TransactionStatusException(org.mule.runtime.core.api.transaction.TransactionStatusException) TransactionRollbackException(org.mule.runtime.core.api.transaction.TransactionRollbackException) XAException(javax.transaction.xa.XAException) IllegalTransactionStateException(org.mule.runtime.core.privileged.transaction.xa.IllegalTransactionStateException)

Example 19 with HeuristicRollbackException

use of javax.transaction.HeuristicRollbackException in project requery by requery.

the class ManagedTransaction method commit.

@Override
public void commit() {
    if (initiatedTransaction) {
        try {
            transactionListener.beforeCommit(entities.types());
            getUserTransaction().commit();
            transactionListener.afterCommit(entities.types());
        } catch (RollbackException | SystemException | HeuristicMixedException | HeuristicRollbackException e) {
            throw new TransactionException(e);
        }
    }
    try {
        entities.clear();
    } finally {
        close();
    }
}
Also used : HeuristicRollbackException(javax.transaction.HeuristicRollbackException) TransactionException(io.requery.TransactionException) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException)

Example 20 with HeuristicRollbackException

use of javax.transaction.HeuristicRollbackException in project jackrabbit by apache.

the class UserTransactionImpl method commit.

/**
 * @see javax.transaction.UserTransaction#commit
 */
public void commit() throws HeuristicMixedException, HeuristicRollbackException, IllegalStateException, RollbackException, SecurityException, SystemException {
    if (status != Status.STATUS_ACTIVE) {
        throw new IllegalStateException("Transaction not active");
    }
    try {
        for (Iterator it = xaResources.keySet().iterator(); it.hasNext(); ) {
            XAResource resource = (XAResource) it.next();
            XidImpl xid = (XidImpl) xaResources.get(resource);
            resource.end(xid, XAResource.TMSUCCESS);
        }
        status = Status.STATUS_PREPARING;
        for (Iterator it = xaResources.keySet().iterator(); it.hasNext(); ) {
            XAResource resource = (XAResource) it.next();
            XidImpl xid = (XidImpl) xaResources.get(resource);
            resource.prepare(xid);
        }
        status = Status.STATUS_PREPARED;
        status = Status.STATUS_COMMITTING;
        if (distributedThreadAccess) {
            Thread distributedThread = new Thread() {

                public void run() {
                    try {
                        for (Iterator it = xaResources.keySet().iterator(); it.hasNext(); ) {
                            XAResource resource = (XAResource) it.next();
                            XidImpl xid = (XidImpl) xaResources.get(resource);
                            resource.commit(xid, false);
                        }
                    } catch (Exception e) {
                        throw new RuntimeException(e.getMessage());
                    }
                }
            };
            distributedThread.start();
            distributedThread.join(1000);
            if (distributedThread.isAlive()) {
                throw new SystemException("Commit from different thread but same XID must not block");
            }
        } else {
            for (Iterator it = xaResources.keySet().iterator(); it.hasNext(); ) {
                XAResource resource = (XAResource) it.next();
                XidImpl xid = (XidImpl) xaResources.get(resource);
                resource.commit(xid, false);
            }
        }
        status = Status.STATUS_COMMITTED;
    } catch (XAException e) {
        if (e.errorCode >= XAException.XA_RBBASE && e.errorCode <= XAException.XA_RBEND) {
            RollbackException re = new RollbackException("Transaction rolled back: XA_ERR=" + e.errorCode);
            re.initCause(e.getCause());
            throw re;
        } else {
            SystemException se = new SystemException("Unable to commit transaction: XA_ERR=" + e.errorCode);
            se.initCause(e.getCause());
            throw se;
        }
    } catch (InterruptedException e) {
        throw new SystemException("Thread.join() interrupted");
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) XAException(javax.transaction.xa.XAException) SystemException(javax.transaction.SystemException) Iterator(java.util.Iterator) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) XAException(javax.transaction.xa.XAException) HeuristicMixedException(javax.transaction.HeuristicMixedException)

Aggregations

HeuristicRollbackException (javax.transaction.HeuristicRollbackException)40 HeuristicMixedException (javax.transaction.HeuristicMixedException)32 RollbackException (javax.transaction.RollbackException)32 SystemException (javax.transaction.SystemException)30 NotSupportedException (javax.transaction.NotSupportedException)17 XAException (javax.transaction.xa.XAException)9 TransactionManager (javax.transaction.TransactionManager)7 IOException (java.io.IOException)6 SQLException (java.sql.SQLException)6 HeuristicCommitException (javax.transaction.HeuristicCommitException)6 Transaction (javax.transaction.Transaction)6 Test (org.junit.Test)6 UnexpectedConditionException (com.arjuna.ats.jta.exceptions.UnexpectedConditionException)5 Connection (java.sql.Connection)5 UserTransaction (javax.transaction.UserTransaction)5 XAResource (javax.transaction.xa.XAResource)5 PreparedStatement (java.sql.PreparedStatement)4 Synchronization (javax.transaction.Synchronization)4 Statement (java.sql.Statement)3 NamingException (javax.naming.NamingException)3