Search in sources :

Example 11 with MuleTransactionConfig

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

the class AbstractTxThreadAssociationTestCase method testAlwaysBeginXaTransactionSuspendResume.

/**
 * AlwaysBegin action suspends current transaction and begins a new one.
 *
 * @throws Exception if any error
 */
@Test
public void testAlwaysBeginXaTransactionSuspendResume() throws Exception {
    muleContext.setTransactionManager(tm);
    assertNull("There should be no current transaction associated.", tm.getTransaction());
    // don't wait for ages, has to be set before TX is begun
    tm.setTransactionTimeout(TRANSACTION_TIMEOUT_SECONDS);
    // this is one component with a TX always begin
    TransactionConfig config = new MuleTransactionConfig(TransactionConfig.ACTION_ALWAYS_BEGIN);
    config.setFactory(new XaTransactionFactory());
    ExecutionTemplate<Void> executionTemplate = TransactionalExecutionTemplate.createTransactionalExecutionTemplate(muleContext, config);
    // and the callee component which should begin new transaction, current must be suspended
    final TransactionConfig nestedConfig = new MuleTransactionConfig(TransactionConfig.ACTION_ALWAYS_BEGIN);
    nestedConfig.setFactory(new XaTransactionFactory());
    // start the call chain
    executionTemplate.execute(new ExecutionCallback<Void>() {

        @Override
        public Void process() throws Exception {
            // the callee executes within its own TX template, but uses the same global XA transaction,
            // bound to the current thread of execution via a ThreadLocal
            ExecutionTemplate<Void> innerExecutionTemplate = TransactionalExecutionTemplate.createTransactionalExecutionTemplate(muleContext, nestedConfig);
            final Transaction firstTx = tm.getTransaction();
            assertNotNull(firstTx);
            assertEquals(firstTx.getStatus(), Status.STATUS_ACTIVE);
            return innerExecutionTemplate.execute(new ExecutionCallback<Void>() {

                @Override
                public Void process() throws Exception {
                    Transaction secondTx = tm.getTransaction();
                    assertNotNull(secondTx);
                    assertEquals(firstTx.getStatus(), Status.STATUS_ACTIVE);
                    assertEquals(secondTx.getStatus(), Status.STATUS_ACTIVE);
                    try {
                        tm.resume(firstTx);
                        fail("Second transaction must be active");
                    } catch (java.lang.IllegalStateException e) {
                    // expected
                    // Thrown if the thread is already associated with another transaction.
                    // Second tx is associated with the current thread
                    }
                    try {
                        Transaction currentTx = tm.suspend();
                        assertTrue(currentTx.equals(secondTx));
                        tm.resume(firstTx);
                        assertEquals(firstTx, tm.getTransaction());
                        assertEquals(firstTx.getStatus(), Status.STATUS_ACTIVE);
                        assertEquals(secondTx.getStatus(), Status.STATUS_ACTIVE);
                        Transaction a = tm.suspend();
                        assertTrue(a.equals(firstTx));
                        tm.resume(secondTx);
                    } catch (Exception e) {
                        fail("Error: " + e);
                    }
                    // do not care about the return really
                    return null;
                }
            });
        }
    });
    assertNull("Committing via TX Manager should have disassociated TX from the current thread.", tm.getTransaction());
}
Also used : TransactionConfig(org.mule.runtime.core.api.transaction.TransactionConfig) MuleTransactionConfig(org.mule.runtime.core.api.transaction.MuleTransactionConfig) MuleTransactionConfig(org.mule.runtime.core.api.transaction.MuleTransactionConfig) ExecutionTemplate(org.mule.runtime.core.api.execution.ExecutionTemplate) TransactionalExecutionTemplate(org.mule.runtime.core.api.execution.TransactionalExecutionTemplate) Transaction(javax.transaction.Transaction) XaTransaction(org.mule.runtime.core.privileged.transaction.XaTransaction) XaTransactionFactory(org.mule.runtime.core.privileged.transaction.xa.XaTransactionFactory) ExecutionCallback(org.mule.runtime.core.api.execution.ExecutionCallback) Test(org.junit.Test)

Example 12 with MuleTransactionConfig

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

the class AbstractTxThreadAssociationTestCase method testNoNestedTxStarted.

/**
 * This is a former TransactionTemplateTestCase. http://mule.mulesoft.org/jira/browse/MULE-1494
 *
 * @throws Exception in case of any error
 */
