Search in sources :

Example 1 with TransactionException

use of org.mule.runtime.api.tx.TransactionException in project mule by mulesoft.

the class DelegateTransaction method bindResource.

@Override
public void bindResource(Object key, Object resource) throws TransactionException {
    if (!(this.delegate instanceof NullTransaction)) {
        throw new TransactionException(CoreMessages.createStaticMessage("Single resource transaction has already a resource bound"));
    }
    TransactionFactory transactionFactory = muleContext.getTransactionFactoryManager().getTransactionFactoryFor(key.getClass());
    this.unbindTransaction();
    int timeout = delegate.getTimeout();
    this.delegate = transactionFactory.beginTransaction(muleContext);
    delegate.setTimeout(timeout);
    delegate.bindResource(key, resource);
}
Also used : TransactionException(org.mule.runtime.api.tx.TransactionException) TransactionFactory(org.mule.runtime.core.api.transaction.TransactionFactory)

Example 2 with TransactionException

use of org.mule.runtime.api.tx.TransactionException 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 TransactionException

use of org.mule.runtime.api.tx.TransactionException in project mule by mulesoft.

the class XaTransaction method doBegin.

protected void doBegin() throws TransactionException {
    if (txManager == null) {
        throw new IllegalStateException(CoreMessages.objectNotRegistered("javax.transaction.TransactionManager", "Transaction Manager").getMessage());
    }
    try {
        txManager.setTransactionTimeout(getTimeoutInSeconds());
        txManager.begin();
        synchronized (this) {
            transaction = txManager.getTransaction();
        }
    } catch (Exception e) {
        throw new TransactionException(CoreMessages.cannotStartTransaction("XA"), e);
    }
}
Also used : InvalidTransactionException(javax.transaction.InvalidTransactionException) TransactionException(org.mule.runtime.api.tx.TransactionException) 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 4 with TransactionException

use of org.mule.runtime.api.tx.TransactionException in project mule by mulesoft.

the class PetStoreRetryPolicyProviderConnectionTestCase method createTransactionMock.

private Transaction createTransactionMock() throws TransactionException {
    Transaction transaction = mock(Transaction.class);
    doAnswer((invocationOnMock -> {
        TransactionCoordination.getInstance().bindTransaction(transaction);
        return null;
    })).when(transaction).begin();
    return transaction;
}
Also used : AbstractExtensionFunctionalTestCase(org.mule.test.module.extension.AbstractExtensionFunctionalTestCase) ErrorTypeMatcher.errorType(org.mule.tck.junit4.matcher.ErrorTypeMatcher.errorType) Thread.currentThread(java.lang.Thread.currentThread) Assert.assertThat(org.junit.Assert.assertThat) Every.everyItem(org.hamcrest.core.Every.everyItem) UNIT_TEST_THREAD_GROUP(org.mule.tck.SimpleUnitTestSupportSchedulerService.UNIT_TEST_THREAD_GROUP) CONNECTIVITY_ERROR_IDENTIFIER(org.mule.runtime.core.api.exception.Errors.Identifiers.CONNECTIVITY_ERROR_IDENTIFIER) ACTION_ALWAYS_BEGIN(org.mule.functional.api.flow.TransactionConfigEnum.ACTION_ALWAYS_BEGIN) Mockito.doAnswer(org.mockito.Mockito.doAnswer) PetStoreOperationsWithFailures.resetConnectionThreads(org.mule.test.petstore.extension.PetStoreOperationsWithFailures.resetConnectionThreads) After(org.junit.After) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ExpectedException(org.junit.rules.ExpectedException) Collectors.toSet(java.util.stream.Collectors.toSet) Before(org.junit.Before) PetStoreOperationsWithFailures.getConnectionThreads(org.mule.test.petstore.extension.PetStoreOperationsWithFailures.getConnectionThreads) TestTransactionFactory(org.mule.tck.testmodels.mule.TestTransactionFactory) Set(java.util.Set) Test(org.junit.Test) TransactionCoordination(org.mule.runtime.core.api.transaction.TransactionCoordination) Transaction(org.mule.runtime.core.api.transaction.Transaction) Rule(org.junit.Rule) TransactionException(org.mule.runtime.api.tx.TransactionException) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) Mockito.mock(org.mockito.Mockito.mock) Transaction(org.mule.runtime.core.api.transaction.Transaction)

Example 5 with TransactionException

use of org.mule.runtime.api.tx.TransactionException in project mule by mulesoft.

the class DefaultSourceCallbackContext method bindConnection.

/**
 * {@inheritDoc}
 */
@Override
public TransactionHandle bindConnection(Object connection) throws ConnectionException, TransactionException {
    checkArgument(connection != null, "Cannot bind a null connection");
    if (this.connection != null) {
        throw new IllegalArgumentException("Connection can only be set once per " + SourceCallbackContext.class.getSimpleName());
    }
    this.connection = connection;
    if (sourceCallback.getTransactionConfig().isTransacted() && connection instanceof TransactionalConnection) {
        ConnectionHandler<Object> connectionHandler = sourceCallback.getSourceConnectionManager().getConnectionHandler(connection).orElseThrow(() -> new TransactionException(createWrongConnectionMessage(connection)));
        sourceCallback.getTransactionSourceBinder().bindToTransaction(sourceCallback.getTransactionConfig(), sourceCallback.getConfigurationInstance(), connectionHandler);
        transactionHandle = DEFAULT_TRANSACTION_HANDLE;
    }
    return transactionHandle;
}
Also used : TransactionException(org.mule.runtime.api.tx.TransactionException) TransactionalConnection(org.mule.runtime.extension.api.connectivity.TransactionalConnection)

Aggregations

TransactionException (org.mule.runtime.api.tx.TransactionException)12 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)3 InvalidTransactionException (javax.transaction.InvalidTransactionException)3 RollbackException (javax.transaction.RollbackException)3 SystemException (javax.transaction.SystemException)3 XAException (javax.transaction.xa.XAException)3 Test (org.junit.Test)3 TransactionRollbackException (org.mule.runtime.core.api.transaction.TransactionRollbackException)3 TransactionManager (javax.transaction.TransactionManager)2 Transaction (org.mule.runtime.core.api.transaction.Transaction)2 TransactionStatusException (org.mule.runtime.core.api.transaction.TransactionStatusException)2 ExternalXaTransaction (org.mule.runtime.core.internal.transaction.ExternalXaTransaction)2 XaTransaction (org.mule.runtime.core.privileged.transaction.XaTransaction)2 IllegalTransactionStateException (org.mule.runtime.core.privileged.transaction.xa.IllegalTransactionStateException)2 SmallTest (org.mule.tck.size.SmallTest)2 TestTransaction (org.mule.tck.testmodels.mule.TestTransaction)2 Thread.currentThread (java.lang.Thread.currentThread)1 Set (java.util.Set)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 Transaction (javax.transaction.Transaction)1