Search in sources :

Example 6 with XATerminatorImple

use of com.arjuna.ats.internal.jta.transaction.jts.jca.XATerminatorImple in project narayana by jbosstm.

the class XATerminatorImpleUnitTest method testPrepareCommit.

@Test
public void testPrepareCommit() throws Exception {
    XidImple xid = new XidImple(new Uid());
    TransactionImporter imp = SubordinationManager.getTransactionImporter();
    imp.importTransaction(xid);
    XATerminatorImple xa = new XATerminatorImple();
    assertTrue(xa.beforeCompletion(xid));
    assertEquals(xa.prepare(xid), XAResource.XA_RDONLY);
    try {
        xa.commit(xid, false);
        fail();
    } catch (final XAException ex) {
    }
    imp.importTransaction(xid);
    xa.commit(xid, true);
}
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 7 with XATerminatorImple

use of com.arjuna.ats.internal.jta.transaction.jts.jca.XATerminatorImple in project narayana by jbosstm.

the class XATerminatorImpleUnitTest method testAbort.

@Test
public void testAbort() throws Exception {
    XidImple xid = new XidImple(new Uid());
    TransactionImporter imp = SubordinationManager.getTransactionImporter();
    imp.importTransaction(xid);
    XATerminatorImple xa = new XATerminatorImple();
    xa.rollback(xid);
}
Also used : XidImple(com.arjuna.ats.jta.xa.XidImple) Uid(com.arjuna.ats.arjuna.common.Uid) 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 8 with XATerminatorImple

use of com.arjuna.ats.internal.jta.transaction.jts.jca.XATerminatorImple in project narayana by jbosstm.

the class XATerminatorImpleUnitTest method testDoRecover.

@Test
public void testDoRecover() throws Exception {
    Implementations.initialise();
    Implementationsx.initialise();
    Xid xid1 = XidUtils.getXid(new Uid(), false);
    Xid xid2 = XidUtils.getXid(new Uid(), false);
    TransactionImporter imp = SubordinationManager.getTransactionImporter();
    SubordinateTransaction subTxn1 = imp.importTransaction(xid1);
    SubordinateTransaction subTxn2 = imp.importTransaction(xid2);
    XATerminatorImple xaTerminator = new XATerminatorImple();
    subTxn1.enlistResource(new XAResourceImple(XAResource.XA_OK));
    subTxn2.enlistResource(new XAResourceImple(XAResource.XA_OK));
    try {
        subTxn1.doPrepare();
        subTxn2.doPrepare();
        Xid[] xidsAll = xaTerminator.doRecover(null, null);
        Assert.assertNotNull("expecting some unfinished transactions to be returned but they are null", xidsAll);
        Assert.assertTrue("expecting some unfinished transactions to be returned but there is none", xidsAll.length > 1);
    } finally {
        subTxn1.doCommit();
        subTxn2.doCommit();
    }
}
Also used : 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 9 with XATerminatorImple

use of com.arjuna.ats.internal.jta.transaction.jts.jca.XATerminatorImple in project narayana by jbosstm.

the class XATerminatorImpleUnitTest method testXARMERR.

