Search in sources :

Example 26 with HeuristicRollbackException

use of javax.transaction.HeuristicRollbackException in project narayana by jbosstm.

the class EnlistResourceDuringCommit method testEnlistResourceDuringBeforeCompletion.

@Test
public void testEnlistResourceDuringBeforeCompletion() throws IllegalStateException, RollbackException, SystemException, SecurityException, HeuristicMixedException, HeuristicRollbackException {
    final TransactionImple tx = new TransactionImple(0);
    tx.registerSynchronization(new Synchronization() {

        @Override
        public void beforeCompletion() {
            System.out.println(new Throwable().getStackTrace()[0].getMethodName());
            try {
                tx.enlistResource(new XAResource() {

                    @Override
                    public void start(Xid arg0, int arg1) throws XAException {
                        System.out.println(new Throwable().getStackTrace()[0].getMethodName());
                    }

                    @Override
                    public boolean setTransactionTimeout(int arg0) throws XAException {
                        System.out.println(new Throwable().getStackTrace()[0].getMethodName());
                        return false;
                    }

                    @Override
                    public void rollback(Xid arg0) throws XAException {
                        System.out.println(new Throwable().getStackTrace()[0].getMethodName());
                    }

                    @Override
                    public Xid[] recover(int arg0) throws XAException {
                        System.out.println(new Throwable().getStackTrace()[0].getMethodName());
                        return null;
                    }

                    @Override
                    public int prepare(Xid arg0) throws XAException {
                        System.out.println(new Throwable().getStackTrace()[0].getMethodName());
                        return 0;
                    }

                    @Override
                    public boolean isSameRM(XAResource arg0) throws XAException {
                        System.out.println(new Throwable().getStackTrace()[0].getMethodName());
                        return false;
                    }

                    @Override
                    public int getTransactionTimeout() throws XAException {
                        System.out.println(new Throwable().getStackTrace()[0].getMethodName());
                        return 0;
                    }

                    @Override
                    public void forget(Xid arg0) throws XAException {
                        System.out.println(new Throwable().getStackTrace()[0].getMethodName());
                    }

                    @Override
                    public void end(Xid arg0, int arg1) throws XAException {
                        System.out.println(new Throwable().getStackTrace()[0].getMethodName());
                    }

                    @Override
                    public void commit(Xid arg0, boolean arg1) throws XAException {
                        System.out.println(new Throwable().getStackTrace()[0].getMethodName());
                    }
                });
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (RollbackException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SystemException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        @Override
        public void afterCompletion(int status) {
            System.out.println(new Throwable().getStackTrace()[0].getMethodName());
        }
    });
    tx.commit();
}
Also used : XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) SystemException(javax.transaction.SystemException) TransactionImple(com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple) Synchronization(javax.transaction.Synchronization) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) Test(org.junit.Test)

Example 27 with HeuristicRollbackException

use of javax.transaction.HeuristicRollbackException in project narayana by jbosstm.

the class JTATest method testHeuristicRollbackSuppressedException.

@Test
public void testHeuristicRollbackSuppressedException() throws NotSupportedException, SystemException, IllegalStateException, RollbackException, SecurityException, HeuristicMixedException, HeuristicRollbackException {
    javax.transaction.TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
    tm.begin();
    javax.transaction.Transaction theTransaction = tm.getTransaction();
    assertTrue(theTransaction.enlistResource(new XAResource() {

        @Override
        public void start(Xid xid, int flags) throws XAException {
        }

        @Override
        public void end(Xid xid, int flags) throws XAException {
        }

        @Override
        public int prepare(Xid xid) throws XAException {
            return 0;
        }

        @Override
        public void commit(Xid xid, boolean onePhase) throws XAException {
            exception = new XAException(XAException.XA_HEURRB);
            throw exception;
        }

        @Override
        public void rollback(Xid xid) throws XAException {
        }

        @Override
        public void forget(Xid xid) throws XAException {
        }

        @Override
        public Xid[] recover(int flag) throws XAException {
            return null;
        }

        @Override
        public boolean isSameRM(XAResource xaRes) throws XAException {
            return false;
        }

        @Override
        public int getTransactionTimeout() throws XAException {
            return 0;
        }

        @Override
        public boolean setTransactionTimeout(int seconds) throws XAException {
            return false;
        }
    }));
    assertTrue(theTransaction.enlistResource(new XAResource() {

        @Override
        public void start(Xid xid, int flags) throws XAException {
        }

        @Override
        public void end(Xid xid, int flags) throws XAException {
        }

        @Override
        public int prepare(Xid xid) throws XAException {
            return 0;
        }

        @Override
        public void commit(Xid xid, boolean onePhase) throws XAException {
        }

        @Override
        public void rollback(Xid xid) throws XAException {
        }

        @Override
        public void forget(Xid xid) throws XAException {
        }

        @Override
        public Xid[] recover(int flag) throws XAException {
            return null;
        }

        @Override
        public boolean isSameRM(XAResource xaRes) throws XAException {
            return false;
        }

        @Override
        public int getTransactionTimeout() throws XAException {
            return 0;
        }

        @Override
        public boolean setTransactionTimeout(int seconds) throws XAException {
            return false;
        }
    }));
    try {
        tm.commit();
        fail();
    } catch (RollbackException e) {
        e.printStackTrace();
        assertTrue(e.getSuppressed()[0] == exception);
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) XAException(javax.transaction.xa.XAException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) Test(org.junit.Test)

Example 28 with HeuristicRollbackException

use of javax.transaction.HeuristicRollbackException in project narayana by jbosstm.

the class TransactionListenerRegistryTest method test.

@Test
public void test() throws SystemException, NotSupportedException, RollbackException, TransactionTypeNotSupported, InterruptedException, InvalidTransactionException, HeuristicRollbackException, HeuristicMixedException {
    TransactionManager tm = new TransactionManagerDelegate();
    ServerVMClientUserTransaction userTransaction = new ServerVMClientUserTransaction(tm);
    userTransaction.setTransactionTimeout(1);
    for (CompletionType completionType : CompletionType.values()) {
        TransactionListenerRegistry listenerRegistration = (TransactionListenerRegistry) tm;
        userTransaction.begin();
        // The TSR for interposed synchronizations
        final TransactionSynchronizationRegistry tsr = new TransactionSynchronizationRegistryImple();
        final TxListener listener = new TxListener(listenerRegistration);
        if (completionType != CompletionType.CMTSUSPEND) {
            tsr.registerInterposedSynchronization(listener);
        } else {
            tm.getTransaction().registerSynchronization(listener);
        }
        listenerRegistration.addListener(tm.getTransaction(), listener, EnumSet.allOf(EventType.class));
        if (completionType == CompletionType.CMTSUSPEND) {
            Transaction suspended = tm.suspend();
            Thread.sleep(2000);
            assertTrue(listener.shouldDisassoc());
            tm.resume(suspended);
        } else {
            Thread.sleep(2000);
            assertFalse(listener.shouldDisassoc());
        }
        assertTrue(listener.singleCallAC());
        assertTrue(Status.STATUS_ROLLEDBACK == userTransaction.getStatus());
        // https://community.jboss.org/thread/92489
        if (completionType == CompletionType.BMTCOMMIT) {
            try {
                userTransaction.commit();
                fail("Should not have been able to commit");
            } catch (RollbackException e) {
            }
        } else if (completionType == CompletionType.BMTROLLBACK) {
            userTransaction.rollback();
        } else if (completionType == CompletionType.CMT) {
            // This is possible in CMT mode
            // If they did check the status, it is still expected that a CMT
            // calls suspend at least when a tx is marked as completed to
            // clear
            // it from the thread
            tm.suspend();
        }
        assertTrue(listener.singleCallAC());
        if (completionType == CompletionType.CMTSUSPEND) {
            assertFalse(listener.shouldDisassoc());
        } else {
            assertTrue(listener.shouldDisassoc());
        }
        assertTrue(listener.isClosed());
    }
}
Also used : ServerVMClientUserTransaction(org.jboss.tm.usertx.client.ServerVMClientUserTransaction) TransactionSynchronizationRegistryImple(com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple) TransactionManagerDelegate(com.arjuna.ats.jbossatx.jta.TransactionManagerDelegate) Transaction(javax.transaction.Transaction) ServerVMClientUserTransaction(org.jboss.tm.usertx.client.ServerVMClientUserTransaction) TransactionManager(javax.transaction.TransactionManager) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) TxListener(com.arjuna.ats.jta.distributed.spi.TxListener) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) Test(org.junit.Test)

