Search in sources :

Example 6 with HeuristicRollbackException

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

the class TransactionManagerImpl method commit.

/**
   * Complete the transaction associated with the current thread by calling the
   * GlobalTransaction.commit(). When this method completes, the thread is no longer associated with
   * a transaction.
   * 
   * @throws RollbackException - Thrown to indicate that the transaction has been rolled back rather
   *         than committed.
   * @throws HeuristicMixedException - Thrown to indicate that a heuristic decision was made and
   *         that some relevant updates have been committed while others have been rolled back.
   * @throws HeuristicRollbackException - Thrown to indicate that a heuristic decision was made and
   *         that all relevant updates have been rolled back.
   * @throws java.lang.SecurityException - Thrown to indicate that the thread is not allowed to
   *         commit the transaction.
   * @throws java.lang.IllegalStateException - Thrown if the current thread is not associated with a
   *         transaction.
   * @throws SystemException - Thrown if the transaction manager encounters an unexpected error
   *         condition.
   * 
   * @see javax.transaction.TransactionManager#commit()
   */
public void commit() throws HeuristicRollbackException, RollbackException, HeuristicMixedException, SystemException {
    if (!isActive) {
        throw new SystemException(LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGER_INVALID.toLocalizedString());
    }
    int cozOfException = -1;
    Transaction transactionImpl = getTransaction();
    if (transactionImpl == null) {
        String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_COMMIT_TRANSACTION_IS_NULL_CANNOT_COMMIT_A_NULL_TRANSACTION.toLocalizedString();
        LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
        if (VERBOSE)
            writer.fine(exception);
        throw new IllegalStateException(exception);
    }
    GlobalTransaction gtx = getGlobalTransaction(transactionImpl);
    if (gtx == null) {
        String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_COMMIT_GLOBAL_TRANSACTION_IS_NULL_CANNOT_COMMIT_A_NULL_GLOBAL_TRANSACTION.toLocalizedString();
        LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
        if (VERBOSE)
            writer.fine(exception);
        throw new SystemException(exception);
    }
    boolean isCommit = false;
    // ensure only one thread can commit. Use a synchronized block
    // Asif
    int status = -1;
    if (((status = gtx.getStatus()) == Status.STATUS_ACTIVE) || status == Status.STATUS_MARKED_ROLLBACK) {
        synchronized (gtx) {
            if ((status = gtx.getStatus()) == Status.STATUS_ACTIVE) {
                gtx.setStatus(Status.STATUS_COMMITTING);
                isCommit = true;
            } else if (status == Status.STATUS_MARKED_ROLLBACK) {
                gtx.setStatus(Status.STATUS_ROLLING_BACK);
                cozOfException = MARKED_ROLLBACK;
            } else {
                String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_COMMIT_TRANSACTION_NOT_ACTIVE_CANNOT_BE_COMMITTED_TRANSACTION_STATUS_0.toLocalizedString(Integer.valueOf(status));
                LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
                if (VERBOSE)
                    writer.fine(exception);
                throw new IllegalStateException(exception);
            }
        }
    } else {
        String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_COMMIT_TRANSACTION_IS_NOT_ACTIVE_AND_CANNOT_BE_COMMITTED.toLocalizedString();
        LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
        if (VERBOSE)
            writer.fine(exception);
        throw new IllegalStateException(exception);
    }
    // Only one thread can call commit (the first thread to do reach the block
    // above).
    // Before commiting the notifications to be done before the done are called
    // the global transaction is called and then the after completion
    // notifications
    // are taken care of. The transactions associated to the global
    // transactions are
    // removed from the map and also the tread to transaction.
    //
    // Asif : Store the thrown Exception in case of commit .
    // Reuse it for thrwing later.
    // Asif TODO:Verify if it is a good practise
    boolean isClean = false;
    Exception e = null;
    try {
        ((TransactionImpl) transactionImpl).notifyBeforeCompletion();
        isClean = true;
    } catch (Exception ge) {
        // Asif : Just mark the Tranxn to setRollbackOnly to ensure Rollback
        setRollbackOnly();
        cozOfException = EXCEPTION_IN_NOTIFY_BEFORE_COMPLETION;
        e = ge;
    }
    // will be harmless
    if (isCommit) {
        synchronized (gtx) {
            if ((status = gtx.getStatus()) == Status.STATUS_COMMITTING) {
                // and appropriately mark the exception code
                try {
                    gtx.commit();
                } catch (RollbackException rbe) {
                    e = rbe;
                    cozOfException = COMMIT_FAILED_SO_ROLLEDBAK;
                } catch (SystemException se) {
                    e = se;
                    cozOfException = COMMIT_FAILED_ROLLBAK_ALSO_FAILED;
                }
            } else if (status == Status.STATUS_ROLLING_BACK) {
                try {
                    gtx.rollback();
                    if (isClean)
                        cozOfException = MARKED_ROLLBACK;
                } catch (SystemException se) {
                    e = se;
                    cozOfException = ROLLBAK_FAILED;
                }
            }
        }
    } else {
        try {
            gtx.rollback();
        } catch (SystemException se) {
            e = se;
            cozOfException = ROLLBAK_FAILED;
        }
    }
    try {
        ((TransactionImpl) transactionImpl).notifyAfterCompletion(status = gtx.getStatus());
    } catch (Exception ge) {
        LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
        if (writer.infoEnabled())
            writer.info(LocalizedStrings.TransactionManagerImpl_EXCEPTION_IN_NOTIFY_AFTER_COMPLETION_DUE_TO__0, ge.getMessage(), ge);
    }
    Thread thread = Thread.currentThread();
    transactionMap.remove(thread);
    this.gtxSet.remove(gtx);
    if (status != Status.STATUS_COMMITTED) {
        switch(cozOfException) {
            case EXCEPTION_IN_NOTIFY_BEFORE_COMPLETION:
                {
                    String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_COMMIT_TRANSACTION_ROLLED_BACK_BECAUSE_OF_EXCEPTION_IN_NOTIFYBEFORECOMPLETION_FUNCTION_CALL_ACTUAL_EXCEPTION_0.toLocalizedString();
                    LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
                    if (VERBOSE)
                        writer.fine(exception, e);
                    RollbackException re = new RollbackException(exception);
                    re.initCause(e);
                    throw re;
                }
            case MARKED_ROLLBACK:
                {
                    String exception = LocalizedStrings.TransactionManagerImpl_TRANSACTIONMANAGERIMPL_COMMIT_TRANSACTION_ROLLED_BACK_BECAUSE_A_USER_MARKED_IT_FOR_ROLLBACK.toLocalizedString();
                    LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
                    if (VERBOSE)
                        writer.fine(exception, e);
                    throw new RollbackException(exception);
                }
            case COMMIT_FAILED_SO_ROLLEDBAK:
                {
                    LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
                    if (VERBOSE)
                        writer.fine(e);
                    throw (RollbackException) e;
                }
            case COMMIT_FAILED_ROLLBAK_ALSO_FAILED:
            case ROLLBAK_FAILED:
                {
                    LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
                    if (VERBOSE)
                        writer.fine(e);
                    throw (SystemException) e;
                }
        }
    }
    gtx.setStatus(Status.STATUS_NO_TRANSACTION);
}
Also used : SystemException(javax.transaction.SystemException) Transaction(javax.transaction.Transaction) LogWriterI18n(org.apache.geode.i18n.LogWriterI18n) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) InvalidTransactionException(javax.transaction.InvalidTransactionException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) HeuristicMixedException(javax.transaction.HeuristicMixedException) CancelException(org.apache.geode.CancelException)

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

