Search in sources :

Example 26 with TransactionImple

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

the class TransactionImpleUnitTest method testXidCreation.

@Test
public void testXidCreation() throws Exception {
    ThreadActionData.purgeActions();
    Class[] parameterTypes = new Class[3];
    TransactionImple tx = new TransactionImple(0);
    parameterTypes[0] = boolean.class;
    parameterTypes[1] = XAModifier.class;
    parameterTypes[2] = XAResource.class;
    Method m = tx.getClass().getDeclaredMethod("createXid", parameterTypes);
    m.setAccessible(true);
    Object[] parameters = new Object[3];
    parameters[0] = false;
    parameters[1] = new DummyXAModifier();
    parameters[2] = new DummyXA(false);
    Xid res = (Xid) m.invoke(tx, parameters);
    assertTrue(res != null);
    tx.rollback();
}
Also used : Xid(javax.transaction.xa.Xid) DummyXA(com.hp.mwtests.ts.jta.common.DummyXA) TransactionImple(com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 27 with TransactionImple

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

the class SynchronizationUnitTest method testSynchronizationFailure.

@Test
public void testSynchronizationFailure() throws Exception {
    TransactionImple tx = new TransactionImple(0);
    DummyXA res = new DummyXA(false) {

        public void rollback(Xid xid) throws XAException {
            super.rollback(xid);
            throw new XAException(XAException.XA_RETRY);
        }
    };
    tx.enlistResource(res);
    final String exceptionError = "intentional testing exception";
    tx.registerSynchronization(new javax.transaction.Synchronization() {

        @Override
        public void beforeCompletion() {
            throw new RuntimeException(exceptionError);
        }

        @Override
        public void afterCompletion(int status) {
        }
    });
    try {
        tx.commit();
    } catch (Exception e) {
        Throwable exceptionToCheck = e;
        while (exceptionToCheck != null) {
            if (exceptionToCheck.getMessage().equals(exceptionError))
                return;
            exceptionToCheck = exceptionToCheck.getCause();
        }
        throw e;
    }
}
Also used : Xid(javax.transaction.xa.Xid) DummyXA(com.hp.mwtests.ts.jta.common.DummyXA) XAException(javax.transaction.xa.XAException) TransactionImple(com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple) XAException(javax.transaction.xa.XAException) Test(org.junit.Test)

Example 28 with TransactionImple

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

the class FailAfterPrepareBase method generateCMRRecord.

protected Uid generateCMRRecord(final DataSource dataSource) throws Exception {
    ((JdbcDataSource) dataSource).setURL("jdbc:h2:mem:JBTMDB;MVCC=TRUE;DB_CLOSE_DELAY=-1");
    // Test code
    Utils.createTables(dataSource.getConnection());
    // We can't just instantiate one as we need to be using the
    // same one as
    // the transaction
    // manager would have used to mark the transaction for GC
    Vector recoveryModules = manager.getModules();
    if (recoveryModules != null) {
        Enumeration modules = recoveryModules.elements();
        while (modules.hasMoreElements()) {
            RecoveryModule m = (RecoveryModule) modules.nextElement();
            if (m instanceof CommitMarkableResourceRecordRecoveryModule) {
                recoveryModule = (CommitMarkableResourceRecordRecoveryModule) m;
            } else if (m instanceof XARecoveryModule) {
                XARecoveryModule xarm = (XARecoveryModule) m;
                xarm.addXAResourceRecoveryHelper(new XAResourceRecoveryHelper() {

                    public boolean initialise(String p) throws Exception {
                        return true;
                    }

                    public XAResource[] getXAResources() throws Exception {
                        return new XAResource[] { xaResource };
                    }
                });
            }
        }
    }
    // final Object o = new Object();
    // synchronized (o) {
    final Uid[] uids = new Uid[1];
    Thread foo = new Thread(new Runnable() {

        public void run() {
            try {
                javax.transaction.TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
                tm.begin();
                Connection localJDBCConnection = dataSource.getConnection();
                localJDBCConnection.setAutoCommit(false);
                nonXAResource = new JDBCConnectableResource(localJDBCConnection);
                tm.getTransaction().enlistResource(nonXAResource);
                xaResource = new SimpleXAResource();
                tm.getTransaction().enlistResource(xaResource);
                localJDBCConnection.createStatement().execute("INSERT INTO foo (bar) VALUES (1)");
                uids[0] = ((TransactionImple) tm.getTransaction()).get_uid();
                tm.commit();
            } catch (ExecuteException t) {
            } catch (Exception t) {
                t.printStackTrace();
                failed = true;
            } catch (Error t) {
            }
        }
    });
    foo.start();
    foo.join();
    assertFalse(failed);
    return uids[0];
}
Also used : Enumeration(java.util.Enumeration) TransactionImple(com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Connection(java.sql.Connection) XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper) ExecuteException(org.jboss.byteman.rule.exception.ExecuteException) NamingException(javax.naming.NamingException) ObjectStoreException(com.arjuna.ats.arjuna.exceptions.ObjectStoreException) Uid(com.arjuna.ats.arjuna.common.Uid) XAResource(javax.transaction.xa.XAResource) ExecuteException(org.jboss.byteman.rule.exception.ExecuteException) CommitMarkableResourceRecordRecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.CommitMarkableResourceRecordRecoveryModule) RecoveryModule(com.arjuna.ats.arjuna.recovery.RecoveryModule) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) Vector(java.util.Vector) CommitMarkableResourceRecordRecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.CommitMarkableResourceRecordRecoveryModule) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule)

