Search in sources :

Example 51 with XAConnection

use of javax.sql.XAConnection in project jaybird by FirebirdSQL.

the class TestFBXADataSource method testConnection.

/**
 * Tests if the connection obtained from the PooledConnection can be used
 * and has expected defaults.
 */
@Test
public void testConnection() throws SQLException {
    XAConnection pc = getXAConnection();
    Connection con = pc.getConnection();
    assertTrue("Autocommit should be true", con.getAutoCommit());
    assertTrue("Read-only should be false", !con.isReadOnly());
    assertEquals("Tx isolation level should be read committed.", Connection.TRANSACTION_READ_COMMITTED, con.getTransactionIsolation());
    try (Statement stmt = con.createStatement()) {
        ResultSet rs = stmt.executeQuery("SELECT cast(1 AS INTEGER) FROM rdb$database");
        assertTrue("Should select one row", rs.next());
        assertEquals("Selected value should be 1.", 1, rs.getInt(1));
    }
    con.close();
    assertTrue("Connection should report as being closed.", con.isClosed());
}
Also used : XAConnection(javax.sql.XAConnection) XAConnection(javax.sql.XAConnection)

Example 52 with XAConnection

use of javax.sql.XAConnection in project jaybird by FirebirdSQL.

the class TestFBXADataSource method testInDistributed_setAutoCommit_true_notInAutoCommit.

/**
 * Tests if setting autoCommit(true) when autoCommit is false throws an exception when participating in a distributed transaction (JDBC 4.0 section 12.4).
 */
@Test
public void testInDistributed_setAutoCommit_true_notInAutoCommit() throws Exception {
    XAConnection pc = getXAConnection();
    XAResource xa = pc.getXAResource();
    Xid xid = new XidImpl();
    try (Connection con = pc.getConnection()) {
        con.setAutoCommit(false);
        xa.start(xid, XAResource.TMNOFLAGS);
        expectedException.expect(SQLException.class);
        expectedException.expect(sqlStateEquals(SQLStateConstants.SQL_STATE_INVALID_TX_STATE));
        con.setAutoCommit(true);
    } finally {
        xa.end(xid, XAResource.TMSUCCESS);
        xa.rollback(xid);
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) XidImpl(org.firebirdsql.jca.TestXABase.XidImpl) XAConnection(javax.sql.XAConnection) XAConnection(javax.sql.XAConnection)

Example 53 with XAConnection

use of javax.sql.XAConnection in project jaybird by FirebirdSQL.

the class TestFBXADataSource method getXAConnection.

protected XAConnection getXAConnection() throws SQLException {
    XAConnection pc = ds.getXAConnection();
    connections.add(pc);
    return pc;
}
Also used : XAConnection(javax.sql.XAConnection)

Example 54 with XAConnection

use of javax.sql.XAConnection in project jaybird by FirebirdSQL.

the class TestFBXADataSource method testInDistributed_rollback.

/**
 * Test if calling rollback throws an exception when participating in a distributed transaction (JDBC 4.0 section 12.4).
 */
@Test
public void testInDistributed_rollback() throws Exception {
    XAConnection pc = getXAConnection();
    XAResource xa = pc.getXAResource();
    Xid xid = new XidImpl();
    try (Connection con = pc.getConnection()) {
        con.setAutoCommit(false);
        xa.start(xid, XAResource.TMNOFLAGS);
        expectedException.expect(SQLException.class);
        expectedException.expect(sqlStateEquals(SQLStateConstants.SQL_STATE_INVALID_TX_STATE));
        con.rollback();
    } finally {
        xa.end(xid, XAResource.TMSUCCESS);
        xa.rollback(xid);
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) XidImpl(org.firebirdsql.jca.TestXABase.XidImpl) XAConnection(javax.sql.XAConnection) XAConnection(javax.sql.XAConnection)

Example 55 with XAConnection

use of javax.sql.XAConnection in project jaybird by FirebirdSQL.

the class TestFBXADataSource method testInDistributed_setSavepoint_named.

/**
 * Test if calling setSavePoint (named) throws an exception when participating in a distributed transaction (JDBC 4.0 section 12.4).
 */
@Test
public void testInDistributed_setSavepoint_named() throws Exception {
    XAConnection pc = getXAConnection();
    XAResource xa = pc.getXAResource();
    Xid xid = new XidImpl();
    try (Connection con = pc.getConnection()) {
        con.setAutoCommit(false);
        xa.start(xid, XAResource.TMNOFLAGS);
        expectedException.expect(SQLException.class);
        expectedException.expect(sqlStateEquals(SQLStateConstants.SQL_STATE_INVALID_TX_STATE));
        con.setSavepoint("test_sp");
    } finally {
        xa.end(xid, XAResource.TMSUCCESS);
        xa.rollback(xid);
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) XidImpl(org.firebirdsql.jca.TestXABase.XidImpl) XAConnection(javax.sql.XAConnection) XAConnection(javax.sql.XAConnection)

Aggregations

XAConnection (javax.sql.XAConnection)115 Connection (java.sql.Connection)78 XAResource (javax.transaction.xa.XAResource)56 XADataSource (javax.sql.XADataSource)52 Xid (javax.transaction.xa.Xid)44 Statement (java.sql.Statement)34 SQLException (java.sql.SQLException)30 PreparedStatement (java.sql.PreparedStatement)23 PooledConnection (javax.sql.PooledConnection)23 ResultSet (java.sql.ResultSet)22 CallableStatement (java.sql.CallableStatement)18 Test (org.junit.Test)18 XAException (javax.transaction.xa.XAException)16 Transaction (javax.transaction.Transaction)8 DataSource (javax.sql.DataSource)7 XidImpl (org.firebirdsql.jca.TestXABase.XidImpl)7 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)7 NotSupportedException (javax.transaction.NotSupportedException)5 ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)4 J2EEDataSource (org.apache.derbyTesting.junit.J2EEDataSource)4