Search in sources :

Example 86 with XAConnection

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

the class Derby5165Test method launchInsert.

public void launchInsert() throws Exception {
    String tableName = "d5165t3";
    // setup to setup for update
    // open a connection to the database
    Connection simpleconn = getConnection();
    setAutoCommit(false);
    createAndLoadTable(simpleconn, tableName, true);
    // step 0 - initialize db and xa constructs
    XADataSource xads = J2EEDataSource.getXADataSource();
    J2EEDataSource.setBeanProperty(xads, "databaseName", "d5165db4");
    XAConnection xac = xads.getXAConnection();
    XAResource xar = xac.getXAResource();
    Connection conn = xac.getConnection();
    // step 1 - perform insert with XA, using Xid xid1
    Statement s = conn.createStatement();
    JDBC.assertSingleValueResultSet(s.executeQuery("select * from " + tableName), "1");
    conn.close();
    s.close();
    Xid xid1 = new MyXid(1, 2, 3);
    xar.start(xid1, XAResource.TMNOFLAGS);
    Connection c1 = xac.getConnection();
    Statement s1 = c1.createStatement();
    // insert
    s1.execute("insert into " + tableName + " values 2");
    xar.end(xid1, XAResource.TMSUCCESS);
    // step 2-prepare xid1 with XA but do NOT commit
    xar.prepare(xid1);
// doing nothing further should stop this jvm process.
}
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) XAConnection(javax.sql.XAConnection)

Example 87 with XAConnection

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

the class Derby5165Test method launchUpdate.

public void launchUpdate() throws Exception {
    // setup to setup for update
    // open a connection to the database
    Connection simpleconn = getConnection();
    setAutoCommit(false);
    String tableName = "d5165t2";
    createAndLoadTable(simpleconn, tableName, true);
    // step 0 - initialize db and xa constructs
    XADataSource xads = J2EEDataSource.getXADataSource();
    J2EEDataSource.setBeanProperty(xads, "databaseName", "d5165db3");
    XAConnection xac = xads.getXAConnection();
    XAResource xar = xac.getXAResource();
    Connection conn = xac.getConnection();
    // step 1 - perform update with XA, using Xid xid1
    Statement s = conn.createStatement();
    JDBC.assertSingleValueResultSet(s.executeQuery("select * from " + tableName), "1");
    conn.close();
    s.close();
    Xid xid1 = new MyXid(1, 2, 3);
    xar.start(xid1, XAResource.TMNOFLAGS);
    Connection c1 = xac.getConnection();
    Statement s1 = c1.createStatement();
    s1.execute("update " + tableName + " set x = 2 where x = 1");
    xar.end(xid1, XAResource.TMSUCCESS);
    // step 2-prepare xid1 with XA but do NOT commit
    xar.prepare(xid1);
// doing nothing further should stop this jvm process.
}
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) XAConnection(javax.sql.XAConnection)

Example 88 with XAConnection

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

the class XAMemTest method testDerby4137_TransactionTimeoutSpecifiedNotExceeded.

/**
 * DERBY-4137: Execute a bunch of successful XA transactions with a
 * transaction timeout specified.
 *
 * @throws Exception if something goes wrong
 */
public void testDerby4137_TransactionTimeoutSpecifiedNotExceeded() throws Exception {
    XADataSource xads = J2EEDataSource.getXADataSource();
    XAConnection xac = xads.getXAConnection();
    XAResource xar = xac.getXAResource();
    Xid xid = XATestUtil.getXid(8, 9, 10);
    Connection con = xac.getConnection();
    Statement stmt = con.createStatement();
    // Set a long timeout such that the queue won't be able to clean
    // itself as part of normal processing.
    xar.setTransactionTimeout(100000);
    // addressed.
    for (int i = 0; i < 60000; i++) {
        xar.start(xid, XAResource.TMNOFLAGS);
        stmt.executeQuery("values 1");
        xar.end(xid, XAResource.TMSUCCESS);
        xar.commit(xid, true);
    }
    xac.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) XAConnection(javax.sql.XAConnection)

Example 89 with XAConnection

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

the class XATest method testDerby4310CallableStatement.

/**
 * This test checks the fix on DERBY-4310, for not repreparing CallableStatements
 * upon calling close() on them.
 */
