Search in sources :

Example 1 with IllegalTransactionStateException

use of org.mule.runtime.core.privileged.transaction.xa.IllegalTransactionStateException in project mule by mulesoft.

the class AbstractTransaction method commit.

@Override
public void commit() throws TransactionException {
    try {
        logger.debug("Committing transaction " + identityHashCode(this));
        if (isRollbackOnly()) {
            throw new IllegalTransactionStateException(transactionMarkedForRollback());
        }
        doCommit();
        fireNotification(new TransactionNotification(getId(), TRANSACTION_COMMITTED, getApplicationName()));
    } finally {
        TransactionCoordination.getInstance().unbindTransaction(this);
    }
}
Also used : TransactionNotification(org.mule.runtime.api.notification.TransactionNotification) IllegalTransactionStateException(org.mule.runtime.core.privileged.transaction.xa.IllegalTransactionStateException)

Example 2 with IllegalTransactionStateException

use of org.mule.runtime.core.privileged.transaction.xa.IllegalTransactionStateException 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 3 with IllegalTransactionStateException

use of org.mule.runtime.core.privileged.transaction.xa.IllegalTransactionStateException in project mule by mulesoft.

the class XaTransaction method bindResource.

/**
 * @param key Must be the provider of the resource object. i.e. for JDBC it's the XADataSource, for JMS is the
 *        XAConnectionFactory. It can be a wrapper in which case should be a
 *        {@link XaResourceFactoryHolder} to be able to determine correctly if there's already a
 *        resource for that {@link javax.transaction.xa.XAResource} provider.
 * @param resource the resource object. It must be an {@link javax.transaction.xa.XAResource} or a
 *        {@link MuleXaObject}
 * @throws TransactionException
 */
public synchronized void bindResource(Object key, Object resource) throws TransactionException {
    ResourceKey normalizedKey = getResourceEntry(key, resource);
    if (resources.containsKey(key)) {
        throw new IllegalTransactionStateException(CoreMessages.transactionResourceAlreadyListedForKey(key));
    }
    resources.put(normalizedKey, resource);
    if (key == null) {
        logger.error("Key for bound resource " + resource + " is null");
    }
    if (resource instanceof MuleXaObject) {
        MuleXaObject xaObject = (MuleXaObject) resource;
        xaObject.enlist();
    } else if (resource instanceof XAResource) {
        enlistResource((XAResource) resource);
    } else {
        logger.error("Bound resource " + resource + " is neither a MuleXaObject nor XAResource");
    }
}
Also used : MuleXaObject(org.mule.runtime.api.tx.MuleXaObject) XAResource(javax.transaction.xa.XAResource) IllegalTransactionStateException(org.mule.runtime.core.privileged.transaction.xa.IllegalTransactionStateException)

Example 4 with IllegalTransactionStateException

use of org.mule.runtime.core.privileged.transaction.xa.IllegalTransactionStateException in project mule by mulesoft.

the class TransactionCoordination method unbindTransaction.

public void unbindTransaction(final Transaction transaction) throws TransactionException {
    Transaction oldTx = transactions.get();
    try {
        if (oldTx != null && !oldTx.equals(transaction)) {
            throw new IllegalTransactionStateException(CoreMessages.transactionCannotUnbind());
        }
    } finally {
        transactions.set(null);
        logTransactionUnbound(transaction);
    }
}
Also used : DelegateTransaction(org.mule.runtime.core.internal.processor.DelegateTransaction) IllegalTransactionStateException(org.mule.runtime.core.privileged.transaction.xa.IllegalTransactionStateException)

Example 5 with IllegalTransactionStateException

use of org.mule.runtime.core.privileged.transaction.xa.IllegalTransactionStateException in project mule by mulesoft.

the class TransactionCoordination method bindTransaction.

public void bindTransaction(final Transaction transaction) throws TransactionException {
    Transaction oldTx = transactions.get();
    // special handling for transaction collection
    if (oldTx != null && !(oldTx instanceof DelegateTransaction)) {
        throw new IllegalTransactionStateException(CoreMessages.transactionAlreadyBound());
    }
    if (oldTx != null && oldTx instanceof DelegateTransaction) {
        DelegateTransaction delegateTransaction = (DelegateTransaction) oldTx;
        if (!delegateTransaction.supportsInnerTransaction(transaction)) {
            throw new IllegalTransactionStateException(CoreMessages.transactionAlreadyBound());
        }
        return;
    }
    transactions.set(transaction);
    logTransactionBound(transaction);
}
Also used : DelegateTransaction(org.mule.runtime.core.internal.processor.DelegateTransaction) IllegalTransactionStateException(org.mule.runtime.core.privileged.transaction.xa.IllegalTransactionStateException) DelegateTransaction(org.mule.runtime.core.internal.processor.DelegateTransaction)

Aggregations

IllegalTransactionStateException (org.mule.runtime.core.privileged.transaction.xa.IllegalTransactionStateException)7 Test (org.junit.Test)2 DelegateTransaction (org.mule.runtime.core.internal.processor.DelegateTransaction)2 SmallTest (org.mule.tck.size.SmallTest)2 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)1 InvalidTransactionException (javax.transaction.InvalidTransactionException)1 RollbackException (javax.transaction.RollbackException)1 SystemException (javax.transaction.SystemException)1 XAException (javax.transaction.xa.XAException)1 XAResource (javax.transaction.xa.XAResource)1 TransactionNotification (org.mule.runtime.api.notification.TransactionNotification)1 MuleXaObject (org.mule.runtime.api.tx.MuleXaObject)1 TransactionException (org.mule.runtime.api.tx.TransactionException)1 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)1 TransactionRollbackException (org.mule.runtime.core.api.transaction.TransactionRollbackException)1 TransactionStatusException (org.mule.runtime.core.api.transaction.TransactionStatusException)1 TestTransaction (org.mule.tck.testmodels.mule.TestTransaction)1