Search in sources :

Example 1 with TransactionConfig

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

the class AbstractTxThreadAssociationTestCase method testNoneXaTransactionSuspendResume.

/**
 * NONE action suspends current transaction and begins a new one.
 *
 * @throws Exception if any error
 */
@Test
public void testNoneXaTransactionSuspendResume() 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_NONE);
    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);
            final Transaction firstTx = tm.getTransaction();
            assertNotNull(firstTx);
            assertEquals(firstTx.getStatus(), Status.STATUS_ACTIVE);
            return nestedExecutionTemplate.execute(new ExecutionCallback<Void>() {

                @Override
                public Void process() throws Exception {
                    Transaction secondTx = tm.getTransaction();
                    assertNull(secondTx);
                    assertEquals(firstTx.getStatus(), Status.STATUS_ACTIVE);
                    try {
                        tm.resume(firstTx);
                        assertEquals(firstTx, tm.getTransaction());
                        assertEquals(firstTx.getStatus(), Status.STATUS_ACTIVE);
                        Transaction a = tm.suspend();
                        assertTrue(a.equals(firstTx));
                    } 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 : 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) 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 2 with TransactionConfig

use of org.mule.runtime.core.api.transaction.TransactionConfig 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 3 with TransactionConfig

use of org.mule.runtime.core.api.transaction.TransactionConfig 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 4 with TransactionConfig

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

the class TryScope method process.

@Override
public CoreEvent process(final CoreEvent event) throws MuleException {
    if (nestedChain == null) {
        return event;
    } else {
        ExecutionTemplate<CoreEvent> executionTemplate = createScopeTransactionalExecutionTemplate(muleContext, transactionConfig);
        ExecutionCallback<CoreEvent> processingCallback = () -> {
            try {
                CoreEvent e = processToApply(event, p -> from(p).flatMap(request -> processWithChildContext(request, nestedChain, ofNullable(getLocation()), messagingExceptionHandler)));
                return e;
            } catch (Exception e) {
                throw e;
            }
        };
        try {
            return executionTemplate.execute(processingCallback);
        } catch (MuleException e) {
            throw e;
        } catch (Exception e) {
            throw new DefaultMuleException(errorInvokingMessageProcessorWithinTransaction(nestedChain, transactionConfig), e);
        }
    }
}
Also used : CoreMessages.errorInvokingMessageProcessorWithinTransaction(org.mule.runtime.core.api.config.i18n.CoreMessages.errorInvokingMessageProcessorWithinTransaction) TransactionalExecutionTemplate.createScopeTransactionalExecutionTemplate(org.mule.runtime.core.api.execution.TransactionalExecutionTemplate.createScopeTransactionalExecutionTemplate) MessageProcessors.newChain(org.mule.runtime.core.privileged.processor.MessageProcessors.newChain) Optional.of(java.util.Optional.of) Flux.from(reactor.core.publisher.Flux.from) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) LoggerFactory(org.slf4j.LoggerFactory) LifecycleUtils.initialiseIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.initialiseIfNeeded) Processor(org.mule.runtime.core.api.processor.Processor) TransactionCoordination.isTransactionActive(org.mule.runtime.core.api.transaction.TransactionCoordination.isTransactionActive) Collections.singletonList(java.util.Collections.singletonList) Scope(org.mule.runtime.core.privileged.processor.Scope) TransactionConfig(org.mule.runtime.core.api.transaction.TransactionConfig) MuleException(org.mule.runtime.api.exception.MuleException) AbstractMessageProcessorOwner(org.mule.runtime.core.api.processor.AbstractMessageProcessorOwner) LifecycleUtils.stopIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.stopIfNeeded) MuleTransactionConfig(org.mule.runtime.core.api.transaction.MuleTransactionConfig) Logger(org.slf4j.Logger) MessageProcessors.processToApply(org.mule.runtime.core.privileged.processor.MessageProcessors.processToApply) Optional.ofNullable(java.util.Optional.ofNullable) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) LifecycleUtils.startIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.startIfNeeded) ACTION_INDIFFERENT(org.mule.runtime.core.api.transaction.TransactionConfig.ACTION_INDIFFERENT) FlowExceptionHandler(org.mule.runtime.core.api.exception.FlowExceptionHandler) MessageProcessorChain(org.mule.runtime.core.privileged.processor.chain.MessageProcessorChain) LifecycleUtils.disposeIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.disposeIfNeeded) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) List(java.util.List) ExecutionTemplate(org.mule.runtime.core.api.execution.ExecutionTemplate) MessageProcessors.processWithChildContext(org.mule.runtime.core.privileged.processor.MessageProcessors.processWithChildContext) MessageProcessors.getProcessingStrategy(org.mule.runtime.core.privileged.processor.MessageProcessors.getProcessingStrategy) ExecutionCallback(org.mule.runtime.core.api.execution.ExecutionCallback) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) MuleException(org.mule.runtime.api.exception.MuleException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) MuleException(org.mule.runtime.api.exception.MuleException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException)

