Search in sources :

Example 71 with XidImple

use of com.arjuna.ats.jta.xa.XidImple in project narayana by jbosstm.

the class XATerminatorUnitTest method testImportMultipleTx.

@Test
public void testImportMultipleTx() throws XAException, RollbackException, SystemException {
    Implementations.initialise();
    XidImple xid = new XidImple(new Uid());
    TransactionImporter imp = SubordinationManager.getTransactionImporter();
    SubordinateTransaction subordinateTransaction = imp.importTransaction(xid);
    XATerminatorImple xa = new XATerminatorImple();
    XAResourceImple xar1 = new XAResourceImple(XAResource.XA_OK, XAResource.XA_OK);
    XAResourceImple xar2 = new XAResourceImple(XAResource.XA_OK, XAException.XAER_RMFAIL);
    subordinateTransaction.enlistResource(xar1);
    subordinateTransaction.enlistResource(xar2);
    xa.prepare(xid);
    try {
        xa.commit(xid, false);
        fail("Did not expect to pass");
    } catch (XAException xae) {
        assertTrue(xae.errorCode == XAException.XAER_RMFAIL);
    }
    XARecoveryModule xarm = new XARecoveryModule();
    xarm.addXAResourceRecoveryHelper(new XAResourceRecoveryHelper() {

        @Override
        public boolean initialise(String p) throws Exception {
            return false;
        }

        @Override
        public XAResource[] getXAResources() throws Exception {
            return new XAResource[] { xar2 };
        }
    });
    RecoveryManager.manager().addModule(xarm);
    Xid[] xids = xa.recover(XAResource.TMSTARTRSCAN);
    assertTrue(Arrays.binarySearch(xids, xid, new Comparator<Xid>() {

        @Override
        public int compare(Xid o1, Xid o2) {
            if (((XidImple) o1).equals(o2)) {
                return 0;
            } else {
                return -1;
            }
        }
    }) != -1);
    xa.rollback(xid);
    assertTrue(xar2.rollbackCalled());
    xa.recover(XAResource.TMENDRSCAN);
}
Also used : XidImple(com.arjuna.ats.jta.xa.XidImple) XAException(javax.transaction.xa.XAException) XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper) UnexpectedConditionException(com.arjuna.ats.jta.exceptions.UnexpectedConditionException) RollbackException(javax.transaction.RollbackException) SystemException(javax.transaction.SystemException) XAException(javax.transaction.xa.XAException) Uid(com.arjuna.ats.arjuna.common.Uid) Xid(javax.transaction.xa.Xid) TransactionImporter(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.TransactionImporter) XATerminatorImple(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.XATerminatorImple) SubordinateTransaction(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateTransaction) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) Test(org.junit.Test)

Example 72 with XidImple

use of com.arjuna.ats.jta.xa.XidImple in project narayana by jbosstm.

the class XATerminatorUnitTest method testConcurrentImport.

@Test
public void testConcurrentImport() throws Exception {
    AtomicInteger completionCount = new AtomicInteger(0);
    XidImple xid = new XidImple(new Uid());
    final int TASK_COUNT = 400;
    final int THREAD_COUNT = 200;
    final CyclicBarrier gate = new CyclicBarrier(THREAD_COUNT + 1);
    ArrayList<CompletableFuture<SubordinateTransaction>> futures = new ArrayList<>();
    ExecutorService executor = Executors.newFixedThreadPool(THREAD_COUNT);
    for (int i = 0; i < TASK_COUNT; i++) futures.add(doAsync(completionCount, gate, i < THREAD_COUNT, executor, xid));
    gate.await();
    SubordinateTransaction prevStx = null;
    for (CompletableFuture<SubordinateTransaction> future : futures) {
        SubordinateTransaction stx = future.get();
        if (stx == null) {
            fail("transaction import returned null for future ");
        } else {
            if (prevStx != null)
                assertEquals("transaction import for same xid returned a different instance", stx, prevStx);
            else
                prevStx = stx;
        }
    }
    assertEquals("some transaction import futures did not complete", completionCount.get(), TASK_COUNT);
}
Also used : XidImple(com.arjuna.ats.jta.xa.XidImple) Uid(com.arjuna.ats.arjuna.common.Uid) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) ExecutorService(java.util.concurrent.ExecutorService) SubordinateTransaction(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateTransaction) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 73 with XidImple

