Search in sources :

Example 56 with JdbcDataSource

use of org.h2.jdbcx.JdbcDataSource in project h2database by h2database.

the class TestXASimple method testSimple.

private void testSimple() throws SQLException {
    deleteDb("xaSimple1");
    deleteDb("xaSimple2");
    org.h2.Driver.load();
    // InitialContext context = new InitialContext();
    // context.rebind(USER_TRANSACTION_JNDI_NAME, j.getUserTransaction());
    JdbcDataSource ds1 = new JdbcDataSource();
    ds1.setPassword(getPassword());
    ds1.setUser("sa");
    ds1.setURL(getURL("xaSimple1", true));
    JdbcDataSource ds2 = new JdbcDataSource();
    ds2.setPassword(getPassword());
    ds2.setUser("sa");
    ds2.setURL(getURL("xaSimple2", true));
    // UserTransaction ut = (UserTransaction)
    // context.lookup("UserTransaction");
    // ut.begin();
    XAConnection xa1 = ds1.getXAConnection();
    Connection c1 = xa1.getConnection();
    c1.setAutoCommit(false);
    XAConnection xa2 = ds2.getXAConnection();
    Connection c2 = xa2.getConnection();
    c2.setAutoCommit(false);
    c1.createStatement().executeUpdate("create table test(id int, test varchar(255))");
    c2.createStatement().executeUpdate("create table test(id int, test varchar(255))");
    // ut.rollback();
    c1.close();
    c2.close();
    xa1.close();
    xa2.close();
    // j.stop();
    // System.exit(0);
    deleteDb("xaSimple1");
    deleteDb("xaSimple2");
}
Also used : JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) XAConnection(javax.sql.XAConnection)

Example 57 with JdbcDataSource

use of org.h2.jdbcx.JdbcDataSource in project h2database by h2database.

the class TestXASimple method testTwoPhase.

private void testTwoPhase(String db, boolean shutdown, boolean commit) throws Exception {
    deleteDb(db);
    JdbcDataSource ds = new JdbcDataSource();
    ds.setPassword(getPassword());
    ds.setUser("sa");
    // ds.setURL(getURL("xaSimple", true) + ";trace_level_system_out=3");
    ds.setURL(getURL(db, true));
    XAConnection xa;
    xa = ds.getXAConnection();
    Connection conn;
    conn = xa.getConnection();
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key, name varchar(255))");
    Xid xid = SimpleXid.createRandom();
    xa.getXAResource().start(xid, XAResource.TMNOFLAGS);
    conn.setAutoCommit(false);
    stat.execute("insert into test values(1, 'Hello')");
    xa.getXAResource().end(xid, XAResource.TMSUCCESS);
    xa.getXAResource().prepare(xid);
    if (shutdown) {
        shutdown(ds);
    }
    xa = ds.getXAConnection();
    Xid[] list = xa.getXAResource().recover(XAResource.TMSTARTRSCAN);
    assertEquals(1, list.length);
    assertTrue(xid.equals(list[0]));
    if (commit) {
        xa.getXAResource().commit(list[0], false);
    } else {
        xa.getXAResource().rollback(list[0]);
    }
    conn = xa.getConnection();
    conn.createStatement().executeQuery("select * from test");
    if (shutdown) {
        shutdown(ds);
    }
    xa = ds.getXAConnection();
    list = xa.getXAResource().recover(XAResource.TMSTARTRSCAN);
    assertEquals(0, list.length);
    conn = xa.getConnection();
    ResultSet rs;
    rs = conn.createStatement().executeQuery("select * from test");
    if (commit) {
        assertTrue(rs.next());
    } else {
        assertFalse(rs.next());
    }
    xa.close();
}
Also used : Xid(javax.transaction.xa.Xid) Statement(java.sql.Statement) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) ResultSet(java.sql.ResultSet) XAConnection(javax.sql.XAConnection)

Example 58 with JdbcDataSource

use of org.h2.jdbcx.JdbcDataSource in project crnk-framework by crnk-project.

the class JpaTestConfig method testDataSource.

@Bean
public DataSource testDataSource() {
    JdbcDataSource dataSource = new JdbcDataSource();
    dataSource.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=TRUE");
    dataSource.setUser("sa");
    return dataSource;
}
Also used : JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Bean(org.springframework.context.annotation.Bean) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)

Example 59 with JdbcDataSource

use of org.h2.jdbcx.JdbcDataSource 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 60 with JdbcDataSource

use of org.h2.jdbcx.JdbcDataSource 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)

Aggregations

JdbcDataSource (org.h2.jdbcx.JdbcDataSource)81 Connection (java.sql.Connection)24 Test (org.junit.Test)24 Before (org.junit.Before)15 XAResource (javax.transaction.xa.XAResource)13 DataSource (javax.sql.DataSource)9 Xid (javax.transaction.xa.Xid)9 SQLException (java.sql.SQLException)8 InitialContext (javax.naming.InitialContext)8 XAConnection (javax.sql.XAConnection)8 RecoveryModule (com.arjuna.ats.arjuna.recovery.RecoveryModule)7 CommitMarkableResourceRecordRecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.CommitMarkableResourceRecordRecoveryModule)7 XARecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule)7 XAResourceRecoveryHelper (com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper)7 Statement (java.sql.Statement)7 BMScript (org.jboss.byteman.contrib.bmunit.BMScript)7 File (java.io.File)6 Enumeration (java.util.Enumeration)6 Properties (java.util.Properties)6 Vector (java.util.Vector)6