Search in sources :

Example 1 with TransactionalDriver

use of com.arjuna.ats.jdbc.TransactionalDriver in project narayana by jbosstm.

the class PoolingTest method setup.

@Before
public void setup() throws Exception {
    url = "jdbc:arjuna:";
    Properties p = System.getProperties();
    p.put("jdbc.drivers", Driver.class.getName());
    System.setProperties(p);
    DriverManager.registerDriver(new TransactionalDriver());
    dbProperties = new Properties();
    JdbcDataSource ds = new JdbcDataSource();
    ds.setURL("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
    dbProperties.put(TransactionalDriver.XADataSource, ds);
    dbProperties.setProperty(TransactionalDriver.maxConnections, "" + POOLSIZE);
    try (Connection conn = DriverManager.getConnection(url, dbProperties)) {
        try (Statement stmt = conn.createStatement()) {
            try {
                stmt.executeUpdate("DROP TABLE test_table");
            } catch (Exception e) {
            // Ignore
            } finally {
                stmt.executeUpdate("CREATE TABLE test_table (a varchar2)");
            }
        }
    }
}
Also used : Statement(java.sql.Statement) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Connection(java.sql.Connection) TransactionalDriver(com.arjuna.ats.jdbc.TransactionalDriver) Driver(org.h2.Driver) Properties(java.util.Properties) TransactionalDriver(com.arjuna.ats.jdbc.TransactionalDriver) SQLException(java.sql.SQLException) Before(org.junit.Before)

Example 2 with TransactionalDriver

use of com.arjuna.ats.jdbc.TransactionalDriver in project narayana by jbosstm.

the class RecoveryTest method directRecoverableConnection.

@Test
@BMRule(name = "throw rmfail xaexception", targetClass = "org.h2.jdbcx.JdbcXAConnection", targetMethod = "commit", action = "throw new javax.transaction.xa.XAException(javax.transaction.xa.XAException.XAER_RMFAIL)", targetLocation = "AT ENTRY")
public void directRecoverableConnection() throws Exception {
    // jdbc:arjuna: <path to properties file>, path starting at System.getProperty("user.dir")
    String url = TransactionalDriver.arjunaDriver + "ds-direct.properties";
    Properties dbProperties = new Properties();
    dbProperties.put(TransactionalDriver.dynamicClass, PropertyFileDynamicClass.class.getName());
    dbProperties.put(TransactionalDriver.userName, "");
    dbProperties.put(TransactionalDriver.password, "");
    Properties p = System.getProperties();
    p.put("jdbc.drivers", Driver.class.getName());
    System.setProperties(p);
    transactionalDriver = new TransactionalDriver();
    DriverManager.registerDriver(transactionalDriver);
    step0_setupTables(url, dbProperties);
    // rmfail means the db connection was left to be recovered
    step1_leaveXidsInDbTable(url, dbProperties);
    step2_checkDbIsNotCommitted(url, dbProperties);
    // 3. Perform recovery - checking only top down XARecoveryModule functionality
    {
        XARecoveryModule xarm = new XARecoveryModule();
        RecoveryManager manager = RecoveryManager.manager();
        manager.removeAllModules(true);
        manager.addModule(xarm);
        AtomicActionRecoveryModule aarm = new AtomicActionRecoveryModule();
        aarm.periodicWorkFirstPass();
        Transformer.disableTriggers(true);
        aarm.periodicWorkSecondPass();
        Transformer.enableTriggers(true);
    }
    step4_finalDbCommitCheck(url, dbProperties);
}
Also used : RecoveryManager(com.arjuna.ats.arjuna.recovery.RecoveryManager) AtomicActionRecoveryModule(com.arjuna.ats.internal.arjuna.recovery.AtomicActionRecoveryModule) TransactionalDriver(com.arjuna.ats.jdbc.TransactionalDriver) Driver(org.h2.Driver) PropertyFileDynamicClass(com.arjuna.ats.internal.jdbc.drivers.PropertyFileDynamicClass) Properties(java.util.Properties) TransactionalDriver(com.arjuna.ats.jdbc.TransactionalDriver) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) BMRule(org.jboss.byteman.contrib.bmunit.BMRule) Test(org.junit.Test)

Example 3 with TransactionalDriver

use of com.arjuna.ats.jdbc.TransactionalDriver in project narayana by jbosstm.

the class JDBC2Test method setup.

@Before
public void setup() throws Exception {
    url = "jdbc:arjuna:";
    Properties p = System.getProperties();
    p.put("jdbc.drivers", Driver.class.getName());
    System.setProperties(p);
    DriverManager.registerDriver(new TransactionalDriver());
    dbProperties = new Properties();
    JdbcDataSource ds = new JdbcDataSource();
    ds.setURL("jdbc:h2:./h2/foo");
    dbProperties.put(TransactionalDriver.XADataSource, ds);
    dbProperties.put(TransactionalDriver.poolConnections, "false");
    conn = DriverManager.getConnection(url, dbProperties);
}
Also used : JdbcDataSource(org.h2.jdbcx.JdbcDataSource) TransactionalDriver(com.arjuna.ats.jdbc.TransactionalDriver) Driver(org.h2.Driver) Properties(java.util.Properties) TransactionalDriver(com.arjuna.ats.jdbc.TransactionalDriver) Before(org.junit.Before)

