Search in sources :

Example 11 with TransactionImporter

use of com.arjuna.ats.internal.jta.transaction.arjunacore.jca.TransactionImporter 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 12 with TransactionImporter

use of com.arjuna.ats.internal.jta.transaction.arjunacore.jca.TransactionImporter 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 13 with TransactionImporter

use of com.arjuna.ats.internal.jta.transaction.arjunacore.jca.TransactionImporter in project narayana by jbosstm.

the class XATerminatorImpleUnitTest method testMoveToAssumedComplete.

@Test
public void testMoveToAssumedComplete() throws Exception {
    Implementations.initialise();
    Implementationsx.initialise();
    Uid uid = new Uid();
    Xid xid = XidUtils.getXid(uid, false);
    XATerminatorImple xaTerminator = new XATerminatorImple();
    TransactionImporter importer = SubordinationManager.getTransactionImporter();
    SubordinateTransaction subordinateTransaction = importer.importTransaction(xid);
    Uid subordinateTransactionUid = getImportedSubordinateTransactionUid(subordinateTransaction);
    com.hp.mwtests.ts.jta.subordinate.TestXAResource xares = new com.hp.mwtests.ts.jta.subordinate.TestXAResource();
    xares.setCommitException(new XAException(XAException.XAER_RMFAIL));
    subordinateTransaction.enlistResource(xares);
    subordinateTransaction.doPrepare();
    boolean commitFailed = subordinateTransaction.doCommit();
    Assert.assertFalse("Commit should fail as XAResource defined XAException on commit being thrown", commitFailed);
    int assumedCompletedRetryOriginalValue = jtsPropertyManager.getJTSEnvironmentBean().getCommitedTransactionRetryLimit();
    jtsPropertyManager.getJTSEnvironmentBean().setCommitedTransactionRetryLimit(1);
    try {
        SubordinateTransaction recoveredTxn = importer.recoverTransaction(subordinateTransactionUid);
        Xid xidRecovered = recoveredTxn.baseXid();
        Assert.assertEquals("recovered subordinate xid should be equal to imported one", xid, xidRecovered);
        Runnable runCommitExpectingException = () -> {
            try {
                // importing transaction
                xaTerminator.recover(XAResource.TMSTARTRSCAN);
                xaTerminator.recover(XAResource.TMENDRSCAN);
                // try to commit the imported transaction
                xaTerminator.commit(xid, false);
                Assert.fail("XAException is expected to be thrown as txn was not yet moved to assumed complete state");
            } catch (XAException expected) {
                Assert.assertTrue("Commit expect to throw exception indicating that othe commit call is expected, but error code is " + expected.errorCode, XAException.XA_RETRY == expected.errorCode || XAException.XAER_RMFAIL == expected.errorCode);
            }
        };
        // replay first
        runCommitExpectingException.run();
        // assume completed check first time
        runCommitExpectingException.run();
        // importing transaction
        xaTerminator.recover(XAResource.TMSTARTRSCAN);
        xaTerminator.recover(XAResource.TMENDRSCAN);
        // moving to assumed completed state
        xaTerminator.commit(xid, false);
    } finally {
        jtsPropertyManager.getJTSEnvironmentBean().setCommitedTransactionRetryLimit(assumedCompletedRetryOriginalValue);
    }
    try {
        importer.recoverTransaction(subordinateTransactionUid);
        Assert.fail("Transaction '" + subordinateTransaction + "' should fail to recover as it should be moved " + "to category AssumedCompleteServerTrasactions");
    } catch (IllegalArgumentException expected) {
    }
    ObjectStoreIterator objectStoreIterator = new ObjectStoreIterator(StoreManager.getRecoveryStore(), AssumedCompleteServerTransaction.typeName());
    List<Uid> assumedCompletedUids = new ArrayList<Uid>();
    Uid iteratedUid = objectStoreIterator.iterate();
    while (Uid.nullUid().notEquals(iteratedUid)) {
        assumedCompletedUids.add(iteratedUid);
        iteratedUid = objectStoreIterator.iterate();
    }
    Assert.assertTrue("the subordinate transaction has to be moved under assumed completed in object store", assumedCompletedUids.contains(subordinateTransactionUid));
}
Also used : TestXAResource(com.hp.mwtests.ts.jta.jts.TestXAResource) XAException(javax.transaction.xa.XAException) ObjectStoreIterator(com.arjuna.ats.arjuna.objectstore.ObjectStoreIterator) ArrayList(java.util.ArrayList) 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.jts.jca.XATerminatorImple) SubordinateTransaction(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateTransaction) Test(org.junit.Test)

