Search in sources :

Example 6 with XAResourceRecoveryHelper

use of com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper in project narayana by jbosstm.

the class FailAfterCommitBase method doTest.

protected void doTest(final DataSource dataSource) throws Exception {
    // 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
    CommitMarkableResourceRecordRecoveryModule commitMarkableResourceRecoveryModule = null;
    Vector recoveryModules = manager.getModules();
    if (recoveryModules != null) {
        Enumeration modules = recoveryModules.elements();
        while (modules.hasMoreElements()) {
            RecoveryModule m = (RecoveryModule) modules.nextElement();
            if (m instanceof CommitMarkableResourceRecordRecoveryModule) {
                commitMarkableResourceRecoveryModule = (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) {
    Thread background = 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)");
                tm.commit();
            } catch (ExecuteException t) {
            } catch (Exception e) {
                e.printStackTrace();
                failed = true;
            } catch (Error e) {
            }
        }
    });
    background.start();
    background.join();
    assertFalse(failed);
    Xid committed = ((JDBCConnectableResource) nonXAResource).getStartedXid();
    assertNotNull(committed);
    // The recovery module has to perform lookups
    new InitialContext().rebind("commitmarkableresource", dataSource);
    // Check if the item is still in the db
    commitMarkableResourceRecoveryModule.periodicWorkFirstPass();
    assertTrue(commitMarkableResourceRecoveryModule.wasCommitted("commitmarkableresource", committed));
    commitMarkableResourceRecoveryModule.periodicWorkSecondPass();
    // Now we need to correctly complete the transaction
    manager.scan();
    // Make sure the item is no longer in the DB, it will need two scans as
    // second phase of the CommitMarkableResourceRecoveryModule will have
    // executed before the AtomicActionRecoveryModule has been able to GC
    // it
    assertTrue(commitMarkableResourceRecoveryModule.wasCommitted("commitmarkableresource", committed));
    manager.scan();
    commitMarkableResourceRecoveryModule.periodicWorkFirstPass();
    commitMarkableResourceRecoveryModule.periodicWorkSecondPass();
    assertTrue(xaResource.wasCommitted());
    assertFalse(xaResource.wasRolledback());
    // Make sure that the resource was GC'd by the CRRRM
    assertFalse(commitMarkableResourceRecoveryModule.wasCommitted("commitmarkableresource", committed));
}
Also used : Enumeration(java.util.Enumeration) Connection(java.sql.Connection) XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper) ExecuteException(org.jboss.byteman.rule.exception.ExecuteException) InitialContext(javax.naming.InitialContext) XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) 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) CommitMarkableResourceRecordRecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.CommitMarkableResourceRecordRecoveryModule) Vector(java.util.Vector) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule)

Example 7 with XAResourceRecoveryHelper

use of com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper 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 8 with XAResourceRecoveryHelper

use of com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper in project narayana by jbosstm.

the class UnserializableSerializableXAResourceTest method test.

