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");
}
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();
}
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;
}
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);
}
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);
}
Aggregations