Example 14 with TransactionImporter

use of com.arjuna.ats.internal.jta.transaction.arjunacore.jca.TransactionImporter in project narayana by jbosstm.

the class XATerminatorImpleUnitTest method testPrepareAbort.

@Test
public void testPrepareAbort() throws Exception {
    XidImple xid = new XidImple(new Uid());
    TransactionImporter imp = SubordinationManager.getTransactionImporter();
    imp.importTransaction(xid);
    XATerminatorImple xa = new XATerminatorImple();
    assertEquals(xa.prepare(xid), XAResource.XA_RDONLY);
    try {
        xa.rollback(xid);
    } 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) TransactionImporter(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.TransactionImporter) XATerminatorImple(com.arjuna.ats.internal.jta.transaction.jts.jca.XATerminatorImple) Test(org.junit.Test)

Example 15 with TransactionImporter

use of com.arjuna.ats.internal.jta.transaction.arjunacore.jca.TransactionImporter in project narayana by jbosstm.

the class XATerminatorImpleUnitTest method testImportMultipleTx.

@Test
public void testImportMultipleTx() throws XAException, RollbackException, SystemException {
    Implementations.initialise();
    Implementationsx.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);
    XAResourceImple xar2 = new XAResourceImple(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);
    }
    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);
    // Will throw a heuristic. The doRollback should be allowed but when we realise that the XAR1 is commited it shouldn't be allowed. Maybe we should also not shutdown the first XAR or maybe we need to rely on bottom up.
    xa.rollback(xid);
    assertTrue(xar2.rollbackCalled());
    xa.recover(XAResource.TMENDRSCAN);
}
Also used : XidImple(com.arjuna.ats.jta.xa.XidImple) Uid(com.arjuna.ats.arjuna.common.Uid) Xid(javax.transaction.xa.Xid) XAException(javax.transaction.xa.XAException) TransactionImporter(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.TransactionImporter) XATerminatorImple(com.arjuna.ats.internal.jta.transaction.jts.jca.XATerminatorImple) SubordinateTransaction(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateTransaction) Test(org.junit.Test)

Aggregations

TransactionImporter (com.arjuna.ats.internal.jta.transaction.arjunacore.jca.TransactionImporter)15 Uid (com.arjuna.ats.arjuna.common.Uid)14 Test (org.junit.Test)14 XATerminatorImple (com.arjuna.ats.internal.jta.transaction.jts.jca.XATerminatorImple)12 XidImple (com.arjuna.ats.jta.xa.XidImple)12 XAException (javax.transaction.xa.XAException)10 Xid (javax.transaction.xa.Xid)8 SubordinateTransaction (com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateTransaction)7 RollbackException (javax.transaction.RollbackException)4 SystemException (javax.transaction.SystemException)4 TestXAResource (com.hp.mwtests.ts.jta.jts.TestXAResource)3 XARecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule)2 XATerminatorImple (com.arjuna.ats.internal.jta.transaction.arjunacore.jca.XATerminatorImple)2 XAResourceRecoveryHelper (com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper)2 XAResource (javax.transaction.xa.XAResource)2 ObjectStoreIterator (com.arjuna.ats.arjuna.objectstore.ObjectStoreIterator)1 RecoveryModule (com.arjuna.ats.arjuna.recovery.RecoveryModule)1 ObjStoreBrowser (com.arjuna.ats.arjuna.tools.osb.mbean.ObjStoreBrowser)1 XARecoveryModule (com.arjuna.ats.internal.jta.recovery.jts.XARecoveryModule)1 XAResourceRecord (com.arjuna.ats.internal.jta.resources.arjunacore.XAResourceRecord)1