Search in sources :

Example 6 with XaTransaction

use of org.mule.runtime.core.privileged.transaction.XaTransaction in project mule by mulesoft.

the class XaTransactionFactory method beginTransaction.

public Transaction beginTransaction(MuleContext muleContext) throws TransactionException {
    try {
        XaTransaction xat = new XaTransaction(muleContext);
        xat.setTimeout(timeout);
        xat.begin();
        return xat;
    } catch (Exception e) {
        throw new TransactionException(CoreMessages.cannotStartTransaction("XA"), e);
    }
}
Also used : ExternalXaTransaction(org.mule.runtime.core.internal.transaction.ExternalXaTransaction) XaTransaction(org.mule.runtime.core.privileged.transaction.XaTransaction) TransactionException(org.mule.runtime.api.tx.TransactionException) TransactionException(org.mule.runtime.api.tx.TransactionException)

Example 7 with XaTransaction

use of org.mule.runtime.core.privileged.transaction.XaTransaction in project mule by mulesoft.

the class ExtensionConnectionSupplierTestCase method xaTransaction.

@Test
public void xaTransaction() throws Exception {
    muleContext.setTransactionManager(mock(TransactionManager.class, RETURNS_DEEP_STUBS));
    XaTransaction transaction = spy(new XaTransaction(muleContext));
    XATransactionalConnection connection = mock(XATransactionalConnection.class, RETURNS_DEEP_STUBS);
    Object config = new Object();
    ExecutionContextAdapter operationContext = mock(ExecutionContextAdapter.class, RETURNS_DEEP_STUBS);
    ConnectionProvider connectionProvider = mock(ConnectionProvider.class);
    ConfigurationInstance configurationInstance = mock(ConfigurationInstance.class);
    when(configurationInstance.getConnectionProvider()).thenReturn(of(connectionProvider));
    when(operationContext.getConfiguration()).thenReturn(of(configurationInstance));
    when(configurationInstance.getValue()).thenReturn(config);
    when(connectionProvider.connect()).thenReturn(connection);
    TransactionConfig transactionConfig = mock(TransactionConfig.class);
    when(transactionConfig.getAction()).thenReturn(ACTION_ALWAYS_JOIN);
    when(transactionConfig.isTransacted()).thenReturn(true);
    when(operationContext.getTransactionConfig()).thenReturn(of(transactionConfig));
    connectionManager.bind(config, connectionProvider);
    TransactionCoordination.getInstance().bindTransaction(transaction);
    adapter.getConnection(operationContext);
    verify(transaction).bindResource(any(), any(XAExtensionTransactionalResource.class));
}
Also used : XaTransaction(org.mule.runtime.core.privileged.transaction.XaTransaction) XAExtensionTransactionalResource(org.mule.runtime.module.extension.internal.runtime.transaction.XAExtensionTransactionalResource) TransactionManager(javax.transaction.TransactionManager) TransactionConfig(org.mule.runtime.core.api.transaction.TransactionConfig) XATransactionalConnection(org.mule.runtime.extension.api.connectivity.XATransactionalConnection) ExecutionContextAdapter(org.mule.runtime.module.extension.api.runtime.privileged.ExecutionContextAdapter) ConnectionProvider(org.mule.runtime.api.connection.ConnectionProvider) ConfigurationInstance(org.mule.runtime.extension.api.runtime.config.ConfigurationInstance) Test(org.junit.Test)

Example 8 with XaTransaction

use of org.mule.runtime.core.privileged.transaction.XaTransaction in project mule by mulesoft.

the class AbstractTxThreadAssociationTestCase method testXaTransactionTermination.

/**
 * This is a former XaTransactionTestCase.
 *
 * @throws Exception in case of any error
 */
@Test
public void testXaTransactionTermination() 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);
    XaTransaction muleTx = new XaTransaction(muleContext);
    assertFalse(muleTx.isBegun());
    assertEquals(Status.STATUS_NO_TRANSACTION, muleTx.getStatus());
    muleTx.begin();
    assertTrue(muleTx.isBegun());
    muleTx.commit();
    Transaction jtaTx = tm.getTransaction();
    assertNull("Committing via TX Manager should have disassociated TX from the current thread.", jtaTx);
    assertEquals(Status.STATUS_NO_TRANSACTION, muleTx.getStatus());
}
Also used : XaTransaction(org.mule.runtime.core.privileged.transaction.XaTransaction) Transaction(javax.transaction.Transaction) XaTransaction(org.mule.runtime.core.privileged.transaction.XaTransaction) Test(org.junit.Test)

Example 9 with XaTransaction

use of org.mule.runtime.core.privileged.transaction.XaTransaction in project mule by mulesoft.

the class XaTransactionTestCase method setTxTimeoutWhenEnlistingResource.

@Test
public void setTxTimeoutWhenEnlistingResource() throws Exception {
    javax.transaction.Transaction tx = mock(javax.transaction.Transaction.class);
    when(mockTransactionManager.getTransaction()).thenReturn(tx);
    XaTransaction xaTransaction = new XaTransaction(mockMuleContext);
    int timeoutValue = 1500;
    int timeoutValueInSeconds = 1500 / 1000;
    xaTransaction.setTimeout(timeoutValue);
    xaTransaction.begin();
    xaTransaction.enlistResource(mockXaResource);
    verify(mockXaResource).setTransactionTimeout(timeoutValueInSeconds);
}
Also used : XaTransaction(org.mule.runtime.core.privileged.transaction.XaTransaction) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Aggregations

XaTransaction (org.mule.runtime.core.privileged.transaction.XaTransaction)9 Test (org.junit.Test)6 SmallTest (org.mule.tck.size.SmallTest)4 TransactionManager (javax.transaction.TransactionManager)2 TransactionException (org.mule.runtime.api.tx.TransactionException)2 ExternalXaTransaction (org.mule.runtime.core.internal.transaction.ExternalXaTransaction)2 Transaction (javax.transaction.Transaction)1 XAResource (javax.transaction.xa.XAResource)1 InOrder (org.mockito.InOrder)1 ConnectionProvider (org.mule.runtime.api.connection.ConnectionProvider)1 TransactionConfig (org.mule.runtime.core.api.transaction.TransactionConfig)1 XATransactionalConnection (org.mule.runtime.extension.api.connectivity.XATransactionalConnection)1 ConfigurationInstance (org.mule.runtime.extension.api.runtime.config.ConfigurationInstance)1 ExecutionContextAdapter (org.mule.runtime.module.extension.api.runtime.privileged.ExecutionContextAdapter)1 XAExtensionTransactionalResource (org.mule.runtime.module.extension.internal.runtime.transaction.XAExtensionTransactionalResource)1