Search in sources :

Example 6 with MuleTransactionConfig

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

the class TransactionalExecutionTemplateTestCase method testActionAlwaysBeginAndSuspendXaTxAndRollbackNewTx.

@Test
public void testActionAlwaysBeginAndSuspendXaTxAndRollbackNewTx() throws Exception {
    TransactionCoordination.getInstance().bindTransaction(mockTransaction);
    mockTransaction.setXA(true);
    when(mockTransaction.isRollbackOnly()).thenReturn(true);
    MuleTransactionConfig config = new MuleTransactionConfig(TransactionConfig.ACTION_ALWAYS_BEGIN);
    ExecutionTemplate executionTemplate = createExecutionTemplate(config);
    config.setFactory(new TestTransactionFactory(mockNewTransaction));
    Object result = executionTemplate.execute(getRollbackTransactionCallback());
    assertThat(result, is(RETURN_VALUE));
    verify(mockNewTransaction).rollback();
    verify(mockNewTransaction, never()).commit();
    verify(mockTransaction).suspend();
    verify(mockTransaction).resume();
    verify(mockTransaction, never()).commit();
    verify(mockTransaction, never()).rollback();
    assertThat((TestTransaction) TransactionCoordination.getInstance().getTransaction(), is(mockTransaction));
}
Also used : TestTransactionFactory(org.mule.tck.testmodels.mule.TestTransactionFactory) MuleTransactionConfig(org.mule.runtime.core.api.transaction.MuleTransactionConfig) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 7 with MuleTransactionConfig

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

the class TransactionalExecutionTemplateTestCase method testActionAlwaysBeginAndRollbackTxAndRollbackNewTx.

@Test(expected = IllegalTransactionStateException.class)
public void testActionAlwaysBeginAndRollbackTxAndRollbackNewTx() throws Exception {
    TransactionCoordination.getInstance().bindTransaction(mockTransaction);
    when(mockTransaction.isRollbackOnly()).thenReturn(true);
    MuleTransactionConfig config = new MuleTransactionConfig(TransactionConfig.ACTION_ALWAYS_BEGIN);
    ExecutionTemplate executionTemplate = createExecutionTemplate(config);
    config.setFactory(new TestTransactionFactory(mockNewTransaction));
    Object result = executionTemplate.execute(getRollbackTransactionCallback());
    assertThat(result, is(RETURN_VALUE));
    verify(mockTransaction).rollback();
    verify(mockNewTransaction).rollback();
    verify(mockTransaction, never()).commit();
    verify(mockNewTransaction, never()).commit();
    assertThat(TransactionCoordination.getInstance().getTransaction(), IsNull.<Object>nullValue());
}
Also used : TestTransactionFactory(org.mule.tck.testmodels.mule.TestTransactionFactory) MuleTransactionConfig(org.mule.runtime.core.api.transaction.MuleTransactionConfig) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 8 with MuleTransactionConfig

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

the class FlowProcessingPhase method runPhase.

