use of org.mule.runtime.core.api.transaction.Transaction in project mule by mulesoft.
the class TransactionBindingDelegate method getBoundResource.
/**
* @param transactionConfig given transaction config
* @param txKey the transaction key
* @param connectionHandlerSupplier {@link Supplier} to get the {@link ConnectionHandler} of the current component
* @return The {@link ConnectionHandler} that has be bound to the transaction.
* @throws ConnectionException if a problem occurred retrieving the {@link ConnectionHandler}
* @throws TransactionException if the connection could not be bound to the current transaction
*/
public <T extends TransactionalConnection> ConnectionHandler<T> getBoundResource(TransactionConfig transactionConfig, ExtensionTransactionKey txKey, ConnectionSupplier<ConnectionHandler<T>> connectionHandlerSupplier) throws ConnectionException, TransactionException {
final Transaction currentTx = TransactionCoordination.getInstance().getTransaction();
if (currentTx != null) {
if (currentTx.hasResource(txKey)) {
return new TransactionalConnectionHandler((ExtensionTransactionalResource) currentTx.getResource(txKey));
}
ConnectionHandler<T> connectionHandler = connectionHandlerSupplier.get();
T connection = connectionHandler.getConnection();
ExtensionTransactionalResource<T> txResource = createTransactionalResource(currentTx, connectionHandler, connection);
boolean bound = false;
try {
if (currentTx.supports(txKey, txResource)) {
currentTx.bindResource(txKey, txResource);
bound = true;
return new TransactionalConnectionHandler(txResource);
} else if (transactionConfig.isTransacted()) {
throw new TransactionException(createStaticMessage(format("%s '%s' of extension '%s' uses a transactional connection '%s', but the current transaction " + "doesn't support it and could not be bound", getComponentModelTypeName(componentModel), componentModel.getName(), extensionModel.getName(), connection.getClass().getName())));
}
} finally {
if (!bound) {
connectionHandler.release();
}
}
}
return connectionHandlerSupplier.get();
}
use of org.mule.runtime.core.api.transaction.Transaction in project mule by mulesoft.
the class TransactionSourceBinder method bindToTransaction.
public <T extends TransactionalConnection> Optional<ConnectionHandler<T>> bindToTransaction(TransactionConfig transactionConfig, ConfigurationInstance configurationInstance, ConnectionHandler connectionHandler) throws ConnectionException, TransactionException {
if (!transactionConfig.isTransacted()) {
return empty();
}
Transaction tx = transactionConfig.getFactory().beginTransaction(muleContext);
tx.setTimeout(transactionConfig.getTimeout());
ConfigurationInstance configuration = ofNullable(configurationInstance).orElseThrow(() -> new IllegalStateException(format("Source '%s' of extension '%s' cannot participate in a transaction because it doesn't have a config", componentModel.getName(), extensionModel.getName())));
final ExtensionTransactionKey txKey = new ExtensionTransactionKey(configuration);
return Optional.of(transactionBindingDelegate.getBoundResource(transactionConfig, txKey, () -> connectionHandler));
}
use of org.mule.runtime.core.api.transaction.Transaction in project mule by mulesoft.
the class XaTransactionFactoryTestCase method setsTransactionTimeout.
@Test
public void setsTransactionTimeout() throws Exception {
final int timeout = 1000;
final XaTransactionFactory transactionFactory = new XaTransactionFactory();
transactionFactory.setTimeout(timeout);
final MuleContext muleContext = mockContextWithServices();
final TransactionManager transactionManager = mock(TransactionManager.class);
when(muleContext.getTransactionManager()).thenReturn(transactionManager);
final Transaction transaction = transactionFactory.beginTransaction(muleContext);
assertThat(transaction.getTimeout(), equalTo(timeout));
}
use of org.mule.runtime.core.api.transaction.Transaction in project mule by mulesoft.
the class TransactionalExecutionTemplateTestCase method testActionNoneAndWithExternalTransactionWithTx.
@Test
public void testActionNoneAndWithExternalTransactionWithTx() throws Exception {
TransactionCoordination.getInstance().bindTransaction(mockTransaction);
MuleTransactionConfig config = new MuleTransactionConfig(TransactionConfig.ACTION_NONE);
config.setInteractWithExternal(true);
mockExternalTransactionFactory = mock(ExternalTransactionAwareTransactionFactory.class);
config.setFactory(mockExternalTransactionFactory);
Transaction externalTransaction = mock(Transaction.class);
when(mockExternalTransactionFactory.joinExternalTransaction(mockMuleContext)).thenReturn(externalTransaction);
ExecutionTemplate executionTemplate = createExecutionTemplate(config);
Object result = executionTemplate.execute(getEmptyTransactionCallback());
assertThat(result, is(RETURN_VALUE));
verify(mockTransaction, never()).rollback();
verify(mockTransaction, never()).commit();
assertThat(TransactionCoordination.getInstance().getTransaction(), IsNull.<Object>notNullValue());
}
use of org.mule.runtime.core.api.transaction.Transaction in project mule by mulesoft.
the class TransactionalExecutionTemplateTestCase method unbindTransaction.
@Before
public void unbindTransaction() throws Exception {
Transaction currentTransaction = TransactionCoordination.getInstance().getTransaction();
if (currentTransaction != null) {
TransactionCoordination.getInstance().unbindTransaction(currentTransaction);
}
when(mockMessagingException.getStackTrace()).thenReturn(new StackTraceElement[0]);
}
Aggregations