@Test
public void testXARMERR() throws Exception {
    Uid uid = new Uid();
    XidImple xid = new XidImple(uid);
    TransactionImporter imp = SubordinationManager.getTransactionImporter();
    SubordinateTransaction subordinateTransaction = imp.importTransaction(xid);
    Uid savingUid = getImportedSubordinateTransactionUid(subordinateTransaction);
    subordinateTransaction.enlistResource(new TestXAResource() {

        @Override
        public void commit(Xid xid, boolean b) throws XAException {
            this.xid = null;
        }

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

        @Override
        public void rollback(Xid xid) throws XAException {
            fail("Resource was rolled back");
        }
    });
    subordinateTransaction.enlistResource(new TestXAResource() {

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

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

        @Override
        public void rollback(Xid xid) throws XAException {
            fail("Resource was rolled back");
        }
    });
    XATerminatorImple xa = new XATerminatorImple();
    xa.prepare(xid);
    try {
        xa.commit(xid, false);
        fail("Expecting heuristic mixed exception being thrown on commit");
    } catch (final XAException ex) {
        assertEquals(XAException.XA_HEURMIX, ex.errorCode);
    }
    try {
        xa.commit(xid, false);
    } catch (XAException e) {
        assertEquals(XAException.XA_RETRY, e.errorCode);
    }
    ObjStoreBrowser osb = new ObjStoreBrowser();
    osb.viewSubordinateAtomicActions(true);
    osb.setExposeAllRecordsAsMBeans(true);
    osb.start();
    osb.probe();
    Set<ObjectName> participants = JMXServer.getAgent().queryNames("jboss.jta:type=ObjectStore,itype=" + com.arjuna.ats.internal.jta.transaction.jts.subordinate.jca.coordinator.ServerTransaction.getType().substring(1) + ",uid=" + savingUid.stringForm().replaceAll(":", "_") + ",puid=*", null);
    assertEquals(1, participants.size());
    JMXServer.getAgent().getServer().invoke(participants.iterator().next(), "clearHeuristic", null, null);
    xa.recover(XAResource.TMSTARTRSCAN);
    xa.recover(XAResource.TMENDRSCAN);
    Set<ObjectName> xaResourceRecords = JMXServer.getAgent().queryNames("jboss.jta:type=ObjectStore,itype=" + XAResourceRecord.typeName().substring(1) + ",uid=*", null);
    for (ObjectName xaResourceRecord : xaResourceRecords) {
        Object getGlobalTransactionId = JMXServer.getAgent().getServer().getAttribute(xaResourceRecord, "GlobalTransactionId");
        Object getBranchQualifier = JMXServer.getAgent().getServer().getAttribute(xaResourceRecord, "BranchQualifier");
        if (Arrays.equals(failedResourceXid.getGlobalTransactionId(), (byte[]) getGlobalTransactionId) && Arrays.equals(failedResourceXid.getBranchQualifier(), (byte[]) getBranchQualifier)) {
            Object getHeuristicValue = JMXServer.getAgent().getServer().getAttribute(xaResourceRecord, "HeuristicValue");
            assertTrue(getHeuristicValue.equals(new Integer(6)));
            JMXServer.getAgent().getServer().invoke(xaResourceRecord, "clearHeuristic", null, null);
        }
    }
    XARecoveryModule xaRecoveryModule = new XARecoveryModule();
    xaRecoveryModule.addXAResourceRecoveryHelper(new XAResourceRecoveryHelper() {

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

        @Override
        public XAResource[] getXAResources() throws Exception {
            return new XAResource[] { new TestXAResource() {

                public Xid[] recover(int var) throws XAException {
                    if (var == XAResource.TMSTARTRSCAN) {
                        if (failedResourceXid != null) {
                            return new Xid[] { failedResourceXid };
                        }
                    }
                    return new Xid[0];
                }

                @Override
                public void commit(Xid xid, boolean b) throws XAException {
                    failedResourceXid = null;
                }

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

                @Override
                public void rollback(Xid xid) throws XAException {
                    fail("Resource was rolled back");
                }
            } };
        }
    });
    xaRecoveryModule.periodicWorkFirstPass();
    Field safetyIntervalMillis = RecoveryXids.class.getDeclaredField("safetyIntervalMillis");
    safetyIntervalMillis.setAccessible(true);
    Object o1 = safetyIntervalMillis.get(null);
    safetyIntervalMillis.set(null, 0);
    try {
        xaRecoveryModule.periodicWorkSecondPass();
    } finally {
        safetyIntervalMillis.set(null, o1);
    }
    xa.recover(XAResource.TMSTARTRSCAN);
    try {
        xa.commit(xid, false);
        Assert.fail("Expecting XAException being thrown indicating more recover to be called");
    } catch (XAException expected) {
        Assert.assertTrue("On commit XAException error code indicating more recover call is expected but it's " + expected.errorCode, XAException.XA_RETRY == expected.errorCode || XAException.XAER_RMFAIL == expected.errorCode);
    } finally {
        xa.recover(XAResource.TMENDRSCAN);
    }
    assertNull(failedResourceXid);
}
Also used : XidImple(com.arjuna.ats.jta.xa.XidImple) TestXAResource(com.hp.mwtests.ts.jta.jts.TestXAResource) XAException(javax.transaction.xa.XAException) ObjStoreBrowser(com.arjuna.ats.arjuna.tools.osb.mbean.ObjStoreBrowser) XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper) XAException(javax.transaction.xa.XAException) RollbackException(javax.transaction.RollbackException) SystemException(javax.transaction.SystemException) ObjectName(javax.management.ObjectName) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Uid(com.arjuna.ats.arjuna.common.Uid) Field(java.lang.reflect.Field) 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) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.jts.XARecoveryModule) Test(org.junit.Test)

Example 10 with XATerminatorImple

use of com.arjuna.ats.internal.jta.transaction.jts.jca.XATerminatorImple 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)

Aggregations

XATerminatorImple (com.arjuna.ats.internal.jta.transaction.jts.jca.XATerminatorImple)13 Test (org.junit.Test)13 Uid (com.arjuna.ats.arjuna.common.Uid)12 TransactionImporter (com.arjuna.ats.internal.jta.transaction.arjunacore.jca.TransactionImporter)12 XidImple (com.arjuna.ats.jta.xa.XidImple)10 XAException (javax.transaction.xa.XAException)9 SubordinateTransaction (com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateTransaction)6 Xid (javax.transaction.xa.Xid)6 TestXAResource (com.hp.mwtests.ts.jta.jts.TestXAResource)3 RollbackException (javax.transaction.RollbackException)3 SystemException (javax.transaction.SystemException)3 XAResource (javax.transaction.xa.XAResource)2 ObjectStoreIterator (com.arjuna.ats.arjuna.objectstore.ObjectStoreIterator)1 ObjStoreBrowser (com.arjuna.ats.arjuna.tools.osb.mbean.ObjStoreBrowser)1 XARecoveryModule (com.arjuna.ats.internal.jta.recovery.jts.XARecoveryModule)1 XAResourceRecoveryHelper (com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ObjectName (javax.management.ObjectName)1