Example 5 with TransactionConfig

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

the class ExtensionConnectionSupplierTestCase method xaTransaction.

@Test
public void xaTransaction() throws Exception {
    muleContext.setTransactionManager(mock(TransactionManager.class, RETURNS_DEEP_STUBS));
    XaTransaction transaction = spy(new XaTransaction(muleContext));
    XATransactionalConnection connection = mock(XATransactionalConnection.class, RETURNS_DEEP_STUBS);
    Object config = new Object();
    ExecutionContextAdapter operationContext = mock(ExecutionContextAdapter.class, RETURNS_DEEP_STUBS);
    ConnectionProvider connectionProvider = mock(ConnectionProvider.class);
    ConfigurationInstance configurationInstance = mock(ConfigurationInstance.class);
    when(configurationInstance.getConnectionProvider()).thenReturn(of(connectionProvider));
    when(operationContext.getConfiguration()).thenReturn(of(configurationInstance));
    when(configurationInstance.getValue()).thenReturn(config);
    when(connectionProvider.connect()).thenReturn(connection);
    TransactionConfig transactionConfig = mock(TransactionConfig.class);
    when(transactionConfig.getAction()).thenReturn(ACTION_ALWAYS_JOIN);
    when(transactionConfig.isTransacted()).thenReturn(true);
    when(operationContext.getTransactionConfig()).thenReturn(of(transactionConfig));
    connectionManager.bind(config, connectionProvider);
    TransactionCoordination.getInstance().bindTransaction(transaction);
    adapter.getConnection(operationContext);
    verify(transaction).bindResource(any(), any(XAExtensionTransactionalResource.class));
}
Also used : XaTransaction(org.mule.runtime.core.privileged.transaction.XaTransaction) XAExtensionTransactionalResource(org.mule.runtime.module.extension.internal.runtime.transaction.XAExtensionTransactionalResource) TransactionManager(javax.transaction.TransactionManager) TransactionConfig(org.mule.runtime.core.api.transaction.TransactionConfig) XATransactionalConnection(org.mule.runtime.extension.api.connectivity.XATransactionalConnection) ExecutionContextAdapter(org.mule.runtime.module.extension.api.runtime.privileged.ExecutionContextAdapter) ConnectionProvider(org.mule.runtime.api.connection.ConnectionProvider) ConfigurationInstance(org.mule.runtime.extension.api.runtime.config.ConfigurationInstance) Test(org.junit.Test)

Aggregations

TransactionConfig (org.mule.runtime.core.api.transaction.TransactionConfig)5 Test (org.junit.Test)4 ExecutionCallback (org.mule.runtime.core.api.execution.ExecutionCallback)4 ExecutionTemplate (org.mule.runtime.core.api.execution.ExecutionTemplate)4 MuleTransactionConfig (org.mule.runtime.core.api.transaction.MuleTransactionConfig)4 TransactionalExecutionTemplate (org.mule.runtime.core.api.execution.TransactionalExecutionTemplate)3 XaTransaction (org.mule.runtime.core.privileged.transaction.XaTransaction)3 XaTransactionFactory (org.mule.runtime.core.privileged.transaction.xa.XaTransactionFactory)3 Transaction (javax.transaction.Transaction)2 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Optional.of (java.util.Optional.of)1 Optional.ofNullable (java.util.Optional.ofNullable)1 TransactionManager (javax.transaction.TransactionManager)1 ConnectionProvider (org.mule.runtime.api.connection.ConnectionProvider)1 DefaultMuleException (org.mule.runtime.api.exception.DefaultMuleException)1 MuleException (org.mule.runtime.api.exception.MuleException)1 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)1 CoreMessages.errorInvokingMessageProcessorWithinTransaction (org.mule.runtime.core.api.config.i18n.CoreMessages.errorInvokingMessageProcessorWithinTransaction)1 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)1