Search in sources :

Example 1 with JdbcXAConnection

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

the class TestDataSource method testXAConnection.

private void testXAConnection(boolean userInDataSource) throws Exception {
    deleteDb("dataSource");
    JdbcDataSource ds = new JdbcDataSource();
    String url = getURL("dataSource", true);
    String user = getUser();
    ds.setURL(url);
    if (userInDataSource) {
        ds.setUser(user);
        ds.setPassword(getPassword());
    }
    if (userInDataSource) {
        assertEquals("ds" + ds.getTraceId() + ": url=" + url + " user=" + user, ds.toString());
    } else {
        assertEquals("ds" + ds.getTraceId() + ": url=" + url + " user=", ds.toString());
    }
    XAConnection xaConn;
    if (userInDataSource) {
        xaConn = ds.getXAConnection();
    } else {
        xaConn = ds.getXAConnection(user, getPassword());
    }
    int traceId = ((JdbcXAConnection) xaConn).getTraceId();
    assertTrue(xaConn.toString().startsWith("xads" + traceId + ": conn"));
    xaConn.addConnectionEventListener(new ConnectionEventListener() {

        @Override
        public void connectionClosed(ConnectionEvent event) {
        // nothing to do
        }

        @Override
        public void connectionErrorOccurred(ConnectionEvent event) {
        // nothing to do
        }
    });
    XAResource res = xaConn.getXAResource();
    assertFalse(res.setTransactionTimeout(1));
    assertEquals(0, res.getTransactionTimeout());
    assertTrue(res.isSameRM(res));
    assertFalse(res.isSameRM(null));
    Connection conn = xaConn.getConnection();
    assertEquals(user.toUpperCase(), conn.getMetaData().getUserName());
    Xid[] list = res.recover(XAResource.TMSTARTRSCAN);
    assertEquals(0, list.length);
    Statement stat = conn.createStatement();
    stat.execute("SELECT * FROM DUAL");
    conn.close();
    xaConn.close();
}
Also used : Statement(java.sql.Statement) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) JdbcXAConnection(org.h2.jdbcx.JdbcXAConnection) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) JdbcXAConnection(org.h2.jdbcx.JdbcXAConnection) XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) ConnectionEvent(javax.sql.ConnectionEvent) XAConnection(javax.sql.XAConnection) JdbcXAConnection(org.h2.jdbcx.JdbcXAConnection) ConnectionEventListener(javax.sql.ConnectionEventListener)

Aggregations

Connection (java.sql.Connection)1 Statement (java.sql.Statement)1 ConnectionEvent (javax.sql.ConnectionEvent)1 ConnectionEventListener (javax.sql.ConnectionEventListener)1 XAConnection (javax.sql.XAConnection)1 XAResource (javax.transaction.xa.XAResource)1 Xid (javax.transaction.xa.Xid)1 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)1 JdbcXAConnection (org.h2.jdbcx.JdbcXAConnection)1