@Test
public void test() throws Exception {
    XAResource res1 = new XAResource() {

        @Override
        public void start(Xid xid, int flags) throws XAException {
        // TODO Auto-generated method stub
        }

        @Override
        public void end(Xid xid, int flags) throws XAException {
        // TODO Auto-generated method stub
        }

        @Override
        public int prepare(Xid xid) throws XAException {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public void commit(Xid xid, boolean onePhase) throws XAException {
        // TODO Auto-generated method stub
        }

        @Override
        public void rollback(Xid xid) throws XAException {
        // TODO Auto-generated method stub
        }

        @Override
        public void forget(Xid xid) throws XAException {
        // TODO Auto-generated method stub
        }

        @Override
        public Xid[] recover(int flag) throws XAException {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public boolean isSameRM(XAResource xaRes) throws XAException {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public int getTransactionTimeout() throws XAException {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public boolean setTransactionTimeout(int seconds) throws XAException {
            // TODO Auto-generated method stub
            return false;
        }
    };
    UnserializableSerializableXAResource res2 = new UnserializableSerializableXAResource(true);
    final javax.transaction.TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
    tm.getStatus();
    tm.begin();
    javax.transaction.Transaction theTransaction = tm.getTransaction();
    assertTrue(theTransaction.enlistResource(res1));
    assertTrue(theTransaction.enlistResource(res2));
    tm.commit();
    XARecoveryModule xaRecoveryModule = null;
    for (RecoveryModule recoveryModule : ((Vector<RecoveryModule>) recoveryManager.getModules())) {
        if (recoveryModule instanceof XARecoveryModule) {
            xaRecoveryModule = (XARecoveryModule) recoveryModule;
            break;
        }
    }
    final Xid xid = res2.getXid();
    xaRecoveryModule.addXAResourceRecoveryHelper(new XAResourceRecoveryHelper() {

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

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

                @Override
                public void start(Xid xid, int flags) throws XAException {
                // TODO Auto-generated method stub
                }

                @Override
                public void end(Xid xid, int flags) throws XAException {
                // TODO Auto-generated method stub
                }

                @Override
                public int prepare(Xid xid) throws XAException {
                    // TODO Auto-generated method stub
                    return 0;
                }

                @Override
                public void commit(Xid xid, boolean onePhase) throws XAException {
                    committed = true;
                }

                @Override
                public void rollback(Xid xid) throws XAException {
                // TODO Auto-generated method stub
                }

                @Override
                public void forget(Xid xid) throws XAException {
                // TODO Auto-generated method stub
                }

                @Override
                public Xid[] recover(int flag) throws XAException {
                    if (committed) {
                        return null;
                    } else {
                        return new Xid[] { xid };
                    }
                }

                @Override
                public boolean isSameRM(XAResource xaRes) throws XAException {
                    // TODO Auto-generated method stub
                    return false;
                }

                @Override
                public int getTransactionTimeout() throws XAException {
                    // TODO Auto-generated method stub
                    return 0;
                }

                @Override
                public boolean setTransactionTimeout(int seconds) throws XAException {
                    // TODO Auto-generated method stub
                    return false;
                }
            } };
        }
    });
    recoveryManager.scan();
    assertTrue(committed);
}
Also used : XAException(javax.transaction.xa.XAException) XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper) SystemException(org.omg.CORBA.SystemException) XAException(javax.transaction.xa.XAException) XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.jts.XARecoveryModule) RecoveryModule(com.arjuna.ats.arjuna.recovery.RecoveryModule) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.jts.XARecoveryModule) Test(org.junit.Test)

Example 9 with XAResourceRecoveryHelper

use of com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper in project spring-boot by spring-projects.

the class NarayanaXAConnectionFactoryWrapper method wrapConnectionFactory.

@Override
public ConnectionFactory wrapConnectionFactory(XAConnectionFactory xaConnectionFactory) {
    XAResourceRecoveryHelper recoveryHelper = getRecoveryHelper(xaConnectionFactory);
    this.recoveryManager.registerXAResourceRecoveryHelper(recoveryHelper);
    return new ConnectionFactoryProxy(xaConnectionFactory, new TransactionHelperImpl(this.transactionManager));
}
Also used : ConnectionFactoryProxy(org.jboss.narayana.jta.jms.ConnectionFactoryProxy) TransactionHelperImpl(org.jboss.narayana.jta.jms.TransactionHelperImpl) JmsXAResourceRecoveryHelper(org.jboss.narayana.jta.jms.JmsXAResourceRecoveryHelper) XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper)

Example 10 with XAResourceRecoveryHelper

use of com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper in project spring-boot by spring-projects.

the class NarayanaXADataSourceWrapper method wrapDataSource.

@Override
public DataSource wrapDataSource(XADataSource dataSource) {
    XAResourceRecoveryHelper recoveryHelper = getRecoveryHelper(dataSource);
    this.recoveryManager.registerXAResourceRecoveryHelper(recoveryHelper);
    return new NarayanaDataSourceBean(dataSource);
}
Also used : XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper)

Aggregations

XAResourceRecoveryHelper (com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper)24 XARecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule)19 XAResource (javax.transaction.xa.XAResource)17 Test (org.junit.Test)17 Xid (javax.transaction.xa.Xid)13 RecoveryModule (com.arjuna.ats.arjuna.recovery.RecoveryModule)11 XAException (javax.transaction.xa.XAException)11 Uid (com.arjuna.ats.arjuna.common.Uid)8 CommitMarkableResourceRecordRecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.CommitMarkableResourceRecordRecoveryModule)8 Connection (java.sql.Connection)8 Enumeration (java.util.Enumeration)8 Vector (java.util.Vector)8 InitialContext (javax.naming.InitialContext)7 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)7 ExecuteException (org.jboss.byteman.rule.exception.ExecuteException)6 XidImple (com.arjuna.ats.jta.xa.XidImple)5 TransactionImple (com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple)4 BMScript (org.jboss.byteman.contrib.bmunit.BMScript)4 AtomicAction (com.arjuna.ats.arjuna.AtomicAction)3 InputObjectState (com.arjuna.ats.arjuna.state.InputObjectState)3