Example 8 with HeuristicRollbackException

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

the class MessageEndpointInvocationHandler method afterDelivery.

@Override
public void afterDelivery() throws ResourceException {
    final TransactionManager tm = getTransactionManager();
    try {
        if (currentTx != null) {
            if (currentTx.getStatus() == Status.STATUS_MARKED_ROLLBACK)
                tm.rollback();
            else
                tm.commit();
            currentTx = null;
        }
        if (previousTx != null) {
            tm.resume(previousTx);
            previousTx = null;
        }
    } catch (InvalidTransactionException e) {
        throw new LocalTransactionException(e);
    } catch (HeuristicMixedException e) {
        throw new LocalTransactionException(e);
    } catch (SystemException e) {
        throw new LocalTransactionException(e);
    } catch (HeuristicRollbackException e) {
        throw new LocalTransactionException(e);
    } catch (RollbackException e) {
        throw new LocalTransactionException(e);
    } finally {
        WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(previousClassLoader);
        previousClassLoader = null;
    }
}
Also used : HeuristicRollbackException(javax.transaction.HeuristicRollbackException) LocalTransactionException(javax.resource.spi.LocalTransactionException) SystemException(javax.transaction.SystemException) TransactionManager(javax.transaction.TransactionManager) HeuristicMixedException(javax.transaction.HeuristicMixedException) InvalidTransactionException(javax.transaction.InvalidTransactionException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException)

Example 9 with HeuristicRollbackException

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

the class DatabaseTimerPersistence method shouldRun.

@Override
public boolean shouldRun(TimerImpl timer, TransactionManager tm) {
    if (!allowExecution) {
        //timers never execute on this node
        return false;
    }
    String loadTimer = sql(UPDATE_RUNNING);
    Connection connection = null;
    PreparedStatement statement = null;
    try {
        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) {
            // something wrong with the preparation
            throw new RuntimeException(e);
        }
        tm.begin();
        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 : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException) NotSupportedException(javax.transaction.NotSupportedException)

Example 10 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)12 HeuristicMixedException (javax.transaction.HeuristicMixedException)10 SystemException (javax.transaction.SystemException)10 RollbackException (javax.transaction.RollbackException)9 NotSupportedException (javax.transaction.NotSupportedException)5 IOException (java.io.IOException)3 XAException (javax.transaction.xa.XAException)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 InvalidTransactionException (javax.transaction.InvalidTransactionException)2 Transaction (javax.transaction.Transaction)2 UserTransaction (javax.transaction.UserTransaction)2 XAResource (javax.transaction.xa.XAResource)2 Test (org.junit.Test)2 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)2 TransactionException (io.requery.TransactionException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1