Example 29 with TransactionImple

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

the class WorkUnitTest method testWorkSynchronization.

@Test
public void testWorkSynchronization() throws Exception {
    Transaction tx = new TransactionImple(0);
    Synchronization ws = new WorkSynchronization(tx);
    DummyWork work = new DummyWork();
    TxWorkManager.addWork(work, tx);
    try {
        ws.beforeCompletion();
        fail();
    } catch (final IllegalStateException ex) {
    }
    ws.afterCompletion(Status.STATUS_COMMITTED);
}
Also used : Transaction(javax.transaction.Transaction) TransactionImple(com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple) WorkSynchronization(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.WorkSynchronization) Synchronization(javax.transaction.Synchronization) WorkSynchronization(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.WorkSynchronization) Test(org.junit.Test)

Example 30 with TransactionImple

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

the class CrashRecoveryCommitReturnsXA_RETRYHeuristicRollback method testHeuristicRollback.

@Test
public void testHeuristicRollback() throws Exception {
    // this test is supposed to leave a record around in the log store
    // during a commit long enough
    // that the periodic recovery thread runs and detects it. rather than
    // rely on delays to make
    // this happen (placing us at the mercy of the scheduler) we use a
    // byteman script to enforce
    // the thread sequence we need
    RecoveryEnvironmentBean recoveryEnvironmentBean = BeanPopulator.getDefaultInstance(RecoveryEnvironmentBean.class);
    // JBTM-1354 we need to make sure that a full scan has gone off
    recoveryEnvironmentBean.setRecoveryBackoffPeriod(1);
    recoveryEnvironmentBean.setPeriodicRecoveryPeriod(Integer.MAX_VALUE);
    List<String> recoveryModuleClassNames = new ArrayList<String>();
    recoveryModuleClassNames.add("com.arjuna.ats.internal.arjuna.recovery.AtomicActionRecoveryModule");
    recoveryModuleClassNames.add("com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule");
    recoveryEnvironmentBean.setRecoveryModuleClassNames(recoveryModuleClassNames);
    List<String> expiryScannerClassNames = new ArrayList<String>();
    expiryScannerClassNames.add("com.arjuna.ats.internal.arjuna.recovery.ExpiredTransactionStatusManagerScanner");
    recoveryEnvironmentBean.setExpiryScannerClassNames(expiryScannerClassNames);
    recoveryEnvironmentBean.setRecoveryActivators(null);
    // start the recovery manager
    RecoveryManager.manager().initialize();
    XARecoveryModule xaRecoveryModule = null;
    for (RecoveryModule recoveryModule : ((Vector<RecoveryModule>) RecoveryManager.manager().getModules())) {
        if (recoveryModule instanceof XARecoveryModule) {
            xaRecoveryModule = (XARecoveryModule) recoveryModule;
            break;
        }
    }
    if (xaRecoveryModule == null) {
        throw new Exception("No XARM");
    }
    // JBTM-1354 Run a scan to make sure that the recovery thread has completed a full run before starting the test
    // The important thing is that replayCompletion is allowed to do a scan of the transactions
    RecoveryManager.manager().scan();
    XAResource firstResource = new SimpleResource();
    Object toWakeUp = new Object();
    final SimpleResourceXA_RETRYHeuristicRollback secondResource = new SimpleResourceXA_RETRYHeuristicRollback();
    xaRecoveryModule.addXAResourceRecoveryHelper(new XAResourceRecoveryHelper() {

        @Override
        public boolean initialise(String p) throws Exception {
            // TODO Auto-generated method stub
            return true;
        }

        @Override
        public XAResource[] getXAResources() throws Exception {
            // TODO Auto-generated method stub
            return new XAResource[] { secondResource };
        }
    });
    // ok, now drive a TX to completion. the script should ensure that the
    // recovery
    javax.transaction.TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
    tm.begin();
    javax.transaction.Transaction theTransaction = tm.getTransaction();
    Uid txUid = ((TransactionImple) theTransaction).get_uid();
    theTransaction.enlistResource(firstResource);
    theTransaction.enlistResource(secondResource);
    assertFalse(secondResource.wasCommitted());
    tm.commit();
    InputObjectState uids = new InputObjectState();
    String type = new AtomicAction().type();
    StoreManager.getRecoveryStore().allObjUids(type, uids);
    boolean moreUids = true;
    boolean found = false;
    while (moreUids) {
        Uid theUid = UidHelper.unpackFrom(uids);
        if (theUid.equals(txUid)) {
            found = true;
            Field heuristicListField = BasicAction.class.getDeclaredField("heuristicList");
            heuristicListField.setAccessible(true);
            ActionStatusService ass = new ActionStatusService();
            {
                int theStatus = ass.getTransactionStatus(type, theUid.stringForm());
                assertTrue(theStatus == ActionStatus.COMMITTED);
                RecoverAtomicAction rcvAtomicAction = new RecoverAtomicAction(theUid, theStatus);
                theStatus = rcvAtomicAction.status();
                rcvAtomicAction.replayPhase2();
                assertTrue(theStatus == ActionStatus.COMMITTED);
                assertTrue(secondResource.wasCommitted());
                RecordList heuristicList = (RecordList) heuristicListField.get(rcvAtomicAction);
                assertTrue("Expected 1 heuristics: " + heuristicList.size(), heuristicList.size() == 1);
            }
            {
                int theStatus = ass.getTransactionStatus(type, theUid.stringForm());
                assertTrue(theStatus == ActionStatus.COMMITTED);
                RecoverAtomicAction rcvAtomicAction = new RecoverAtomicAction(theUid, theStatus);
                theStatus = rcvAtomicAction.status();
                assertTrue(theStatus == ActionStatus.COMMITTED);
                RecordList heuristicList = (RecordList) heuristicListField.get(rcvAtomicAction);
                assertTrue("Expected 1 heuristics: " + heuristicList.size(), heuristicList.size() == 1);
                assertTrue(secondResource.wasCommitted());
            }
        } else if (theUid.equals(Uid.nullUid())) {
            moreUids = false;
        }
    }
    if (!found) {
        throw new Exception("Could not locate the Uid");
    }
}
Also used : TransactionImple(com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple) ArrayList(java.util.ArrayList) XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper) AtomicAction(com.arjuna.ats.arjuna.AtomicAction) RecoverAtomicAction(com.arjuna.ats.arjuna.recovery.RecoverAtomicAction) Field(java.lang.reflect.Field) ActionStatusService(com.arjuna.ats.arjuna.recovery.ActionStatusService) RecoverAtomicAction(com.arjuna.ats.arjuna.recovery.RecoverAtomicAction) InputObjectState(com.arjuna.ats.arjuna.state.InputObjectState) Uid(com.arjuna.ats.arjuna.common.Uid) RecoveryEnvironmentBean(com.arjuna.ats.arjuna.common.RecoveryEnvironmentBean) XAResource(javax.transaction.xa.XAResource) RecordList(com.arjuna.ats.arjuna.coordinator.RecordList) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) RecoveryModule(com.arjuna.ats.arjuna.recovery.RecoveryModule) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) Test(org.junit.Test)

