Search in sources :

Example 46 with XAConnection

use of javax.sql.XAConnection in project derby by apache.

the class XATransactionTest method testGlobalXIDinTransactionTable.

/**
 * Tests whether it is possible to reconstruct the original Xid value
 * correctly from SYSCS_DIAG.TRANSACTION_TABLE.
 */
public void testGlobalXIDinTransactionTable() throws Exception {
    Statement stm = getConnection().createStatement();
    stm.execute("create table XATT2 (i int, text char(10))");
    XADataSource xaDataSource = J2EEDataSource.getXADataSource();
    XAConnection xaConn = xaDataSource.getXAConnection();
    XAResource xaRes = xaConn.getXAResource();
    Connection conn = xaConn.getConnection();
    // create large enough xid
    byte[] gid = new byte[64];
    byte[] bid = new byte[64];
    for (int i = 0; i < 64; i++) {
        gid[i] = (byte) i;
        bid[i] = (byte) (64 - i);
    }
    Xid xid = XATestUtil.getXid(0x1234, gid, bid);
    // get the stuff required to execute the global transaction
    xaConn = xaDataSource.getXAConnection();
    xaRes = xaConn.getXAResource();
    conn = xaConn.getConnection();
    // start the transaction with that xid
    xaRes.start(xid, XAResource.TMNOFLAGS);
    // do some work
    stm = conn.createStatement();
    stm.execute("insert into XATT2 values (1234, 'Test_Entry')");
    stm.close();
    // end the wotk on the transaction branch
    xaRes.end(xid, XAResource.TMSUCCESS);
    ResultSet rs = null;
    stm = null;
    try {
        // check the output of the global xid in
        // syscs_diag.transaction_table
        stm = getConnection().createStatement();
        String query = "select global_xid from syscs_diag.transaction_table" + " where global_xid is not null";
        // execute the query to obtain the xid of the global transaction
        rs = stm.executeQuery(query);
        // there should be at least one globaltransaction in progress
        assertTrue(rs.next());
        // check whether the xid obtained matches the original xid
        Xid rXid = parseXid(rs.getString(1));
        assertEquals(xid, rXid);
        // there should be at most one global transaction in progress
        assertFalse(rs.next());
    } catch (Exception ex) {
        try {
            // close all the stuff
            if (rs != null)
                rs.close();
            if (stm != null)
                stm.close();
            // rollback the global transaction
            xaRes.rollback(xid);
            // close the connection
            xaConn.close();
        } catch (Exception e) {
        // ignore the exception because it
        // would hide the original exception
        }
        // throw the stuff further
        throw ex;
    }
    // close all the stuff
    rs.close();
    stm.close();
    // rollback the global transaction
    xaRes.rollback(xid);
    // close the connection
    xaConn.close();
}
Also used : XADataSource(javax.sql.XADataSource) XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) Statement(java.sql.Statement) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) ResultSet(java.sql.ResultSet) SQLException(java.sql.SQLException) XAException(javax.transaction.xa.XAException) XAConnection(javax.sql.XAConnection)

Example 47 with XAConnection

use of javax.sql.XAConnection in project derby by apache.

the class XATransactionTest method testPendingLocalTranAndServerShutdown.

/**
 * This test does following
 * 1)Start the network server
 * 2)Start a local xa transaction
 * 3)Do not commit the local XA transaction
 * 4)Shutdown the network server
 * 5)Start the server again
 *
 * Before the fix for DERBY-4053 went in, step 4) would not shutdown the
 * server properly because of the pending local XA transaction. During the
 * server shutdown, we try to close all the open connections but the close
 * on the XA connection results into an exception because there is still a
 * pending transaction. That exception is not handled by the server and
 * because of that, all the code necessary to shutdown the server is not
 * executed. The next time around, step 5), when we try to bring up the
 * server again, it ends up hanging
 * 2009-07-09 21:21:28.828 GMT : Invalid reply from network server: Insufficient data.
 * 2009-07-09 21:21:28.843 GMT : Could not listen on port 1527 on host 127.0.0.1: java.net.BindException: Address already in use: JVM_Bind
 *
 * The fix for DERBY-4053 makes sure that before calling close on local XA
 * transaction, we first rollback any transaction active on the
 * connection.
 */
