Search in sources :

Example 1 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 2 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 3 with HeuristicRollbackException

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

the class TransactionalMethod method onExit.

public void onExit() throws SecurityException, HeuristicMixedException, HeuristicRollbackException, SystemException, InvalidTransactionException, IllegalStateException {
    switch(propagation) {
        case REQUIRES:
            // Are we the owner of the transaction?
            Transaction transaction = m_owned.get(Thread.currentThread());
            if (transaction != null) {
                // Owner.
                try {
                    // Commit the transaction
                    transaction.commit();
                    m_owned.remove(Thread.currentThread());
                    // Manage potential notification.
                    handler.transactionCommitted(transaction);
                } catch (RollbackException e) {
                    m_owned.remove(Thread.currentThread());
                    // The transaction was rolledback
                    if (exceptionOnRollback) {
                        throw new IllegalStateException("The transaction was rolled back : " + e.getMessage());
                    }
                    // Manage potential notification.
                    handler.transactionRolledback(transaction);
                }
            }
            // Else wait for commit.
            break;
        case MANDATORY:
            // We are never the owner, so just exits the transaction.
            break;
        case SUPPORTED:
            // Do nothing.
            break;
        case NOT_SUPPORTED:
            // Do nothing.
            break;
        case NEVER:
            // Do nothing.
            break;
        case REQUIRES_NEW:
            // We're necessary the owner.
            transaction = m_owned.get(Thread.currentThread());
            if (transaction == null) {
                throw new RuntimeException("Cannot apply the REQUIRES NEW propagation, we're not the transaction owner!");
            }
            try {
                // Commit the transaction
                transaction.commit();
                m_owned.remove(Thread.currentThread());
                // Manage potential notification.
                handler.transactionCommitted(transaction);
                if (suspended != null) {
                    // suspend the completed transaction.
                    manager.suspend();
                    manager.resume(suspended);
                    suspended = null;
                }
            } catch (RollbackException e) {
                // The transaction was rolledback rather than committed
                m_owned.remove(Thread.currentThread());
                if (suspended != null) {
                    // suspend the completed transaction.
                    manager.suspend();
                    // The resume transaction is not rolledback, they are independent.
                    manager.resume(suspended);
                    suspended = null;
                }
                // The transaction was rolledback
                if (exceptionOnRollback) {
                    throw new IllegalStateException("The transaction was rolled back : " + e.getMessage());
                }
                // Manage potential notification.
                handler.transactionRolledback(transaction);
            }
            break;
        default:
            throw new UnsupportedOperationException("Unknown or unsupported propagation policy for " + method + " :" + propagation);
    }
}
Also used : Transaction(javax.transaction.Transaction) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException)

Example 4 with HeuristicRollbackException

use of javax.transaction.HeuristicRollbackException in project smscgateway by RestComm.

the class SchedulerResourceAdaptor method doInjectSms.

private boolean doInjectSms(SmsSet smsSet, boolean callFromSbb) throws NotSupportedException, SystemException, Exception, RollbackException, HeuristicMixedException, HeuristicRollbackException {
    if (!callFromSbb) {
        // If this call is from SBB it comes with Transaction and no need to start one
        SleeTransaction sleeTx = this.sleeTransactionManager.beginSleeTransaction();
    }
    try {
        this.doSetDestCluster(smsSet);
        String eventName = null;
        switch(smsSet.getType()) {
            case SMS_FOR_ESME:
                eventName = EVENT_SMPP_SM;
                break;
            case SMS_FOR_SS7:
                eventName = EVENT_SS7_SM;
                break;
            case SMS_FOR_SIP:
                eventName = EVENT_SIP_SM;
                break;
        }
        final FireableEventType eventTypeId = this.eventIdCache.getEventId(eventName);
        SmsSetEvent event = new SmsSetEvent();
        event.setSmsSet(smsSet);
        SchedulerActivityImpl activity = new SchedulerActivityImpl(this);
        this.sleeEndpoint.startActivityTransacted(activity.getActivityHandle(), activity, ACTIVITY_FLAGS);
        try {
            this.sleeEndpoint.fireEventTransacted(activity.getActivityHandle(), eventTypeId, event, null, null);
        } catch (Exception e) {
            if (this.tracer.isSevereEnabled()) {
                this.tracer.severe("Failed to fire SmsSet event Class=: " + eventTypeId.getEventClassName(), e);
            }
            try {
                this.sleeEndpoint.endActivityTransacted(activity.getActivityHandle());
            } catch (Exception ee) {
            }
        }
        markAsInSystem(smsSet);
    } catch (Exception e) {
        if (!callFromSbb) {
            this.sleeTransactionManager.rollback();
        }
        throw e;
    }
    if (!callFromSbb) {
        // If this call is from SBB it comes with Transaction and no need to commit here
        this.sleeTransactionManager.commit();
    }
    this.incrementActivityCount();
    return true;
}
Also used : SleeTransaction(javax.slee.transaction.SleeTransaction) SmsSetEvent(org.mobicents.smsc.slee.services.smpp.server.events.SmsSetEvent) FireableEventType(javax.slee.resource.FireableEventType) RollbackException(javax.transaction.RollbackException) PersistenceException(org.mobicents.smsc.cassandra.PersistenceException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) InvalidConfigurationException(javax.slee.resource.InvalidConfigurationException) HeuristicMixedException(javax.transaction.HeuristicMixedException)

Example 5 with HeuristicRollbackException

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

the class JTATest method testDuplicateXAREndCalledFailure.

@Test
public void testDuplicateXAREndCalledFailure() throws SystemException, NotSupportedException, HeuristicRollbackException, HeuristicMixedException, RollbackException {
    javax.transaction.TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
    tm.begin();
    javax.transaction.Transaction theTransaction = tm.getTransaction();
    XAException rmFail = new XAException(XAException.XAER_RMFAIL);
    XAException rbRollback = new XAException(XAException.XA_RBROLLBACK);
    assertTrue(theTransaction.enlistResource(new SimpleXAResource() {

        @Override
        public boolean isSameRM(XAResource xares) throws XAException {
            return true;
        }

        @Override
        public void end(Xid xid, int flags) throws XAException {
            super.end(xid, flags);
        }
    }));
    assertTrue(theTransaction.enlistResource(new SimpleXAResource() {

        @Override
        public boolean isSameRM(XAResource xares) throws XAException {
            return true;
        }

        @Override
        public void end(Xid xid, int flags) throws XAException {
            throw rmFail;
        }
    }));
    assertTrue(theTransaction.enlistResource(new SimpleXAResource() {

        @Override
        public boolean isSameRM(XAResource xares) throws XAException {
            return true;
        }

        @Override
        public void end(Xid xid, int flags) throws XAException {
            throw rbRollback;
        }
    }));
    try {
        tm.commit();
        fail("Committed");
    } catch (RollbackException e) {
        assertTrue(Arrays.asList(e.getSuppressed()).contains(rmFail));
        assertTrue(Arrays.asList(e.getSuppressed()).contains(rbRollback));
    }
}
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)

Aggregations

HeuristicRollbackException (javax.transaction.HeuristicRollbackException)36 HeuristicMixedException (javax.transaction.HeuristicMixedException)28 RollbackException (javax.transaction.RollbackException)28 SystemException (javax.transaction.SystemException)26 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 Statement (java.sql.Statement)3 NamingException (javax.naming.NamingException)3 InvalidTransactionException (javax.transaction.InvalidTransactionException)3