use of com.arjuna.ats.jta.xa.XidImple in project narayana by jbosstm.

the class XATerminatorUnitTest method testRecovery.

@Test
public void testRecovery() throws Exception {
    Implementations.initialise();
    XATerminatorImple xa = new XATerminatorImple();
    Xid[] recover = xa.recover(XAResource.TMSTARTRSCAN);
    int initialLength = recover == null ? 0 : recover.length;
    xa.recover(XAResource.TMENDRSCAN);
    XidImple xid = new XidImple(new Uid());
    TransactionImporter imp = SubordinationManager.getTransactionImporter();
    SubordinateTransaction importTransaction = imp.importTransaction(xid);
    importTransaction.enlistResource(new XAResource() {

        @Override
        public void start(Xid xid, int flags) throws XAException {
        }

        @Override
        public void end(Xid xid, int flags) throws XAException {
        }

        @Override
        public int prepare(Xid xid) throws XAException {
            return 0;
        }

        @Override
        public void commit(Xid xid, boolean onePhase) throws XAException {
            throw new XAException(XAException.XA_RETRY);
        }

        @Override
        public void rollback(Xid xid) throws XAException {
        }

        @Override
        public void forget(Xid xid) throws XAException {
        }

        @Override
        public Xid[] recover(int flag) throws XAException {
            return null;
        }

        @Override
        public boolean isSameRM(XAResource xaRes) throws XAException {
            return false;
        }

        @Override
        public int getTransactionTimeout() throws XAException {
            return 0;
        }

        @Override
        public boolean setTransactionTimeout(int seconds) throws XAException {
            return false;
        }
    });
    assertTrue(xa.beforeCompletion(xid));
    assertEquals(xa.prepare(xid), XAResource.XA_OK);
    try {
        xa.commit(xid, false);
        fail();
    } catch (XAException e) {
        assertTrue(e.errorCode == XAException.XAER_RMFAIL);
    }
    Xid[] recover2 = xa.recover(XAResource.TMSTARTRSCAN);
    assertTrue(recover2.length == initialLength + 1);
    try {
        xa.commit(xid, false);
        fail();
    } catch (XAException e) {
        assertTrue("Wrong errorcode" + e.errorCode, e.errorCode == XAException.XAER_RMFAIL);
    }
    xa.recover(XAResource.TMENDRSCAN);
    // Feed the recovery manager with something it can recover with
    RecoveryModule module = new XARecoveryModule() {

        @Override
        public XAResource getNewXAResource(final XAResourceRecord xaResourceRecord) {
            return new XAResource() {

                @Override
                public void start(Xid xid, int flags) throws XAException {
                }

                @Override
                public void end(Xid xid, int flags) throws XAException {
                }

                @Override
                public int prepare(Xid xid) throws XAException {
                    return 0;
                }

                @Override
                public void commit(Xid xid, boolean onePhase) throws XAException {
                }

                @Override
                public void rollback(Xid xid) throws XAException {
                }

                @Override
                public void forget(Xid xid) throws XAException {
                }

                @Override
                public Xid[] recover(int flag) throws XAException {
                    return null;
                }

                @Override
                public boolean isSameRM(XAResource xaRes) throws XAException {
                    return false;
                }

                @Override
                public int getTransactionTimeout() throws XAException {
                    return 0;
                }

                @Override
                public boolean setTransactionTimeout(int seconds) throws XAException {
                    return false;
                }
            };
        }
    };
    RecoveryManager.manager().addModule(module);
    try {
        Xid[] recover3 = xa.recover(XAResource.TMSTARTRSCAN);
        assertTrue(recover3.length == recover2.length);
        xa.commit(xid, false);
        xa.recover(XAResource.TMENDRSCAN);
        Xid[] recover4 = xa.recover(XAResource.TMSTARTRSCAN);
        assertTrue(recover4 == null || recover4.length == initialLength);
        xa.recover(XAResource.TMENDRSCAN);
    } finally {
        RecoveryManager.manager().removeModule(module, false);
    }
}
Also used : XidImple(com.arjuna.ats.jta.xa.XidImple) XAException(javax.transaction.xa.XAException) XAResourceRecord(com.arjuna.ats.internal.jta.resources.arjunacore.XAResourceRecord) Uid(com.arjuna.ats.arjuna.common.Uid) Xid(javax.transaction.xa.Xid) FailureXAResource(com.hp.mwtests.ts.jta.common.FailureXAResource) XAResource(javax.transaction.xa.XAResource) TransactionImporter(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.TransactionImporter) XATerminatorImple(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.XATerminatorImple) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) RecoveryModule(com.arjuna.ats.arjuna.recovery.RecoveryModule) SubordinateTransaction(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateTransaction) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) Test(org.junit.Test)

