Search in sources :

Example 11 with Transaction

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();
}
Also used : TransactionException(org.mule.runtime.api.tx.TransactionException) Transaction(org.mule.runtime.core.api.transaction.Transaction)

Example 12 with Transaction

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));
}
Also used : Transaction(org.mule.runtime.core.api.transaction.Transaction) ConfigurationInstance(org.mule.runtime.extension.api.runtime.config.ConfigurationInstance)

Example 13 with Transaction

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));
}
Also used : MuleContext(org.mule.runtime.core.api.MuleContext) Transaction(org.mule.runtime.core.api.transaction.Transaction) TransactionManager(javax.transaction.TransactionManager) XaTransactionFactory(org.mule.runtime.core.privileged.transaction.xa.XaTransactionFactory) Test(org.junit.Test)

Example 14 with Transaction

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());
}
Also used : TestTransaction(org.mule.tck.testmodels.mule.TestTransaction) Transaction(org.mule.runtime.core.api.transaction.Transaction) MuleTransactionConfig(org.mule.runtime.core.api.transaction.MuleTransactionConfig) ExternalTransactionAwareTransactionFactory(org.mule.runtime.core.api.transaction.ExternalTransactionAwareTransactionFactory) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 15 with Transaction

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]);
}
Also used : TestTransaction(org.mule.tck.testmodels.mule.TestTransaction) Transaction(org.mule.runtime.core.api.transaction.Transaction) Before(org.junit.Before)

Aggregations

Transaction (org.mule.runtime.core.api.transaction.Transaction)15 Test (org.junit.Test)4 TestTransaction (org.mule.tck.testmodels.mule.TestTransaction)4 Before (org.junit.Before)3 TransactionException (org.mule.runtime.api.tx.TransactionException)2 ExternalTransactionAwareTransactionFactory (org.mule.runtime.core.api.transaction.ExternalTransactionAwareTransactionFactory)2 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)2 Thread.currentThread (java.lang.Thread.currentThread)1 Set (java.util.Set)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 TransactionManager (javax.transaction.TransactionManager)1 Matchers.hasSize (org.hamcrest.Matchers.hasSize)1 Matchers.sameInstance (org.hamcrest.Matchers.sameInstance)1 Every.everyItem (org.hamcrest.core.Every.everyItem)1 After (org.junit.After)1 Assert.assertThat (org.junit.Assert.assertThat)1 Rule (org.junit.Rule)1 ExpectedException (org.junit.rules.ExpectedException)1 Mockito.doAnswer (org.mockito.Mockito.doAnswer)1 Mockito.mock (org.mockito.Mockito.mock)1