Search in sources :

Example 31 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 32 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)

Example 33 with HeuristicRollbackException

use of javax.transaction.HeuristicRollbackException in project Payara by payara.

the class JavaEETransactionImpl method commit.

@Override
public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException {
    checkTransationActive();
    // If this transaction is set for timeout, cancel it as it is in the commit state
    if (isTimerTask) {
        cancelTimerTask();
    }
    // END local transaction timeout
    if (_logger.isLoggable(Level.FINE)) {
        _logger.log(Level.FINE, "--In JavaEETransactionImpl.commit, jtsTx=" + jtsTx + " nonXAResource=" + nonXAResource);
    }
    commitStarted = true;
    boolean success = false;
    if (jtsTx != null) {
        try {
            jtsTx.commit();
            success = true;
        } catch (HeuristicMixedException e) {
            success = true;
            throw e;
        } finally {
            ((JavaEETransactionManagerSimplified) javaEETM).monitorTxCompleted(this, success);
            ((JavaEETransactionManagerSimplified) javaEETM).clearThreadTx();
            onTxCompletion(success);
            try {
                localTxStatus = jtsTx.getStatus();
            } catch (Exception e) {
                localTxStatus = Status.STATUS_NO_TRANSACTION;
            }
            jtsTx = null;
        }
    } else {
        // local tx
        Exception caughtException = null;
        try {
            if (timedOut) {
                // rollback nonXA resource
                if (nonXAResource != null) {
                    nonXAResource.getXAResource().rollback(xid);
                }
                localTxStatus = Status.STATUS_ROLLEDBACK;
                throw new RollbackException(sm.getString("enterprise_distributedtx.rollback_timeout"));
            }
            if (isRollbackOnly()) {
                // rollback nonXA resource
                if (nonXAResource != null) {
                    nonXAResource.getXAResource().rollback(xid);
                }
                localTxStatus = Status.STATUS_ROLLEDBACK;
                throw new RollbackException(sm.getString("enterprise_distributedtx.mark_rollback"));
            }
            // call beforeCompletion
            for (int i = 0; i < syncs.size(); i++) {
                try {
                    Synchronization sync = (Synchronization) syncs.elementAt(i);
                    sync.beforeCompletion();
                } catch (RuntimeException ex) {
                    _logger.log(Level.WARNING, "enterprise_distributedtx.before_completion_excep", ex);
                    setRollbackOnly();
                    caughtException = ex;
                    break;
                } catch (Exception ex) {
                    _logger.log(Level.WARNING, "enterprise_distributedtx.before_completion_excep", ex);
                // XXX-V2 no setRollbackOnly() ???
                }
            }
            for (int i = 0; i < interposedSyncs.size(); i++) {
                try {
                    Synchronization sync = (Synchronization) interposedSyncs.elementAt(i);
                    sync.beforeCompletion();
                } catch (RuntimeException ex) {
                    _logger.log(Level.WARNING, "enterprise_distributedtx.before_completion_excep", ex);
                    setRollbackOnly();
                    caughtException = ex;
                    break;
                } catch (Exception ex) {
                    _logger.log(Level.WARNING, "enterprise_distributedtx.before_completion_excep", ex);
                // XXX-V2 no setRollbackOnly() ???
                }
            }
            // calls marked it for rollback.
            if (isRollbackOnly()) {
                // Check if it is a Local Transaction
                RollbackException rbe = null;
                if (jtsTx == null) {
                    if (nonXAResource != null) {
                        nonXAResource.getXAResource().rollback(xid);
                    }
                    localTxStatus = Status.STATUS_ROLLEDBACK;
                    rbe = new RollbackException(sm.getString("enterprise_distributedtx.mark_rollback"));
                // else it is a global transaction
                } else {
                    jtsTx.rollback();
                    localTxStatus = Status.STATUS_ROLLEDBACK;
                    rbe = new RollbackException(sm.getString("enterprise_distributedtx.mark_rollback"));
                }
                // RollbackException doesn't have a constructor that takes a Throwable.
                if (caughtException != null) {
                    rbe.initCause(caughtException);
                }
                throw rbe;
            }
            // beforeCompletions registered the first XA resource.
            if (jtsTx != null) {
                jtsTx.commit();
            // Note: JTS will not call afterCompletions in this case,
            // because no syncs have been registered with JTS.
            // So afterCompletions are called in finally block below.
            } else {
                // do single-phase commit on nonXA resource
                if (nonXAResource != null) {
                    nonXAResource.getXAResource().commit(xid, true);
                }
            }
            // V2-XXX should this be STATUS_NO_TRANSACTION ?
            localTxStatus = Status.STATUS_COMMITTED;
            success = true;
        } catch (RollbackException ex) {
            // V2-XXX is this correct ?
            localTxStatus = Status.STATUS_ROLLEDBACK;
            throw ex;
        } catch (SystemException ex) {
            // localTxStatus = Status.STATUS_ROLLEDBACK; // V2-XXX is this correct ?
            localTxStatus = Status.STATUS_COMMITTING;
            success = true;
            throw ex;
        } catch (Exception ex) {
            // V2-XXX is this correct ?
            localTxStatus = Status.STATUS_ROLLEDBACK;
            SystemException exc = new SystemException();
            exc.initCause(ex);
            throw exc;
        } finally {
            ((JavaEETransactionManagerSimplified) javaEETM).monitorTxCompleted(this, success);
            ((JavaEETransactionManagerSimplified) javaEETM).clearThreadTx();
            for (int i = 0; i < interposedSyncs.size(); i++) {
                try {
                    Synchronization sync = (Synchronization) interposedSyncs.elementAt(i);
                    sync.afterCompletion(localTxStatus);
                } catch (Exception ex) {
                    _logger.log(Level.WARNING, "enterprise_distributedtx.after_completion_excep", ex);
                }
            }
            // call afterCompletions
            for (int i = 0; i < syncs.size(); i++) {
                try {
                    Synchronization sync = (Synchronization) syncs.elementAt(i);
                    sync.afterCompletion(localTxStatus);
                } catch (Exception ex) {
                    _logger.log(Level.WARNING, "enterprise_distributedtx.after_completion_excep", ex);
                }
            }
            onTxCompletion(success);
            jtsTx = null;
        }
    }
}
Also used : SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) Synchronization(javax.transaction.Synchronization) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException)

