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