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));
}
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());
}
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();
}
}
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;
}
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());
}
Aggregations