Example 34 with HeuristicRollbackException

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

the class DatabaseTimerPersistence method shouldRun.

@Override
public boolean shouldRun(TimerImpl timer) {
    final ContextTransactionManager tm = ContextTransactionManager.getInstance();
    if (!allowExecution) {
        // timers never execute on this node
        return false;
    }
    String loadTimer = sql.getProperty(UPDATE_RUNNING);
    Connection connection = null;
    PreparedStatement statement = null;
    try {
        tm.begin();
        try {
            connection = dataSource.getConnection();
            statement = connection.prepareStatement(loadTimer);
            statement.setString(1, TimerState.IN_TIMEOUT.name());
            setNodeName(TimerState.IN_TIMEOUT, statement, 2);
            statement.setString(3, timer.getId());
            statement.setString(4, TimerState.IN_TIMEOUT.name());
            statement.setString(5, TimerState.RETRY_TIMEOUT.name());
            if (timer.getNextExpiration() == null) {
                statement.setTimestamp(6, null);
            } else {
                statement.setTimestamp(6, timestamp(timer.getNextExpiration()));
            }
        } catch (SQLException e) {
            try {
                tm.rollback();
            } catch (Exception ee) {
                EjbLogger.EJB3_TIMER_LOGGER.timerUpdateFailedAndRollbackNotPossible(ee);
            }
            // fix for WFLY-10130
            EjbLogger.EJB3_TIMER_LOGGER.exceptionCheckingIfTimerShouldRun(timer, e);
            return false;
        }
        int affected = statement.executeUpdate();
        tm.commit();
        return affected == 1;
    } catch (SQLException | SystemException | SecurityException | IllegalStateException | RollbackException | HeuristicMixedException | HeuristicRollbackException e) {
        // failed to update the DB
        try {
            tm.rollback();
        } catch (IllegalStateException | SecurityException | SystemException rbe) {
            EjbLogger.EJB3_TIMER_LOGGER.timerUpdateFailedAndRollbackNotPossible(rbe);
        }
        EjbLogger.EJB3_TIMER_LOGGER.debugf(e, "Timer %s not running due to exception ", timer);
        return false;
    } catch (NotSupportedException e) {
        // happen from tm.begin, no rollback necessary
        EjbLogger.EJB3_TIMER_LOGGER.timerNotRunning(e, timer);
        return false;
    } finally {
        safeClose(statement);
        safeClose(connection);
    }
}
Also used : ContextTransactionManager(org.wildfly.transaction.client.ContextTransactionManager) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) ParseException(java.text.ParseException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) HeuristicMixedException(javax.transaction.HeuristicMixedException) SQLException(java.sql.SQLException) RollbackException(javax.transaction.RollbackException) StartException(org.jboss.msc.service.StartException) IOException(java.io.IOException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException) NotSupportedException(javax.transaction.NotSupportedException)

Example 35 with HeuristicRollbackException

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

the class BeforeCompletionExceptionDestroysBeanTestCase method testExceptionInBeforeCompletionDestroysBean.

@Test
public void testExceptionInBeforeCompletionDestroysBean() throws NamingException, SystemException, NotSupportedException, HeuristicRollbackException, HeuristicMixedException {
    final UserTransaction userTransaction = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    final BeforeCompletionSFSB bean = (BeforeCompletionSFSB) new InitialContext().lookup("java:module/" + BeforeCompletionSFSB.class.getSimpleName());
    // begin a transaction
    userTransaction.begin();
    bean.enlist();
    // commit, this should destroy the bean, as it's @BeforeCompletion method will throw an exception
    try {
        userTransaction.commit();
    } catch (RollbackException expected) {
    }
    try {
        userTransaction.begin();
        bean.enlist();
        throw new RuntimeException("Expected SFSB to be destroyed");
    } catch (NoSuchEJBException expected) {
    } finally {
        userTransaction.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) NoSuchEJBException(javax.ejb.NoSuchEJBException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

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