Search in sources :

Example 6 with Transaction

use of org.mule.runtime.core.api.transaction.Transaction in project mule by mulesoft.

the class ExternalTransactionInterceptor method execute.

@Override
public T execute(ExecutionCallback<T> callback, ExecutionContext executionContext) throws Exception {
    Transaction joinedExternal = null;
    Transaction tx = TransactionCoordination.getInstance().getTransaction();
    try {
        if (tx == null && muleContext != null && transactionConfig != null && transactionConfig.isInteractWithExternal()) {
            TransactionFactory tmFactory = transactionConfig.getFactory();
            if (tmFactory instanceof ExternalTransactionAwareTransactionFactory) {
                ExternalTransactionAwareTransactionFactory externalTransactionFactory = (ExternalTransactionAwareTransactionFactory) tmFactory;
                joinedExternal = externalTransactionFactory.joinExternalTransaction(muleContext);
            }
        }
        return next.execute(callback, executionContext);
    } finally {
        if (joinedExternal != null) {
            TransactionCoordination.getInstance().unbindTransaction(joinedExternal);
        }
    }
}
Also used : Transaction(org.mule.runtime.core.api.transaction.Transaction) TransactionFactory(org.mule.runtime.core.api.transaction.TransactionFactory) ExternalTransactionAwareTransactionFactory(org.mule.runtime.core.api.transaction.ExternalTransactionAwareTransactionFactory) ExternalTransactionAwareTransactionFactory(org.mule.runtime.core.api.transaction.ExternalTransactionAwareTransactionFactory)

Example 7 with Transaction

use of org.mule.runtime.core.api.transaction.Transaction in project mule by mulesoft.

the class IsolateCurrentTransactionInterceptor method execute.

@Override
public T execute(ExecutionCallback<T> muleEventProcessingCallback, ExecutionContext executionContext) throws Exception {
    boolean transactionIsolated = false;
    try {
        if (transactionConfig.getAction() == TransactionConfig.ACTION_NOT_SUPPORTED) {
            Transaction transaction = TransactionCoordination.getInstance().getTransaction();
            if (transaction != null) {
                TransactionCoordination.getInstance().isolateTransaction();
                transactionIsolated = true;
            }
        }
        return next.execute(muleEventProcessingCallback, executionContext);
    } finally {
        if (transactionIsolated) {
            TransactionCoordination.getInstance().restoreIsolatedTransaction();
        }
    }
}
Also used : Transaction(org.mule.runtime.core.api.transaction.Transaction)

Example 8 with Transaction

use of org.mule.runtime.core.api.transaction.Transaction in project mule by mulesoft.

the class SuspendXaTransactionInterceptor method execute.

@Override
public T execute(ExecutionCallback<T> callback, ExecutionContext executionContext) throws Exception {
    Transaction suspendedXATx = null;
    Transaction tx = TransactionCoordination.getInstance().getTransaction();
    byte action = transactionConfig.getAction();
    if ((action == TransactionConfig.ACTION_NONE || action == TransactionConfig.ACTION_ALWAYS_BEGIN) && tx != null && tx.isXA()) {
        if (logger.isDebugEnabled()) {
            logger.debug("suspending XA tx " + action + ", " + "current TX: " + tx);
        }
        suspendedXATx = tx;
        suspendXATransaction(suspendedXATx);
    }
    try {
        T result = next.execute(callback, executionContext);
        resumeXaTransactionIfRequired(suspendedXATx);
        return result;
    } catch (MessagingException e) {
        if (processOnException) {
            TransactionCoordination.getInstance().resumeXaTransactionIfAvailable();
        }
        throw e;
    }
}
Also used : Transaction(org.mule.runtime.core.api.transaction.Transaction) MessagingException(org.mule.runtime.core.internal.exception.MessagingException)

Example 9 with Transaction

use of org.mule.runtime.core.api.transaction.Transaction in project mule by mulesoft.

the class TestTransactionFactory method beginTransaction.

public Transaction beginTransaction(MuleContext muleContext) throws TransactionException {
    Transaction testTransaction;
    if (mockTransaction != null) {
        testTransaction = mockTransaction;
    } else {
        testTransaction = new TestTransaction(muleContext);
    }
    testTransaction.begin();
    return testTransaction;
}
Also used : Transaction(org.mule.runtime.core.api.transaction.Transaction)

Example 10 with Transaction

use of org.mule.runtime.core.api.transaction.Transaction in project mule by mulesoft.

the class ExtensionTransactionFactory method beginTransaction.

/**
 * {@inheritDoc}
 */
@Override
public Transaction beginTransaction(MuleContext muleContext) throws TransactionException {
    Transaction transaction = new ExtensionTransaction(muleContext);
    transaction.begin();
    return transaction;
}
Also used : Transaction(org.mule.runtime.core.api.transaction.Transaction)

Aggregations

Transaction (org.mule.runtime.core.api.transaction.Transaction)15 Test (org.junit.Test)4 TestTransaction (org.mule.tck.testmodels.mule.TestTransaction)4 Before (org.junit.Before)3 TransactionException (org.mule.runtime.api.tx.TransactionException)2 ExternalTransactionAwareTransactionFactory (org.mule.runtime.core.api.transaction.ExternalTransactionAwareTransactionFactory)2 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)2 Thread.currentThread (java.lang.Thread.currentThread)1 Set (java.util.Set)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 TransactionManager (javax.transaction.TransactionManager)1 Matchers.hasSize (org.hamcrest.Matchers.hasSize)1 Matchers.sameInstance (org.hamcrest.Matchers.sameInstance)1 Every.everyItem (org.hamcrest.core.Every.everyItem)1 After (org.junit.After)1 Assert.assertThat (org.junit.Assert.assertThat)1 Rule (org.junit.Rule)1 ExpectedException (org.junit.rules.ExpectedException)1 Mockito.doAnswer (org.mockito.Mockito.doAnswer)1 Mockito.mock (org.mockito.Mockito.mock)1