Example 74 with XidImple

use of com.arjuna.ats.jta.xa.XidImple in project narayana by jbosstm.

the class XATerminatorUnitTest method testFail.

@Test
public void testFail() throws Exception {
    XATerminatorImple term = new XATerminatorImple();
    XidImple xid = new XidImple(new Uid());
    SubordinationManager.getTransactionImporter().importTransaction(xid);
    SubordinateTransaction tx = SubordinationManager.getTransactionImporter().getImportedTransaction(xid);
    tx.enlistResource(new FailureXAResource(FailLocation.commit, FailType.rollback));
    try {
        term.commit(xid, false);
        fail();
    } catch (final XAException ex) {
    }
    xid = new XidImple(new Uid());
    SubordinationManager.getTransactionImporter().importTransaction(xid);
    tx = SubordinationManager.getTransactionImporter().getImportedTransaction(xid);
    tx.enlistResource(new FailureXAResource(FailLocation.commit, FailType.heurcom));
    try {
        term.commit(xid, false);
        fail();
    } catch (final XAException ex) {
    }
    xid = new XidImple(new Uid());
    SubordinationManager.getTransactionImporter().importTransaction(xid);
    tx = SubordinationManager.getTransactionImporter().getImportedTransaction(xid);
    tx.enlistResource(new FailureXAResource(FailLocation.commit, FailType.heurcom));
    term.prepare(xid);
    try {
        term.commit(xid, false);
    } catch (final XAException ex) {
        fail();
    }
    xid = new XidImple(new Uid());
    SubordinationManager.getTransactionImporter().importTransaction(xid);
    tx = SubordinationManager.getTransactionImporter().getImportedTransaction(xid);
    tx.enlistResource(new FailureXAResource(FailLocation.commit, FailType.normal));
    try {
        term.commit(xid, false);
        fail();
    } catch (final XAException ex) {
    }
    xid = new XidImple(new Uid());
    SubordinationManager.getTransactionImporter().importTransaction(xid);
    tx = SubordinationManager.getTransactionImporter().getImportedTransaction(xid);
    tx.enlistResource(new FailureXAResource(FailLocation.rollback, FailType.rollback));
    try {
        term.rollback(xid);
    } catch (final XAException ex) {
        fail();
    }
    xid = new XidImple(new Uid());
    SubordinationManager.getTransactionImporter().importTransaction(xid);
    tx = SubordinationManager.getTransactionImporter().getImportedTransaction(xid);
    tx.enlistResource(new FailureXAResource(FailLocation.rollback, FailType.heurcom));
    term.prepare(xid);
    try {
        term.rollback(xid);
        fail();
    } catch (final XAException ex) {
    }
    xid = new XidImple(new Uid());
    SubordinationManager.getTransactionImporter().importTransaction(xid);
    tx = SubordinationManager.getTransactionImporter().getImportedTransaction(xid);
    tx.enlistResource(new FailureXAResource(FailLocation.rollback, FailType.normal));
    term.prepare(xid);
    try {
        term.rollback(xid);
        fail();
    } catch (final XAException ex) {
    }
    xid = new XidImple(new Uid());
    SubordinationManager.getTransactionImporter().importTransaction(xid);
    tx = SubordinationManager.getTransactionImporter().getImportedTransaction(xid);
    tx.enlistResource(new FailureXAResource(FailLocation.prepare_and_rollback, FailType.normal));
    try {
        term.prepare(xid);
        fail();
    } catch (final XAException ex) {
    }
    xid = new XidImple(new Uid());
    SubordinationManager.getTransactionImporter().importTransaction(xid);
    tx = SubordinationManager.getTransactionImporter().getImportedTransaction(xid);
    tx.enlistResource(new FailureXAResource(FailLocation.prepare_and_rollback, FailType.heurcom));
    try {
        term.prepare(xid);
        fail();
    } catch (final XAException ex) {
    }
    xid = new XidImple(new Uid());
    SubordinationManager.getTransactionImporter().importTransaction(xid);
    tx = SubordinationManager.getTransactionImporter().getImportedTransaction(xid);
    tx.enlistResource(new FailureXAResource(FailLocation.prepare_and_rollback, FailType.rollback));
    try {
        term.prepare(xid);
        fail();
    } catch (final XAException ex) {
    }
}
Also used : XidImple(com.arjuna.ats.jta.xa.XidImple) Uid(com.arjuna.ats.arjuna.common.Uid) XAException(javax.transaction.xa.XAException) FailureXAResource(com.hp.mwtests.ts.jta.common.FailureXAResource) XATerminatorImple(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.XATerminatorImple) SubordinateTransaction(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateTransaction) Test(org.junit.Test)

