Search in sources :

Example 31 with SubordinateTransaction

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

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

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

the class XATerminatorUnitTest method testCommitMid.

@Test
public void testCommitMid() throws Exception {
    TransactionManagerImple tm = new TransactionManagerImple();
    RecordTypeManager.manager().add(new RecordTypeMap() {

        @SuppressWarnings("unchecked")
        public Class getRecordClass() {
            return XAResourceRecord.class;
        }

        public int getType() {
            return RecordType.JTA_RECORD;
        }
    });
    XATerminatorImple xaTerminator = new XATerminatorImple();
    XidImple xid = new XidImple(new Uid());
    XAResourceImple toCommit = new XAResourceImple(XAResource.XA_OK, XAResource.XA_OK);
    {
        SubordinateTransaction subordinateTransaction = SubordinationManager.getTransactionImporter().importTransaction(xid);
        tm.resume(subordinateTransaction);
        subordinateTransaction.enlistResource(new XAResourceImple(XAResource.XA_RDONLY, XAResource.XA_OK));
        subordinateTransaction.enlistResource(toCommit);
        Transaction suspend = tm.suspend();
    }
    {
        SubordinateTransaction subordinateTransaction = SubordinationManager.getTransactionImporter().getImportedTransaction(xid);
        tm.resume(subordinateTransaction);
        subordinateTransaction.doPrepare();
        Transaction suspend = tm.suspend();
    }
    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[] { toCommit };
        }
    });
    RecoveryManager.manager().addModule(xaRecoveryModule);
    xaTerminator.doRecover(null, null);
    {
        SubordinateTransaction subordinateTransaction = SubordinationManager.getTransactionImporter().getImportedTransaction(xid);
        tm.resume(subordinateTransaction);
        subordinateTransaction.doCommit();
        tm.suspend();
    }
    RecoveryManager.manager().removeModule(xaRecoveryModule, false);
    assertTrue(toCommit.wasCommitted());
    SubordinationManager.getTransactionImporter().removeImportedTransaction(xid);
}
Also used : XidImple(com.arjuna.ats.jta.xa.XidImple) 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) TransactionManagerImple(com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple) RecordTypeMap(com.arjuna.ats.arjuna.coordinator.abstractrecord.RecordTypeMap) SubordinateTransaction(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateTransaction) Transaction(javax.transaction.Transaction) 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 34 with SubordinateTransaction

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

the class XATerminatorImple method forget.

public void forget(Xid xid) throws XAException {
    try {
        SubordinateTransaction tx = SubordinationManager.getTransactionImporter().getImportedTransaction(xid);
        if (tx == null)
            throw new XAException(XAException.XAER_INVAL);
        tx.doForget();
    } catch (Exception ex) {
        XAException xaException = new XAException(XAException.XAER_RMERR);
        xaException.initCause(ex);
        throw xaException;
    } finally {
        SubordinationManager.getTransactionImporter().removeImportedTransaction(xid);
    }
}
Also used : XAException(javax.transaction.xa.XAException) SubordinateTransaction(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateTransaction) UnexpectedConditionException(com.arjuna.ats.jta.exceptions.UnexpectedConditionException) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) IOException(java.io.IOException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) XAException(javax.transaction.xa.XAException) HeuristicMixedException(javax.transaction.HeuristicMixedException) HeuristicCommitException(javax.transaction.HeuristicCommitException)

Example 35 with SubordinateTransaction

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

the class TransactionImporterImple method getImportedTransaction.

/**
 * Get the subordinate (imported) transaction associated with the
 * global transaction.
 *
 * @param xid the global transaction.
 *
 * @return the subordinate transaction or <code>null</code> if there
 * is none.
 *
 * @throws XAException thrown if there are any errors.
 */
public SubordinateTransaction getImportedTransaction(Xid xid) throws XAException {
    if (xid == null)
        throw new IllegalArgumentException();
    AtomicReference<SubordinateTransaction> holder = _transactions.get(new SubordinateXidImple(xid));
    SubordinateTransaction tx = holder == null ? null : holder.get();
    if (tx == null) {
        /*
			 * Remark: if holder != null and holder.get() == null then the setter is about to
			 * import the transaction but has not yet updated the holder. We implement the getter
			 * (the thing that is trying to terminate the imported transaction) as though the imported
			 * transaction only becomes observable when it has been fully imported.
			 */
        return null;
    }
    // https://issues.jboss.org/browse/JBTM-927
    try {
        if (tx.getStatus() == javax.transaction.Status.STATUS_ROLLEDBACK) {
            throw new XAException(XAException.XA_RBROLLBACK);
        }
    } catch (SystemException e) {
        throw new XAException(XAException.XA_RBROLLBACK);
    }
    if (tx.baseXid() == null) {
        /*
			 * Try recovery again. If it fails we'll throw a RETRY to the caller who
			 * should try again later.
			 */
        tx.recover();
        return tx;
    } else
        return tx;
}
Also used : SubordinateXidImple(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateXidImple) XAException(javax.transaction.xa.XAException) SystemException(javax.transaction.SystemException) SubordinateTransaction(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateTransaction)

Aggregations

SubordinateTransaction (com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateTransaction)42 Test (org.junit.Test)35 XAException (javax.transaction.xa.XAException)18 Uid (com.arjuna.ats.arjuna.common.Uid)16 Xid (javax.transaction.xa.Xid)13 XidImple (com.arjuna.ats.jta.xa.XidImple)12 TransactionImporter (com.arjuna.ats.internal.jta.transaction.arjunacore.jca.TransactionImporter)7 XATerminatorImple (com.arjuna.ats.internal.jta.transaction.jts.jca.XATerminatorImple)6 RollbackException (javax.transaction.RollbackException)6 SystemException (javax.transaction.SystemException)6 XAResource (javax.transaction.xa.XAResource)6 XATerminatorImple (com.arjuna.ats.internal.jta.transaction.arjunacore.jca.XATerminatorImple)4 ArrayList (java.util.ArrayList)4 XARecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule)3 SubordinateXidImple (com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateXidImple)3 UnexpectedConditionException (com.arjuna.ats.jta.exceptions.UnexpectedConditionException)3 XAResourceRecoveryHelper (com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper)3 TestXAResource (com.hp.mwtests.ts.jta.jts.TestXAResource)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 HeuristicMixedException (javax.transaction.HeuristicMixedException)3