Search in sources :

Example 1 with XaTransactionFactory

use of org.mule.runtime.core.privileged.transaction.xa.XaTransactionFactory 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());
}
Also used : ExecutionTemplate(org.mule.runtime.core.api.execution.ExecutionTemplate) TransactionalExecutionTemplate(org.mule.runtime.core.api.execution.TransactionalExecutionTemplate) Transaction(javax.transaction.Transaction) XaTransaction(org.mule.runtime.core.privileged.transaction.XaTransaction) XaTransactionFactory(org.mule.runtime.core.privileged.transaction.xa.XaTransactionFactory) TransactionConfig(org.mule.runtime.core.api.transaction.TransactionConfig) MuleTransactionConfig(org.mule.runtime.core.api.transaction.MuleTransactionConfig) MuleTransactionConfig(org.mule.runtime.core.api.transaction.MuleTransactionConfig) ExecutionCallback(org.mule.runtime.core.api.execution.ExecutionCallback) Test(org.junit.Test)

Example 2 with XaTransactionFactory

use of org.mule.runtime.core.privileged.transaction.xa.XaTransactionFactory 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());
}
Also used : TransactionConfig(org.mule.runtime.core.api.transaction.TransactionConfig) MuleTransactionConfig(org.mule.runtime.core.api.transaction.MuleTransactionConfig) MuleTransactionConfig(org.mule.runtime.core.api.transaction.MuleTransactionConfig) ExecutionTemplate(org.mule.runtime.core.api.execution.ExecutionTemplate) TransactionalExecutionTemplate(org.mule.runtime.core.api.execution.TransactionalExecutionTemplate) Transaction(javax.transaction.Transaction) XaTransaction(org.mule.runtime.core.privileged.transaction.XaTransaction) XaTransactionFactory(org.mule.runtime.core.privileged.transaction.xa.XaTransactionFactory) ExecutionCallback(org.mule.runtime.core.api.execution.ExecutionCallback) Test(org.junit.Test)

Example 3 with XaTransactionFactory

use of org.mule.runtime.core.privileged.transaction.xa.XaTransactionFactory 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;
                }
            });
        }
    });
}
Also used : ExecutionTemplate(org.mule.runtime.core.api.execution.ExecutionTemplate) TransactionalExecutionTemplate(org.mule.runtime.core.api.execution.TransactionalExecutionTemplate) XaTransactionFactory(org.mule.runtime.core.privileged.transaction.xa.XaTransactionFactory) TransactionConfig(org.mule.runtime.core.api.transaction.TransactionConfig) MuleTransactionConfig(org.mule.runtime.core.api.transaction.MuleTransactionConfig) MuleTransactionConfig(org.mule.runtime.core.api.transaction.MuleTransactionConfig) ExecutionCallback(org.mule.runtime.core.api.execution.ExecutionCallback) Test(org.junit.Test)

Example 4 with XaTransactionFactory

use of org.mule.runtime.core.privileged.transaction.xa.XaTransactionFactory 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)

Aggregations

Test (org.junit.Test)4 XaTransactionFactory (org.mule.runtime.core.privileged.transaction.xa.XaTransactionFactory)4 ExecutionCallback (org.mule.runtime.core.api.execution.ExecutionCallback)3 ExecutionTemplate (org.mule.runtime.core.api.execution.ExecutionTemplate)3 TransactionalExecutionTemplate (org.mule.runtime.core.api.execution.TransactionalExecutionTemplate)3 MuleTransactionConfig (org.mule.runtime.core.api.transaction.MuleTransactionConfig)3 TransactionConfig (org.mule.runtime.core.api.transaction.TransactionConfig)3 Transaction (javax.transaction.Transaction)2 XaTransaction (org.mule.runtime.core.privileged.transaction.XaTransaction)2 TransactionManager (javax.transaction.TransactionManager)1 MuleContext (org.mule.runtime.core.api.MuleContext)1 Transaction (org.mule.runtime.core.api.transaction.Transaction)1