public void testDerby4310CallableStatement() throws SQLException, XAException {
    XADataSource xads = J2EEDataSource.getXADataSource();
    J2EEDataSource.setBeanProperty(xads, "databaseName", "wombat");
    XAConnection xaconn = xads.getXAConnection();
    XAResource xar = xaconn.getXAResource();
    Xid xid = XATestUtil.getXid(1, 93, 18);
    /* Create the procedure bazed on XATest.zeroArg() */
    Connection conn = xaconn.getConnection();
    Statement s = conn.createStatement();
    s.executeUpdate("CREATE PROCEDURE ZA() LANGUAGE JAVA " + "EXTERNAL NAME 'org.apache.derbyTesting.functionTests.tests.jdbcapi.XATest.zeroArg' " + "PARAMETER STYLE JAVA");
    conn.commit();
    /* Prepare and execute CallableStatement based on the procedure above */
    CallableStatement cs = conn.prepareCall("CALL ZA()");
    cs.execute();
    /* Start and end a transaction on the XAResource object */
    xar.start(xid, XAResource.TMNOFLAGS);
    xar.end(xid, XAResource.TMSUCCESS);
    /* Drop the procedure on a parallel, regular connection */
    Connection conn2 = getConnection();
    Statement s2 = conn2.createStatement();
    s2.execute("DROP PROCEDURE ZA");
    conn2.commit();
    conn2.close();
    try {
        /* Try to close the prepared statement. This would throw an exception
             * before the fix, claiming that the table was not found. */
        cs.close();
    } finally {
        /* Rollback the transaction and close the connections */
        xar.rollback(xid);
        conn.close();
        xaconn.close();
    }
}
Also used : XADataSource(javax.sql.XADataSource) XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) XAConnection(javax.sql.XAConnection)

Example 90 with XAConnection

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

the class XATest method testXALockTimeout.

/**
 * DERBY-5552 Check that lock timeout does not destroy connection
 * during an XA Transaction.
 *
 * @throws SQLException
 * @throws XAException
 */
public void testXALockTimeout() throws SQLException, XAException {
    XADataSource xads = J2EEDataSource.getXADataSource();
    J2EEDataSource.setBeanProperty(xads, "databaseName", "wombat");
    // Get the first connection and lock table in
    // xa transaction
    XAConnection xaconn = xads.getXAConnection();
    XAResource xar = xaconn.getXAResource();
    Xid xid = XATestUtil.getXid(998, 10, 50);
    Connection conn = xaconn.getConnection();
    Statement s = conn.createStatement();
    xar.start(xid, XAResource.TMNOFLAGS);
    s.executeUpdate("INSERT INTO TABLT VALUES(2)");
    // Get a second connection and global xact
    // and try to select causing lock timeout
    XAConnection xaconn2 = xads.getXAConnection();
    XAResource xar2 = xaconn2.getXAResource();
    Xid xid2 = XATestUtil.getXid(999, 11, 51);
    Connection conn2 = xaconn2.getConnection();
    // Set to serializable so we get lock timeout
    conn2.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
    xar2.start(xid2, XAResource.TMNOFLAGS);
    Statement s2 = conn2.createStatement();
    s2.executeUpdate("INSERT INTO TABLT VALUES(3)");
    assertGlobalXactCount(2);
    try {
        ResultSet rs = s2.executeQuery("SELECT * FROM TABLT");
        fail("Should have gotten lock timeout error: " + LOCKTIMEOUT);
    } catch (SQLException se) {
        assertSQLState(LOCKTIMEOUT, se);
    }
    // After the lock timeout we just have one global transaction.
    // lock timeout implicitly rolled back xid2
    assertGlobalXactCount(1);
    assertConnOK(conn);
    // DERBY-5552 Make sure connection is ok after lock timeout
    assertConnOK(conn2);
    // Should be able to end and commit xid1
    xar.end(xid, XAResource.TMSUCCESS);
    xar.prepare(xid);
    xar.commit(xid, false);
    // xid2 should have already been rolled back so end should fail
    try {
        xar2.end(xid2, XAResource.TMSUCCESS);
        fail("Should have gotten exception ending xid2");
    } catch (XAException xae) {
        // xae.printStackTrace();
        assertEquals(XAException.XA_RBTIMEOUT, xae.errorCode);
    }
    // Should have no locks on TABLT
    Statement drops = createStatement();
    drops.executeUpdate("DROP TABLE TABLT");
    // verify there are no more global transactions
    assertGlobalXactCount(0);
    // Need to explicitly rollback xid2 as it ended with
    // an implicit rollback XA_RBTIMEOUT
    xar2.rollback(xid2);
    // Make sure both connections can be used to make a new global xact
    xar.start(xid, XAResource.TMNOFLAGS);
    s.executeUpdate("CREATE TABLE TABLT (I INT)");
    s.executeUpdate("INSERT INTO TABLT VALUES(1)");
    xar.end(xid, XAResource.TMSUCCESS);
    xar.prepare(xid);
    xar.commit(xid, false);
    // now the other connection ..
    xar2.start(xid2, XAResource.TMNOFLAGS);
    s2.executeUpdate("INSERT INTO TABLT VALUES(2)");
    xar2.end(xid2, XAResource.TMSUCCESS);
    xar.prepare(xid2);
    xar.commit(xid2, false);
    assertGlobalXactCount(0);
    conn.close();
    xaconn.close();
    conn2.close();
    xaconn2.close();
}
Also used : XADataSource(javax.sql.XADataSource) XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) XAException(javax.transaction.xa.XAException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) ResultSet(java.sql.ResultSet) 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