Example 4 with TransactionalDriver

use of com.arjuna.ats.jdbc.TransactionalDriver in project narayana by jbosstm.

the class RecoveryTest method test.

@Test
@BMRule(name = "throw lang error exception", targetClass = "org.h2.jdbcx.JdbcXAConnection", targetMethod = "commit", action = "throw new java.lang.Error()", targetLocation = "AT ENTRY")
public void test() throws Exception {
    String url = "jdbc:arjuna:";
    Properties p = System.getProperties();
    p.put("jdbc.drivers", Driver.class.getName());
    System.setProperties(p);
    transactionalDriver = new TransactionalDriver();
    DriverManager.registerDriver(transactionalDriver);
    Properties dbProperties = new Properties();
    final JdbcDataSource ds = new JdbcDataSource();
    ds.setURL("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
    dbProperties.put(TransactionalDriver.XADataSource, ds);
    step0_setupTables(url, dbProperties);
    // We need to do this in a different thread as otherwise the transaction would still be associated with the connection due to the java.lang.Error
    // RMFAIL on it's own will cause H2 to close connection and that seems to discard the indoubt transactions
    step1_leaveXidsInDbTable(url, dbProperties);
    step2_checkDbIsNotCommitted(url, dbProperties);
    // 3. Perform recovery
    {
        XARecoveryModule xarm = new XARecoveryModule();
        xarm.addXAResourceRecoveryHelper(new XAResourceRecoveryHelper() {

            @Override
            public boolean initialise(String p) throws Exception {
                return false;
            }

            @Override
            public XAResource[] getXAResources() throws Exception {
                return new XAResource[] { ds.getXAConnection().getXAResource() };
            }
        });
        RecoveryManager manager = RecoveryManager.manager();
        manager.removeAllModules(true);
        manager.addModule(xarm);
        AtomicActionRecoveryModule aarm = new AtomicActionRecoveryModule();
        aarm.periodicWorkFirstPass();
        Transformer.disableTriggers(true);
        aarm.periodicWorkSecondPass();
        Transformer.enableTriggers(true);
    }
    step4_finalDbCommitCheck(url, dbProperties);
}
Also used : RecoveryManager(com.arjuna.ats.arjuna.recovery.RecoveryManager) XAResource(javax.transaction.xa.XAResource) AtomicActionRecoveryModule(com.arjuna.ats.internal.arjuna.recovery.AtomicActionRecoveryModule) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) TransactionalDriver(com.arjuna.ats.jdbc.TransactionalDriver) Driver(org.h2.Driver) Properties(java.util.Properties) XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper) TransactionalDriver(com.arjuna.ats.jdbc.TransactionalDriver) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) BMRule(org.jboss.byteman.contrib.bmunit.BMRule) Test(org.junit.Test)

Example 5 with TransactionalDriver

use of com.arjuna.ats.jdbc.TransactionalDriver in project narayana by jbosstm.

the class TransactionServiceFactory method stop.

/**
 * Stop the transaction service. If the recovery manager was started previously then it to will be stopped.
 */
public static synchronized void stop() {
    if (!initialized)
        return;
    if (recoveryManager != null) {
        recoveryManager.terminate();
        recoveryManager = null;
    }
    unregisterJndiBindings();
    try {
        DriverManager.deregisterDriver(new TransactionalDriver());
    } catch (SQLException e) {
        if (tsLogger.logger.isInfoEnabled())
            tsLogger.logger.debug("Unable to deregister TransactionalDriver: " + e.getMessage(), e);
    }
    if (replacedJndiProperties)
        jdbcPropertyManager.getJDBCEnvironmentBean().setJndiProperties(null);
    initialized = false;
}
Also used : SQLException(java.sql.SQLException) TransactionalDriver(com.arjuna.ats.jdbc.TransactionalDriver)

Aggregations

TransactionalDriver (com.arjuna.ats.jdbc.TransactionalDriver)6 Properties (java.util.Properties)4 Driver (org.h2.Driver)4 SQLException (java.sql.SQLException)3 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)3 RecoveryManager (com.arjuna.ats.arjuna.recovery.RecoveryManager)2 AtomicActionRecoveryModule (com.arjuna.ats.internal.arjuna.recovery.AtomicActionRecoveryModule)2 XARecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule)2 BMRule (org.jboss.byteman.contrib.bmunit.BMRule)2 Before (org.junit.Before)2 Test (org.junit.Test)2 PropertyFileDynamicClass (com.arjuna.ats.internal.jdbc.drivers.PropertyFileDynamicClass)1 XAResourceRecoveryHelper (com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper)1 Connection (java.sql.Connection)1 Statement (java.sql.Statement)1 InitialContext (javax.naming.InitialContext)1 NamingException (javax.naming.NamingException)1 XAResource (javax.transaction.xa.XAResource)1