Example 29 with HeuristicRollbackException

use of javax.transaction.HeuristicRollbackException in project narayana by jbosstm.

the class WedgedResourceDemonstrator method testWedge.

@Test
public void testWedge() throws InvalidName, SystemException, NotSupportedException, javax.transaction.SystemException, IllegalStateException, RollbackException, SecurityException, HeuristicMixedException, HeuristicRollbackException, InterruptedException {
    String mode = "jts";
    if (mode.equals("jts")) {
        ORB myORB = ORB.getInstance("test");
        RootOA myOA = OA.getRootOA(myORB);
        myORB.initORB(new String[0], null);
        myOA.initOA();
        com.arjuna.ats.internal.jts.ORBManager.setORB(myORB);
        com.arjuna.ats.internal.jts.ORBManager.setPOA(myOA);
        RecoveryManager.manager().initialize();
    }
    TransactionManager transactionManager = mode.equals("jts") ? new com.arjuna.ats.internal.jta.transaction.jts.TransactionManagerImple() : new com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple();
    transactionManager.setTransactionTimeout(2);
    transactionManager.begin();
    transactionManager.getTransaction().enlistResource(new TimeoutOnFirstRollbackResource());
    // Business logic
    Thread.currentThread().sleep(5000);
    try {
        transactionManager.commit();
        fail("Should not have been able to commit");
    } catch (RollbackException e) {
    // This is fine
    } catch (IllegalStateException e) {
    // This is fine
    } finally {
        if (mode.equals("jts")) {
            RecoveryManager.manager().terminate();
            ORB myORB = ORB.getInstance("test");
            RootOA myOA = OA.getRootOA(myORB);
            myOA.destroy();
            myORB.shutdown();
        }
    }
}
Also used : RootOA(com.arjuna.orbportability.RootOA) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) TransactionManager(javax.transaction.TransactionManager) ORB(com.arjuna.orbportability.ORB) Test(org.junit.Test)