public void testPendingLocalTranAndServerShutdown() throws Exception {
    if (usingEmbedded())
        return;
    // 1)Server must be up already through the Derby junit framework
    // 2)Start a local xa transaction
    XADataSource xaDataSource = J2EEDataSource.getXADataSource();
    XAConnection xaconn = xaDataSource.getXAConnection();
    XAResource xar = xaconn.getXAResource();
    Connection conn = xaconn.getConnection();
    Statement s = conn.createStatement();
    s.executeUpdate("create table tab(i int)");
    s.executeUpdate("insert into tab values (1),(2),(3),(4)");
    conn.commit();
    conn.setAutoCommit(false);
    ResultSet rs = s.executeQuery("select * from tab");
    rs.next();
    // 3)Do not commit this pending local XA transaction
    // 4)Shutdown the network server
    // bring the server down while the local xa transaction is still active
    TestConfiguration.getCurrent().stopNetworkServer();
    // 5)Start the server again
    TestConfiguration.getCurrent().startNetworkServer();
}
Also used : XADataSource(javax.sql.XADataSource) XAResource(javax.transaction.xa.XAResource) Statement(java.sql.Statement) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) ResultSet(java.sql.ResultSet) XAConnection(javax.sql.XAConnection)

Example 48 with XAConnection

use of javax.sql.XAConnection in project derby by apache.

the class ConnectionMethodsTest method testAbortXA.

/**
 * Test the JDBC 4.1 Connection.abort(Executor) method on XA connections.
 */
public void testAbortXA() throws Exception {
    // 
    if (!TestConfiguration.loadingFromJars()) {
        return;
    }
    XADataSource xads = J2EEDataSource.getXADataSource();
    XAConnection conn0 = getXAConnection(xads, "user0");
    XAConnection conn1 = getXAConnection(xads, "user1");
    XAConnection conn2 = getXAConnection(xads, "user2");
    abortVetter(conn0.getConnection(), conn1.getConnection(), conn2.getConnection());
    // verify that the underlying physical connection is closed
    try {
        conn1.getConnection();
        fail("Expected physical connection to be closed.");
    } catch (SQLException se) {
        assertSQLState(CLOSED_CONNECTION, se);
    }
}
Also used : XADataSource(javax.sql.XADataSource) SQLException(java.sql.SQLException) XAConnection(javax.sql.XAConnection)

Example 49 with XAConnection

use of javax.sql.XAConnection in project derby by apache.

the class VerifySignatures method collectClassesFromXADataSource.

/**
 * Obtain a connection from an <code>XADataSource</code> object
 * and perform JDBC operations on it. Collect the classes of all
 * JDBC objects that are found.
 *
 * @param classes set into which classes are collected
 * @exception SQLException if a database error occurs
 */
private static void collectClassesFromXADataSource(Set<ClassInfo> classes) throws SQLException {
    XADataSource xads = J2EEDataSource.getXADataSource();
    addClass(classes, xads.getClass(), javax.sql.XADataSource.class);
    XAConnection xaconn = xads.getXAConnection(TestConfiguration.getCurrent().getUserName(), TestConfiguration.getCurrent().getUserPassword());
    addClass(classes, xaconn.getClass(), javax.sql.XAConnection.class);
    collectClassesFromConnection(xaconn.getConnection(), classes);
}
Also used : XADataSource(javax.sql.XADataSource) XAConnection(javax.sql.XAConnection)

Example 50 with XAConnection

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

the class TestFBXADataSource method testInDistributed_rollback_savepoint.

/**
 * Test if calling rollback for savepoint throws an exception when participating in a distributed transaction (JDBC 4.0 section 12.4).
 */
@Test
public void testInDistributed_rollback_savepoint() throws Exception {
    XAConnection pc = getXAConnection();
    XAResource xa = pc.getXAResource();
    try (Connection con = pc.getConnection()) {
        assumeTrue("Test requires SAVEPOINT support", supportInfoFor(con).supportsSavepoint());
        Xid xid = new XidImpl();
        try {
            con.setAutoCommit(false);
            // Just to create one
            Savepoint savepoint = con.setSavepoint();
            // Required to make sure start() works.
            con.rollback();
            xa.start(xid, XAResource.TMNOFLAGS);
            expectedException.expect(SQLException.class);
            expectedException.expect(sqlStateEquals(SQLStateConstants.SQL_STATE_INVALID_TX_STATE));
            con.rollback(savepoint);
        } 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