Search in sources :

Example 1 with Transaction

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

the class AsyncDelegateMessageProcessorTestCase method processWithTx.

@Test
public void processWithTx() throws Exception {
    Transaction transaction = new TestTransaction(muleContext);
    TransactionCoordination.getInstance().bindTransaction(transaction);
    try {
        CoreEvent request = testEvent();
        CoreEvent result = process(messageProcessor, request);
        // Wait until processor in async is executed to allow assertions on sensed event
        asyncEntryLatch.countDown();
        assertThat(latch.await(LOCK_TIMEOUT, MILLISECONDS), is(true));
        assertTargetEvent(request);
        assertResponse(result);
    } finally {
        TransactionCoordination.getInstance().unbindTransaction(transaction);
    }
}
Also used : TestTransaction(org.mule.tck.testmodels.mule.TestTransaction) Transaction(org.mule.runtime.core.api.transaction.Transaction) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) TestTransaction(org.mule.tck.testmodels.mule.TestTransaction) Test(org.junit.Test)

Example 2 with Transaction

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

the class BeginAndResolveTransactionInterceptor method execute.

@Override
public T execute(ExecutionCallback<T> callback, ExecutionContext executionContext) throws Exception {
    byte action = transactionConfig.getAction();
    int timeout = transactionConfig.getTimeout();
    boolean resolveStartedTransaction = false;
    Transaction tx = TransactionCoordination.getInstance().getTransaction();
    if (action == TransactionConfig.ACTION_ALWAYS_BEGIN || (action == TransactionConfig.ACTION_BEGIN_OR_JOIN && tx == null)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Beginning transaction");
        }
        executionContext.markTransactionStart();
        tx = transactionConfig.getFactory().beginTransaction(muleContext);
        // Timeout is a traversal attribute of all Transaction implementations.
        // Setting it up here for all of them rather than in every implementation.
        tx.setTimeout(timeout);
        resolveStartedTransaction = true;
        if (logger.isDebugEnabled()) {
            logger.debug("Transaction successfully started: " + tx);
        }
    }
    T result;
    try {
        result = next.execute(callback, executionContext);
        resolveTransactionIfRequired(resolveStartedTransaction);
        return result;
    } catch (MessagingException e) {
        if (processOnException) {
            resolveTransactionIfRequired(resolveStartedTransaction || mustResolveAnyTransaction);
        }
        throw e;
    }
}
Also used : Transaction(org.mule.runtime.core.api.transaction.Transaction) MessagingException(org.mule.runtime.core.internal.exception.MessagingException)

Example 3 with Transaction

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

the class ResolvePreviousTransactionInterceptor method execute.

@Override
public T execute(ExecutionCallback<T> callback, ExecutionContext executionContext) throws Exception {
    byte action = transactionConfig.getAction();
    Transaction transactionBeforeTemplate = TransactionCoordination.getInstance().getTransaction();
    if ((action == TransactionConfig.ACTION_NONE || action == TransactionConfig.ACTION_ALWAYS_BEGIN) && transactionBeforeTemplate != null) {
        if (logger.isDebugEnabled()) {
            logger.debug(action + ", " + "current TX: " + transactionBeforeTemplate);
        }
        resolveTransaction();
    }
    return next.execute(callback, executionContext);
}
Also used : Transaction(org.mule.runtime.core.api.transaction.Transaction)

Example 4 with Transaction

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

the class PetStoreRetryPolicyProviderConnectionTestCase method createTransactionMock.

private Transaction createTransactionMock() throws TransactionException {
    Transaction transaction = mock(Transaction.class);
    doAnswer((invocationOnMock -> {
        TransactionCoordination.getInstance().bindTransaction(transaction);
        return null;
    })).when(transaction).begin();
    return transaction;
}
Also used : AbstractExtensionFunctionalTestCase(org.mule.test.module.extension.AbstractExtensionFunctionalTestCase) ErrorTypeMatcher.errorType(org.mule.tck.junit4.matcher.ErrorTypeMatcher.errorType) Thread.currentThread(java.lang.Thread.currentThread) Assert.assertThat(org.junit.Assert.assertThat) Every.everyItem(org.hamcrest.core.Every.everyItem) UNIT_TEST_THREAD_GROUP(org.mule.tck.SimpleUnitTestSupportSchedulerService.UNIT_TEST_THREAD_GROUP) CONNECTIVITY_ERROR_IDENTIFIER(org.mule.runtime.core.api.exception.Errors.Identifiers.CONNECTIVITY_ERROR_IDENTIFIER) ACTION_ALWAYS_BEGIN(org.mule.functional.api.flow.TransactionConfigEnum.ACTION_ALWAYS_BEGIN) Mockito.doAnswer(org.mockito.Mockito.doAnswer) PetStoreOperationsWithFailures.resetConnectionThreads(org.mule.test.petstore.extension.PetStoreOperationsWithFailures.resetConnectionThreads) After(org.junit.After) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ExpectedException(org.junit.rules.ExpectedException) Collectors.toSet(java.util.stream.Collectors.toSet) Before(org.junit.Before) PetStoreOperationsWithFailures.getConnectionThreads(org.mule.test.petstore.extension.PetStoreOperationsWithFailures.getConnectionThreads) TestTransactionFactory(org.mule.tck.testmodels.mule.TestTransactionFactory) Set(java.util.Set) Test(org.junit.Test) TransactionCoordination(org.mule.runtime.core.api.transaction.TransactionCoordination) Transaction(org.mule.runtime.core.api.transaction.Transaction) Rule(org.junit.Rule) TransactionException(org.mule.runtime.api.tx.TransactionException) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) Mockito.mock(org.mockito.Mockito.mock) Transaction(org.mule.runtime.core.api.transaction.Transaction)

Example 5 with Transaction

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

the class OnErrorContinueHandlerTestCase method before.

@Override
@Before
public void before() throws Exception {
    super.before();
    Transaction currentTransaction = TransactionCoordination.getInstance().getTransaction();
    if (currentTransaction != null) {
        TransactionCoordination.getInstance().unbindTransaction(currentTransaction);
    }
    onErrorContinueHandler = new OnErrorContinueHandler();
    onErrorContinueHandler.setAnnotations(getFlowComponentLocationAnnotations(flow.getName()));
    onErrorContinueHandler.setMuleContext(muleContext);
    onErrorContinueHandler.setNotificationFirer(mock(NotificationDispatcher.class));
}
Also used : TestTransaction(org.mule.tck.testmodels.mule.TestTransaction) Transaction(org.mule.runtime.core.api.transaction.Transaction) NotificationDispatcher(org.mule.runtime.api.notification.NotificationDispatcher) 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