@Test
public void testNoNestedTxStarted() throws Exception {
    muleContext.setTransactionManager(tm);
    assertNull("There should be no current transaction associated.", tm.getTransaction());
    // don't wait for ages, has to be set before TX is begun
    tm.setTransactionTimeout(TRANSACTION_TIMEOUT_SECONDS);
    // this is one service with a TX always begin
    TransactionConfig config = new MuleTransactionConfig(TransactionConfig.ACTION_ALWAYS_BEGIN);
    config.setFactory(new XaTransactionFactory());
    ExecutionTemplate<Void> executionTemplate = TransactionalExecutionTemplate.createTransactionalExecutionTemplate(muleContext, config);
    // and the callee service which should join the current XA transaction, not begin a nested one
    final TransactionConfig nestedConfig = new MuleTransactionConfig(TransactionConfig.ACTION_BEGIN_OR_JOIN);
    nestedConfig.setFactory(new XaTransactionFactory());
    // start the call chain
    executionTemplate.execute(new ExecutionCallback<Void>() {

        @Override
        public Void process() throws Exception {
            // the callee executes within its own TX template, but uses the same global XA transaction,
            // bound to the current thread of execution via a ThreadLocal
            ExecutionTemplate<Void> nestedExecutionTemplate = TransactionalExecutionTemplate.createTransactionalExecutionTemplate(muleContext, nestedConfig);
            return nestedExecutionTemplate.execute(new ExecutionCallback<Void>() {

                @Override
                public Void process() throws Exception {
                    // do not care about the return really
                    return null;
                }
            });
        }
    });
}
Also used : ExecutionTemplate(org.mule.runtime.core.api.execution.ExecutionTemplate) TransactionalExecutionTemplate(org.mule.runtime.core.api.execution.TransactionalExecutionTemplate) XaTransactionFactory(org.mule.runtime.core.privileged.transaction.xa.XaTransactionFactory) TransactionConfig(org.mule.runtime.core.api.transaction.TransactionConfig) MuleTransactionConfig(org.mule.runtime.core.api.transaction.MuleTransactionConfig) MuleTransactionConfig(org.mule.runtime.core.api.transaction.MuleTransactionConfig) ExecutionCallback(org.mule.runtime.core.api.execution.ExecutionCallback) Test(org.junit.Test)

Example 13 with MuleTransactionConfig

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

the class TryProcessorFactoryBean method createTransactionConfig.

protected MuleTransactionConfig createTransactionConfig(String action, TransactionType type) {
    MuleTransactionConfig transactionConfig = new MuleTransactionConfig();
    transactionConfig.setActionAsString(action);
    transactionConfig.setFactory(transactionFactoryLocator.lookUpTransactionFactory(type).orElseThrow(() -> new IllegalArgumentException(format("Unable to create Try Scope with a Transaction Type: [%s]. No factory available for this transaction type", type))));
    return transactionConfig;
}
Also used : MuleTransactionConfig(org.mule.runtime.core.api.transaction.MuleTransactionConfig)

Example 14 with MuleTransactionConfig

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

the class DefaultExecutionContext method buildTransactionConfig.

private TransactionConfig buildTransactionConfig() {
    MuleTransactionConfig transactionConfig = new MuleTransactionConfig();
    transactionConfig.setAction(toActionCode(getTransactionalAction()));
    transactionConfig.setMuleContext(muleContext);
    transactionConfig.setFactory(TRANSACTION_FACTORY);
    return transactionConfig;
}
Also used : MuleTransactionConfig(org.mule.runtime.core.api.transaction.MuleTransactionConfig)

Example 15 with MuleTransactionConfig

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

the class FlowRunner method transactionally.

/**
 * Configures the flow to run inside a transaction.
 *
 * @param action The action to do at the start of the transactional block. See {@link TransactionConfig} constants.
 * @param factory See {@link MuleTransactionConfig#setFactory(TransactionFactory)}.
 * @return this {@link FlowRunner}
 */
public FlowRunner transactionally(TransactionConfigEnum action, TransactionFactory factory) {
    MuleTransactionConfig transactionConfig = new MuleTransactionConfig(action.getAction());
    transactionConfig.setFactory(factory);
    txExecutionTemplate = createTransactionalExecutionTemplate(registry.lookupByType(MuleContext.class).get(), transactionConfig);
    return this;
}
Also used : MuleContext(org.mule.runtime.core.api.MuleContext) MuleTransactionConfig(org.mule.runtime.core.api.transaction.MuleTransactionConfig)

Aggregations

MuleTransactionConfig (org.mule.runtime.core.api.transaction.MuleTransactionConfig)28 Test (org.junit.Test)23 SmallTest (org.mule.tck.size.SmallTest)20 TestTransactionFactory (org.mule.tck.testmodels.mule.TestTransactionFactory)7 TransactionalExecutionTemplate (org.mule.runtime.core.api.execution.TransactionalExecutionTemplate)4 ExecutionCallback (org.mule.runtime.core.api.execution.ExecutionCallback)3 ExecutionTemplate (org.mule.runtime.core.api.execution.ExecutionTemplate)3 TransactionConfig (org.mule.runtime.core.api.transaction.TransactionConfig)3 XaTransactionFactory (org.mule.runtime.core.privileged.transaction.xa.XaTransactionFactory)3 Transaction (javax.transaction.Transaction)2 ExternalTransactionAwareTransactionFactory (org.mule.runtime.core.api.transaction.ExternalTransactionAwareTransactionFactory)2 XaTransaction (org.mule.runtime.core.privileged.transaction.XaTransaction)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 DefaultMuleException (org.mule.runtime.api.exception.DefaultMuleException)1 MuleException (org.mule.runtime.api.exception.MuleException)1 SchedulerBusyException (org.mule.runtime.api.scheduler.SchedulerBusyException)1 TransactionType (org.mule.runtime.api.tx.TransactionType)1 MuleContext (org.mule.runtime.core.api.MuleContext)1 FlowConstruct (org.mule.runtime.core.api.construct.FlowConstruct)1 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)1