Search in sources :

Example 11 with CommitMarkableResourceRecordRecoveryModule

use of com.arjuna.ats.internal.jta.recovery.arjunacore.CommitMarkableResourceRecordRecoveryModule in project narayana by jbosstm.

the class TestCommitMarkableResourceFailActivate method testFailAfterPrepare.

@Test
@BMScript("commitMarkableResourceFailAfterCommit")
public void testFailAfterPrepare() throws Exception {
    final DataSource dataSource = new JdbcDataSource();
    ((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
    CommitMarkableResourceRecordRecoveryModule recoveryModule = null;
    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) {
    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)");
                tm.commit();
            } catch (ExecuteException t) {
            } catch (Exception t) {
                t.printStackTrace();
                failed = true;
            } catch (Error t) {
            }
        }
    });
    foo.start();
    foo.join();
    assertFalse(failed);
    // Now we need to correctly complete the transaction
    manager.scan();
    assertFalse(xaResource.wasCommitted());
    assertFalse(xaResource.wasRolledback());
    // This is test code, it allows us to verify that the
    // correct XID was
    // removed
    Xid committed = ((JDBCConnectableResource) nonXAResource).getStartedXid();
    assertNotNull(committed);
    // The recovery module has to perform lookups
    new InitialContext().rebind("commitmarkableresource", dataSource);
    // Run the first pass it will load the committed Xids into so we can
    // indepently verify that the item was committed
    recoveryModule.periodicWorkFirstPass();
    recoveryModule.periodicWorkSecondPass();
    assertTrue(recoveryModule.wasCommitted("commitmarkableresource", committed));
    // Run the scan to clear the content
    manager.scan();
    assertTrue(xaResource.wasCommitted());
    assertFalse(xaResource.wasRolledback());
    // Make sure that the resource was GC'd by the CRRRM
    assertTrue(recoveryModule.wasCommitted("commitmarkableresource", committed));
    recoveryModule.periodicWorkFirstPass();
    assertTrue(recoveryModule.wasCommitted("commitmarkableresource", committed));
    // This is the pass that will delete it
    recoveryModule.periodicWorkSecondPass();
    assertFalse(recoveryModule.wasCommitted("commitmarkableresource", committed));
}
Also used : Enumeration(java.util.Enumeration) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Connection(java.sql.Connection) XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper) ExecuteException(org.jboss.byteman.rule.exception.ExecuteException) InitialContext(javax.naming.InitialContext) DataSource(javax.sql.DataSource) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) 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) Test(org.junit.Test) BMScript(org.jboss.byteman.contrib.bmunit.BMScript)

Example 12 with CommitMarkableResourceRecordRecoveryModule

use of com.arjuna.ats.internal.jta.recovery.arjunacore.CommitMarkableResourceRecordRecoveryModule in project narayana by jbosstm.

the class TestCommitMarkableResourceFailAfterCommitOrphan method test.

@Test
@BMScript("commitMarkableResourceFailAfterCommit")
public void test() throws Exception {
    final JdbcDataSource dataSource = new 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
    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) {
                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 preCrash = new Thread(new Runnable() {

        public void run() {
            try (Connection localJDBCConnection = dataSource.getConnection()) {
                javax.transaction.TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
                tm.begin();
                localJDBCConnection.setAutoCommit(false);
                cmrResource = new JDBCConnectableResource(localJDBCConnection);
                tm.getTransaction().enlistResource(cmrResource);
                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) {
            }
        }
    });
    preCrash.start();
    preCrash.join();
    assertFalse(failed);
    assertFalse(wasCommitted);
    assertFalse(wasRolledback);
    // The recovery module has to perform lookups
    new InitialContext().rebind("commitmarkableresource", dataSource);
    // Testing that we can find a AAC but that it won't be ignored in orphan detection so need CMRRM run
    commitMarkableResourceRecoveryModule.periodicWorkFirstPass();
    XAResourceOrphanFilter.Vote vote = new JTATransactionLogXAResourceOrphanFilter().checkXid(preparedXid);
    assertTrue(vote == XAResourceOrphanFilter.Vote.LEAVE_ALONE);
    manager.scan();
    manager.scan();
    assertTrue(wasCommitted);
}
Also used : Enumeration(java.util.Enumeration) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Connection(java.sql.Connection) JTATransactionLogXAResourceOrphanFilter(com.arjuna.ats.internal.jta.recovery.arjunacore.JTATransactionLogXAResourceOrphanFilter) XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper) ExecuteException(org.jboss.byteman.rule.exception.ExecuteException) XAException(javax.transaction.xa.XAException) InitialContext(javax.naming.InitialContext) 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) CommitMarkableResourceRecordRecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.CommitMarkableResourceRecordRecoveryModule) Vector(java.util.Vector) XAResourceOrphanFilter(com.arjuna.ats.jta.recovery.XAResourceOrphanFilter) JTATransactionLogXAResourceOrphanFilter(com.arjuna.ats.internal.jta.recovery.arjunacore.JTATransactionLogXAResourceOrphanFilter) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) Test(org.junit.Test) BMScript(org.jboss.byteman.contrib.bmunit.BMScript)

Aggregations

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