Aggregations

TransactionImple (com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple)32 Test (org.junit.Test)30 FailureXAResource (com.hp.mwtests.ts.jta.common.FailureXAResource)12 XAException (javax.transaction.xa.XAException)11 RollbackException (javax.transaction.RollbackException)10 DummyXA (com.hp.mwtests.ts.jta.common.DummyXA)9 Uid (com.arjuna.ats.arjuna.common.Uid)7 XAResourceRecord (com.arjuna.ats.internal.jta.resources.arjunacore.XAResourceRecord)7 HeuristicMixedException (javax.transaction.HeuristicMixedException)6 TestResource (com.hp.mwtests.ts.jta.common.TestResource)5 XAResource (javax.transaction.xa.XAResource)5 Xid (javax.transaction.xa.Xid)5 RecoveryModule (com.arjuna.ats.arjuna.recovery.RecoveryModule)4 InputObjectState (com.arjuna.ats.arjuna.state.InputObjectState)4 XARecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule)4 XAResourceRecoveryHelper (com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper)4 SystemException (javax.transaction.SystemException)4 AtomicAction (com.arjuna.ats.arjuna.AtomicAction)3 CommitMarkableResourceRecordRecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.CommitMarkableResourceRecordRecoveryModule)3 XidImple (com.arjuna.ats.jta.xa.XidImple)3