Example 75 with XidImple

use of com.arjuna.ats.jta.xa.XidImple in project narayana by jbosstm.

the class XATerminatorUnitTest method testInvalid.

@Test
public void testInvalid() throws Exception {
    XATerminatorImple term = new XATerminatorImple();
    XidImple xid = new XidImple(new Uid());
    try {
        SubordinationManager.getTransactionImporter().importTransaction(null);
        fail();
    } catch (final IllegalArgumentException ex) {
    }
    try {
        SubordinationManager.getTransactionImporter().recoverTransaction(null);
        fail();
    } catch (final IllegalArgumentException ex) {
    }
    try {
        SubordinationManager.getTransactionImporter().getImportedTransaction(null);
        fail();
    } catch (final IllegalArgumentException ex) {
    }
    try {
        SubordinationManager.getTransactionImporter().removeImportedTransaction(null);
        fail();
    } catch (final IllegalArgumentException ex) {
    }
    Uid uid = new Uid();
    try {
        Object obj = SubordinationManager.getTransactionImporter().recoverTransaction(uid);
        fail();
    } catch (IllegalArgumentException ex) {
    }
}
Also used : XidImple(com.arjuna.ats.jta.xa.XidImple) Uid(com.arjuna.ats.arjuna.common.Uid) XATerminatorImple(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.XATerminatorImple) Test(org.junit.Test)

Aggregations

XidImple (com.arjuna.ats.jta.xa.XidImple)90 Uid (com.arjuna.ats.arjuna.common.Uid)71 Test (org.junit.Test)71 Xid (javax.transaction.xa.Xid)35 XAException (javax.transaction.xa.XAException)28 SubordinateTransaction (com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateTransaction)27 Transaction (javax.transaction.Transaction)16 XATerminator (javax.resource.spi.XATerminator)15 TransactionImporter (com.arjuna.ats.internal.jta.transaction.arjunacore.jca.TransactionImporter)12 XATerminatorImple (com.arjuna.ats.internal.jta.transaction.jts.jca.XATerminatorImple)10 IOException (java.io.IOException)10 InputObjectState (com.arjuna.ats.arjuna.state.InputObjectState)9 XAOnePhaseResource (com.arjuna.ats.internal.jta.resources.arjunacore.XAOnePhaseResource)9 XAResourceRecord (com.arjuna.ats.internal.jta.resources.arjunacore.XAResourceRecord)9 XARecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule)8 SampleOnePhaseResource (com.hp.mwtests.ts.jta.common.SampleOnePhaseResource)8 RollbackException (javax.transaction.RollbackException)8 SystemException (javax.transaction.SystemException)8 XAResource (javax.transaction.xa.XAResource)8 OutputObjectState (com.arjuna.ats.arjuna.state.OutputObjectState)7