Search in sources :

Example 1 with TestXAResourceWrapper

use of com.hp.mwtests.ts.jta.recovery.TestXAResourceWrapper in project narayana by jbosstm.

the class ImportedTransactionRecoveryUnitTest method testMultipleXAResourceForImportedJcaTransaction.

/**
 * <p>
 * Testing that multiple {@link XAResource}s could be used under imported transaction.<br>
 * Previously there was no check of xid format which resulted to fact of merging usage of multiple XAResources
 * under one Xid even when the subordinate transaction was under management of Narayana (meaning created by Narayana).
 * That could lead to non-recoverable behavior for particular XAResource.
 * <p>
 * By adding check for format of xid Narayana generates new Xid for any XAResource used under transaction
 * created by Narayana. If transaction comes from EIS there is returned the same Xid (as expected).
 */
@Test
public void testMultipleXAResourceForImportedJcaTransaction() throws Exception {
    final Xid xid = XidUtils.getXid(new Uid(), true);
    SubordinateTransaction subordinateTransaction = SubordinationManager.getTransactionImporter().importTransaction(xid);
    TestXAResourceWrapper xar1 = new TestXAResourceWrapper("narayana", "narayana", "java:/test1") {

        boolean wasThrown = false;

        @Override
        public void commit(Xid xid, boolean onePhase) throws XAException {
            if (!wasThrown) {
                wasThrown = true;
                throw new XAException(XAException.XAER_RMFAIL);
            } else {
                super.commit(xid, onePhase);
            }
        }
    };
    TestXAResourceWrapper xar2 = new TestXAResourceWrapper("narayana", "narayana", "java:/test2") {

        boolean wasThrown = false;

        @Override
        public void commit(Xid xid, boolean onePhase) throws XAException {
            if (!wasThrown) {
                wasThrown = true;
                throw new XAException(XAException.XAER_RMFAIL);
            } else {
                super.commit(xid, onePhase);
            }
        }
    };
    assertTrue("Fail to enlist first test XAResource", subordinateTransaction.enlistResource(xar1));
    assertTrue("Fail to enlist second XAResource", subordinateTransaction.enlistResource(xar2));
    assertEquals("transaction should be prepared", TwoPhaseOutcome.PREPARE_OK, subordinateTransaction.doPrepare());
    assertFalse("first resource should fail on transaction commit, thus whole txn can't be committed", subordinateTransaction.doCommit());
    assertNotEquals("XAResources should be enlisted with different xids", xar1.getXid(), xar2.getXid());
    ((XARecoveryModule) recoveryManager.getModules().get(0)).addXAResourceRecoveryHelper(new TestXARecoveryHelper(xar1, xar2));
    recoveryManager.scan();
    assertEquals("XAResource1 can't be rolled-back", 0, xar1.rollbackCount());
    assertEquals("XAResource2 can't be rolled-back", 0, xar2.rollbackCount());
    assertEquals("XAResource1 has to be committed", 1, xar1.commitCount());
    assertEquals("XAResource2 has to be committed", 1, xar2.commitCount());
}
Also used : Uid(com.arjuna.ats.arjuna.common.Uid) Xid(javax.transaction.xa.Xid) XAException(javax.transaction.xa.XAException) TestXAResourceWrapper(com.hp.mwtests.ts.jta.recovery.TestXAResourceWrapper) SubordinateTransaction(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateTransaction) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.jts.XARecoveryModule) Test(org.junit.Test)

Example 2 with TestXAResourceWrapper

use of com.hp.mwtests.ts.jta.recovery.TestXAResourceWrapper in project narayana by jbosstm.

the class XARecoveryModuleUnitTest method testEmptyResourceEnlistmentParams.

/**
 * Test which uses method {@link TransactionImple#enlistResource(XAResource, Object[])} could be used
 * with empty object array and still works.
 */
@Test
public void testEmptyResourceEnlistmentParams() throws Exception {
    final Uid uid = new Uid();
    final Xid xid = XidUtils.getXid(uid, true);
    SubordinateTransaction subordinateTransaction = SubordinationManager.getTransactionImporter().importTransaction(xid);
    TransactionImple subordinateTransactionImple = (TransactionImple) subordinateTransaction;
    TestXAResourceWrapper xar = new TestXAResourceWrapper("narayana", "narayana", "java:/test1");
    subordinateTransactionImple.enlistResource(xar, new Object[] {});
    int statusPrepare = subordinateTransaction.doPrepare();
    subordinateTransaction.doCommit();
    assertEquals("transaction should be prepared", TwoPhaseOutcome.PREPARE_OK, statusPrepare);
    assertEquals("XAResource can't be rolled-back", 0, xar.rollbackCount());
    assertEquals("XAResource has to be committed", 1, xar.commitCount());
}
Also used : Uid(com.arjuna.ats.arjuna.common.Uid) Xid(javax.transaction.xa.Xid) TransactionImple(com.arjuna.ats.internal.jta.transaction.jts.TransactionImple) TestXAResourceWrapper(com.hp.mwtests.ts.jta.recovery.TestXAResourceWrapper) SubordinateTransaction(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateTransaction) Test(org.junit.Test)

Aggregations

Uid (com.arjuna.ats.arjuna.common.Uid)2 SubordinateTransaction (com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateTransaction)2 TestXAResourceWrapper (com.hp.mwtests.ts.jta.recovery.TestXAResourceWrapper)2 Xid (javax.transaction.xa.Xid)2 Test (org.junit.Test)2 XARecoveryModule (com.arjuna.ats.internal.jta.recovery.jts.XARecoveryModule)1 TransactionImple (com.arjuna.ats.internal.jta.transaction.jts.TransactionImple)1 XAException (javax.transaction.xa.XAException)1