Example 30 with HeuristicRollbackException

use of javax.transaction.HeuristicRollbackException in project ovirt-engine by oVirt.

the class TransactionSupport method executeInNewTransaction.

/**
 * Forces "REQUIRES_NEW" and executes given code in that scope
 */
public static <T> T executeInNewTransaction(TransactionMethod<T> code) {
    T result = null;
    Transaction transaction = null;
    try {
        TransactionManager tm = findTransactionManager();
        // suspend existing if exists
        transaction = tm.getTransaction();
        if (transaction != null) {
            transaction = tm.suspend();
        }
        // start new transaction
        tm.begin();
        Transaction newTransaction = tm.getTransaction();
        // run the code
        try {
            result = code.runInTransaction();
        } catch (RuntimeException rte) {
            tm.rollback();
            log.info("transaction rolled back");
            throw rte;
        } catch (Exception e) {
            // code failed need to rollback
            tm.rollback();
            log.info("transaction rolled back");
            log.error("executeInNewTransaction - Wrapping Exception: {} with RunTimeException", e.getClass().getName());
            throw new RuntimeException("Failed executing code", e);
        }
        // commit or rollback according to state
        if (needToRollback(newTransaction.getStatus())) {
            tm.rollback();
        } else {
            tm.commit();
        }
    } catch (SystemException | NotSupportedException | HeuristicRollbackException | HeuristicMixedException | RollbackException | IllegalStateException | SecurityException e) {
        throw new RuntimeException("Failed managing transaction", e);
    } finally {
        // check if we need to resume previous transaction
        if (transaction != null) {
            resume(transaction);
        }
    }
    // and we are done...
    return result;
}
Also used : HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) TransactionRolledbackLocalException(javax.ejb.TransactionRolledbackLocalException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) HeuristicMixedException(javax.transaction.HeuristicMixedException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) TransactionManager(javax.transaction.TransactionManager) HeuristicMixedException(javax.transaction.HeuristicMixedException) NotSupportedException(javax.transaction.NotSupportedException)

Aggregations

HeuristicRollbackException (javax.transaction.HeuristicRollbackException)37 HeuristicMixedException (javax.transaction.HeuristicMixedException)29 RollbackException (javax.transaction.RollbackException)29 SystemException (javax.transaction.SystemException)27 NotSupportedException (javax.transaction.NotSupportedException)14 XAException (javax.transaction.xa.XAException)9 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 TransactionManager (javax.transaction.TransactionManager)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