@Override
public void runPhase(final FlowProcessingPhaseTemplate flowProcessingPhaseTemplate, final MessageProcessContext messageProcessContext, final PhaseResultNotifier phaseResultNotifier) {
    Runnable flowExecutionWork = () -> {
        try {
            FlowConstruct flowConstruct = registry.<FlowConstruct>lookupByName(messageProcessContext.getMessageSource().getRootContainerLocation().toString()).get();
            try {
                final AtomicReference exceptionThrownDuringFlowProcessing = new AtomicReference();
                TransactionalExecutionTemplate<CoreEvent> transactionTemplate = createTransactionalExecutionTemplate(muleContext, messageProcessContext.getTransactionConfig().orElse(new MuleTransactionConfig()));
                CoreEvent response = transactionTemplate.execute(() -> {
                    try {
                        Object message = flowProcessingPhaseTemplate.getOriginalMessage();
                        if (message == null) {
                            return null;
                        }
                        CoreEvent muleEvent = flowProcessingPhaseTemplate.getEvent();
                        muleEvent = flowProcessingPhaseTemplate.beforeRouteEvent(muleEvent);
                        muleEvent = flowProcessingPhaseTemplate.routeEvent(muleEvent);
                        muleEvent = flowProcessingPhaseTemplate.afterRouteEvent(muleEvent);
                        sendResponseIfNeccessary(messageProcessContext.getMessageSource(), flowConstruct, muleEvent, flowProcessingPhaseTemplate);
                        return muleEvent;
                    } catch (Exception e) {
                        exceptionThrownDuringFlowProcessing.set(e);
                        throw e;
                    }
                });
                if (exceptionThrownDuringFlowProcessing.get() != null && !(exceptionThrownDuringFlowProcessing.get() instanceof ResponseDispatchException)) {
                    sendResponseIfNeccessary(messageProcessContext.getMessageSource(), flowConstruct, response, flowProcessingPhaseTemplate);
                }
                flowProcessingPhaseTemplate.afterSuccessfulProcessingFlow(response);
            } catch (ResponseDispatchException e) {
                flowProcessingPhaseTemplate.afterFailureProcessingFlow(e);
            } catch (MessagingException e) {
                sendFailureResponseIfNeccessary(messageProcessContext.getMessageSource(), flowConstruct, e, flowProcessingPhaseTemplate);
                flowProcessingPhaseTemplate.afterFailureProcessingFlow(e);
            }
            phaseResultNotifier.phaseSuccessfully();
        } catch (Exception e) {
            MuleException me = new DefaultMuleException(e);
            try {
                flowProcessingPhaseTemplate.afterFailureProcessingFlow(me);
            } catch (MuleException e1) {
                logger.warn("Failure during exception processing in flow template: " + e.getMessage());
                if (logger.isDebugEnabled()) {
                    logger.debug("Failure during exception processing in flow template: ", e);
                }
            }
            phaseResultNotifier.phaseFailure(e);
        }
    };
    if (messageProcessContext.supportsAsynchronousProcessing()) {
        try {
            messageProcessContext.getFlowExecutionExecutor().execute(flowExecutionWork);
        } catch (SchedulerBusyException e) {
            phaseResultNotifier.phaseFailure(e);
        }
    } else {
        flowExecutionWork.run();
    }
}
Also used : MessagingException(org.mule.runtime.core.internal.exception.MessagingException) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) AtomicReference(java.util.concurrent.atomic.AtomicReference) MuleTransactionConfig(org.mule.runtime.core.api.transaction.MuleTransactionConfig) SchedulerBusyException(org.mule.runtime.api.scheduler.SchedulerBusyException) ResponseDispatchException(org.mule.runtime.core.privileged.exception.ResponseDispatchException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) MuleException(org.mule.runtime.api.exception.MuleException) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) TransactionalExecutionTemplate.createTransactionalExecutionTemplate(org.mule.runtime.core.api.execution.TransactionalExecutionTemplate.createTransactionalExecutionTemplate) TransactionalExecutionTemplate(org.mule.runtime.core.api.execution.TransactionalExecutionTemplate) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ResponseDispatchException(org.mule.runtime.core.privileged.exception.ResponseDispatchException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) MuleException(org.mule.runtime.api.exception.MuleException) SchedulerBusyException(org.mule.runtime.api.scheduler.SchedulerBusyException)

Example 9 with MuleTransactionConfig

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

the class ExtensionMessageSource method buildTransactionConfig.

private TransactionConfig buildTransactionConfig() {
    MuleTransactionConfig transactionConfig = new MuleTransactionConfig();
    transactionConfig.setAction(toActionCode(sourceAdapter.getTransactionalAction()));
    transactionConfig.setMuleContext(muleContext);
    TransactionType transactionalType = sourceAdapter.getTransactionalType();
    transactionConfig.setFactory(transactionFactoryLocator.lookUpTransactionFactory(transactionalType).orElseThrow(() -> new IllegalStateException(format("Unable to create Source with Transactions of Type: [%s]. No factory available for this transaction type", transactionalType))));
    return transactionConfig;
}
Also used : TransactionType(org.mule.runtime.api.tx.TransactionType) MuleTransactionConfig(org.mule.runtime.core.api.transaction.MuleTransactionConfig)

Example 10 with MuleTransactionConfig

use of org.mule.runtime.core.api.transaction.MuleTransactionConfig 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)

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