Package javax.sql

Examples of javax.sql.XAConnection


        Connection connection1 = poolingDataSource1.getConnection();
        connection1.createStatement();

        Connection connection2 = poolingDataSource2.getConnection();
        JdbcConnectionHandle handle = (JdbcConnectionHandle) Proxy.getInvocationHandler(connection2);
        XAConnection xaConnection2 = (XAConnection) AbstractMockJdbcTest.getWrappedXAConnectionOf(handle.getPooledConnection());
        connection2.createStatement();

        Connection connection3 = poolingDataSource2.getConnection();
        connection3.createStatement();

        MockXAResource mockXAResource2 = (MockXAResource) xaConnection2.getXAResource();
        mockXAResource2.setPrepareException(createXAException("resource 2 prepare failed", XAException.XAER_RMERR));

        try {
            tm.commit();
            fail("TM should have thrown an exception");
View Full Code Here


        tm.begin();
        tm.setTransactionTimeout(10); // TX must not timeout

        Connection connection1 = poolingDataSource1.getConnection();
        JdbcConnectionHandle handle = (JdbcConnectionHandle) Proxy.getInvocationHandler(connection1);
        XAConnection xaConnection1 = (XAConnection) AbstractMockJdbcTest.getWrappedXAConnectionOf(handle.getPooledConnection());
        connection1.createStatement();

        Connection connection2 = poolingDataSourceLrc.getConnection();
        connection2.createStatement();
       
        MockXAResource mockXAResource1 = (MockXAResource) xaConnection1.getXAResource();
        mockXAResource1.setPrepareException(createXAException("resource 1 prepare failed", XAException.XAER_RMERR));
       
        try {
            tm.commit();
            fail("TM should have thrown an exception");
View Full Code Here

            assertSQLState("40XC0", sqle);
        }
        cs.setString(1, "Nested2");
        cs.execute();

        XAConnection xac = dsx.getXAConnection();
        // checks currently only implemented for embedded
        if (usingEmbedded())
        {
            SecurityCheck.assertSourceSecurity(xac, "javax.sql.XAConnection");
        }
        AssertEventCatcher aes3 = new AssertEventCatcher(3);
        xac.addConnectionEventListener(aes3);
        assertConnectionOK(
            expectedValues, "XADataSource", xac.getConnection());
        //Check if got connection closed event but not connection error event
        assertTrue(aes3.didConnectionClosedEventHappen());
        assertFalse(aes3.didConnectionErrorEventHappen());
        aes3.resetState();
                      
        pc = dsp.getPooledConnection();
        AssertEventCatcher aes2 = new AssertEventCatcher(2);
        pc.addConnectionEventListener(aes2);
        assertConnectionOK(
            expectedValues, "ConnectionPoolDataSource", pc.getConnection());
        //Check if got connection closed event but not connection error event
        assertTrue(aes2.didConnectionClosedEventHappen());
        assertFalse(aes2.didConnectionErrorEventHappen());
        aes2.resetState();

        // test "local" XAConnections
        xac = dsx.getXAConnection();
        AssertEventCatcher aes4 = new AssertEventCatcher(4);
        xac.addConnectionEventListener(aes4);
        assertConnectionOK(
            expectedValues, "XADataSource", xac.getConnection());
        //Check if got connection closed event but not connection error event
        assertTrue(aes4.didConnectionClosedEventHappen());
        assertFalse(aes4.didConnectionErrorEventHappen());
        aes4.resetState();
        assertConnectionOK(
            expectedValues, "XADataSource", xac.getConnection());
        //Check if got connection closed event but not connection error event
        assertTrue(aes4.didConnectionClosedEventHappen());
        assertFalse(aes4.didConnectionErrorEventHappen());
        aes4.resetState();
        xac.close();

        // test "global" XAConnections
        xac = dsx.getXAConnection();
        AssertEventCatcher aes5 = new AssertEventCatcher(5);
        xac.addConnectionEventListener(aes5);
        XAResource xar = xac.getXAResource();
        // checks currently only implemented for embedded
        if (usingEmbedded())
        {
           SecurityCheck.assertSourceSecurity(
                xar, "javax.transaction.xa.XAResource");
        }
        Xid xid = new cdsXid(1, (byte) 35, (byte) 47);
        xar.start(xid, XAResource.TMNOFLAGS);
        Connection xacc = xac.getConnection();
        xacc.close();
        expectedValues[0] = new Integer(ResultSet.CLOSE_CURSORS_AT_COMMIT);
        if (usingEmbedded())
            expectedValues[1] = "XJ058";
        expectedValues[3] = new Boolean(false);
        assertConnectionOK(
            expectedValues, "Global XADataSource", xac.getConnection());
        //Check if got connection closed event but not connection error event
        assertTrue(aes5.didConnectionClosedEventHappen());
        assertFalse(aes5.didConnectionErrorEventHappen());
        aes5.resetState();
        assertConnectionOK(
            expectedValues, "Global XADataSource", xac.getConnection());
        //Check if got connection closed event but not connection error event
        assertTrue(aes5.didConnectionClosedEventHappen());
        assertFalse(aes5.didConnectionErrorEventHappen());
        aes5.resetState();

        xar.end(xid, XAResource.TMSUCCESS);

        expectedValues[0] = new Integer(ResultSet.HOLD_CURSORS_OVER_COMMIT);
        expectedValues[3] = new Boolean(true);
        assertConnectionOK(expectedValues,
            "Switch to local XADataSource", xac.getConnection());
        //Check if got connection closed event but not connection error event
        assertTrue(aes5.didConnectionClosedEventHappen());
        assertFalse(aes5.didConnectionErrorEventHappen());
        aes5.resetState();
        assertConnectionOK(expectedValues,
            "Switch to local XADataSource", xac.getConnection());
        //Check if got connection closed event but not connection error event
        assertTrue(aes5.didConnectionClosedEventHappen());
        assertFalse(aes5.didConnectionErrorEventHappen());
        aes5.resetState();

        Connection backtoGlobal = xac.getConnection();

        xar.start(xid, XAResource.TMJOIN);
        expectedValues[0] = new Integer(ResultSet.CLOSE_CURSORS_AT_COMMIT);
        expectedValues[3] = new Boolean(false);
        assertConnectionOK(expectedValues,
            "Switch to global XADataSource", backtoGlobal);
        //Check if got connection closed event but not connection error event
        assertTrue(aes5.didConnectionClosedEventHappen());
        assertFalse(aes5.didConnectionErrorEventHappen());
        aes5.resetState();
        assertConnectionOK(expectedValues,
            "Switch to global XADataSource", xac.getConnection());
        //Check if got connection closed event but not connection error event
        assertTrue(aes5.didConnectionClosedEventHappen());
        assertFalse(aes5.didConnectionErrorEventHappen());
        aes5.resetState();
        xar.end(xid, XAResource.TMSUCCESS);
        xar.commit(xid, true);

        xac.close();
    }
View Full Code Here

    public void testClosedXADSConnection() throws SQLException, Exception {
        // verify that outstanding updates from a closed connection, obtained
        // from an XADataSource, are not committed, but rolled back.
        XADataSource dsx = J2EEDataSource.getXADataSource();
        JDBCDataSource.setBeanProperty(dsx, "DatabaseName", dbName);
        XAConnection xac = dsx.getXAConnection();
        Connection c1 = xac.getConnection();
        Statement s = c1.createStatement();

        c1.setAutoCommit(false);

        // this update should be rolled back
        s.executeUpdate("insert into intTable values(2)");
       
        c1 = xac.getConnection();

        ResultSet rs = c1.createStatement().executeQuery(
           "select count(*) from intTable");
        rs.next();

        assertEquals(0, rs.getInt(1));

        rs.close();
        c1.close();
        xac.close();
        xac = null;

        PoolReset("XADataSource", dsx.getXAConnection());
    }
View Full Code Here

        // and setting connection state.
        // some of this may be tested elsewhere too.

        XADataSource dsx = J2EEDataSource.getXADataSource();
        JDBCDataSource.setBeanProperty(dsx, "DatabaseName", dbName);
        XAConnection xac = dsx.getXAConnection();
        AssertEventCatcher aes6 = new AssertEventCatcher(6);
        xac.addConnectionEventListener(aes6);
        XAResource xar = xac.getXAResource();
        Xid xid = new cdsXid(1, (byte) 93, (byte) 103);

        // series 1 - Single connection object
        Connection cs1 = xac.getConnection();
        // initial local
        assertConnectionState(
            ResultSet.HOLD_CURSORS_OVER_COMMIT,
            Connection.TRANSACTION_READ_COMMITTED,
            true, false, cs1);
        xar.start(xid, XAResource.TMNOFLAGS);
        // initial X1
        assertConnectionState(
            ResultSet.CLOSE_CURSORS_AT_COMMIT,
            Connection.TRANSACTION_READ_COMMITTED,
            false, false, cs1);
        cs1.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
        cs1.setReadOnly(true);
        setHoldability(cs1, false); // close cursors
        // modified X1
        boolean ReadOnly = false;
        // see DERBY-911, ReadOnly state different for Embedded/DerbyNetClient
        if (usingEmbedded())
            ReadOnly = true;
        assertConnectionState(
            ResultSet.CLOSE_CURSORS_AT_COMMIT,
            Connection.TRANSACTION_READ_UNCOMMITTED,
            false, ReadOnly, cs1);
        xar.end(xid, XAResource.TMSUCCESS);
        // the underlying local transaction/connection must pick up the
        // state of the Connection handle cs1
        // modified local:
        assertConnectionState(
            ResultSet.CLOSE_CURSORS_AT_COMMIT,
            Connection.TRANSACTION_READ_UNCOMMITTED,
            true, ReadOnly, cs1);
       
        cs1.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        cs1.setReadOnly(false);
        setHoldability(cs1, false); // close cursors

        // reset local
        assertConnectionState(
            ResultSet.CLOSE_CURSORS_AT_COMMIT,
            Connection.TRANSACTION_READ_COMMITTED,
            true, false, cs1);

        // now re-join the transaction, should pick up the read-only
        // and isolation level from the transaction,
        // holdability remains that of this handle.
        xar.start(xid, XAResource.TMJOIN);
        // re-join X1
        assertConnectionState(
            ResultSet.CLOSE_CURSORS_AT_COMMIT,
            Connection.TRANSACTION_READ_UNCOMMITTED,
            false, ReadOnly, cs1);
        xar.end(xid, XAResource.TMSUCCESS);

        // back to local - should be the same as the reset local
        assertConnectionState(
            ResultSet.CLOSE_CURSORS_AT_COMMIT,
            Connection.TRANSACTION_READ_COMMITTED,
            true, false, cs1);
       
        // test suspend/resume
        // now re-join the transaction (X1) for the second time, should pick
        // up the read-only and isolation level from the transaction,
        // holdability remains that of this handle.
        xar.start(xid, XAResource.TMJOIN);
        assertConnectionState(
            ResultSet.CLOSE_CURSORS_AT_COMMIT,
            Connection.TRANSACTION_READ_UNCOMMITTED,
            false, ReadOnly, cs1);
       
        xar.end(xid, XAResource.TMSUSPEND);
        // local after suspend
        assertConnectionState(
            ResultSet.CLOSE_CURSORS_AT_COMMIT,
            Connection.TRANSACTION_READ_COMMITTED,
            true, false, cs1);
       
        xar.start(xid, XAResource.TMRESUME);
        // resume X1
        assertConnectionState(
            ResultSet.CLOSE_CURSORS_AT_COMMIT,
            Connection.TRANSACTION_READ_UNCOMMITTED,
            false, ReadOnly, cs1);
       
        xar.end(xid, XAResource.TMSUCCESS);
        // back to local (second time)
        assertConnectionState(
            ResultSet.CLOSE_CURSORS_AT_COMMIT,
            Connection.TRANSACTION_READ_COMMITTED,
            true, false, cs1);
       
        cs1.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
        cs1.setReadOnly(true);
        setHoldability(cs1, true); // hold
        //Confirm - no connection closed event & connection error event
        assertFalse(aes6.didConnectionClosedEventHappen());
        assertFalse(aes6.didConnectionErrorEventHappen());
        aes6.resetState();
        cs1.close();
        //Check if got connection closed event but not connection error event
        assertTrue(aes6.didConnectionClosedEventHappen());
        assertFalse(aes6.didConnectionErrorEventHappen());
        aes6.resetState();
       
        cs1 = xac.getConnection();
        // new handle - local
        assertConnectionState(
            ResultSet.HOLD_CURSORS_OVER_COMMIT,
            Connection.TRANSACTION_READ_COMMITTED,
            true, false, cs1);
        cs1.close();
        //Check if got connection closed event but not connection error event
        assertTrue(aes6.didConnectionClosedEventHappen());
        assertFalse(aes6.didConnectionErrorEventHappen());
        aes6.resetState();
       
        xar.start(xid, XAResource.TMJOIN);
        cs1 = xac.getConnection();
        // re-join with new handle X1
        assertConnectionState(
            ResultSet.CLOSE_CURSORS_AT_COMMIT,
            Connection.TRANSACTION_READ_UNCOMMITTED,
            false, ReadOnly, cs1);
        cs1.close();
        xar.end(xid, XAResource.TMSUCCESS);
        //Check if got connection closed event but not connection error event
        assertTrue(aes6.didConnectionClosedEventHappen());
        assertFalse(aes6.didConnectionErrorEventHappen());
        aes6.resetState();

        // now get a connection (attached to a local)
        // attach to the global and commit it.
        // state should be that of the local after the commit.
        cs1 = xac.getConnection();
        cs1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
        // pre-X1 commit - local
        assertConnectionState(
            ResultSet.HOLD_CURSORS_OVER_COMMIT,
            Connection.TRANSACTION_REPEATABLE_READ,
View Full Code Here

        // DERBY-421 Setting isolation level with SQL was not getting
        // handled correctly
        // Some more isolation testing using SQL and JDBC api
        XADataSource dsx = J2EEDataSource.getXADataSource();
        JDBCDataSource.setBeanProperty(dsx, "DatabaseName", dbName);
        XAConnection xac = dsx.getXAConnection();
        AssertEventCatcher aes6 = new AssertEventCatcher(6);
        xac.addConnectionEventListener(aes6);
        XAResource xar = xac.getXAResource();
        Connection conn = xac.getConnection();
        Statement s = conn.createStatement();
        // initial local
        assertConnectionState(
            ResultSet.HOLD_CURSORS_OVER_COMMIT,
            Connection.TRANSACTION_READ_COMMITTED,
View Full Code Here

            ResultSet.FETCH_REVERSE, 999, 137, 85,
            ResultSet.HOLD_CURSORS_OVER_COMMIT};

        XADataSource dsx = J2EEDataSource.getXADataSource();
        JDBCDataSource.setBeanProperty(dsx, "DatabaseName", dbName);
        XAConnection xac = dsx.getXAConnection();
        AssertEventCatcher aes6 = new AssertEventCatcher(6);
        xac.addConnectionEventListener(aes6);
        XAResource xar = xac.getXAResource();
        Xid xid = new cdsXid(1, (byte) 103, (byte) 119);

        // now check re-use of *Statement objects across local/global
        // connections.
        Connection cs1 = xac.getConnection();

        // ensure read locks stay around until end-of transaction
        cs1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
        cs1.setAutoCommit(false);
        assertLocks(null, cs1);

        Statement sru1 = cs1.createStatement();
        sru1.setCursorName("SN1");
        sru1.executeUpdate("insert into intTable values 1,2,3");
        Statement sruBatch = cs1.createStatement();
        sruBatch.setCursorName("sruBatch");
        Statement sruState = createFloatStatementForStateChecking(
            StatementExpectedValues, cs1);
        PreparedStatement psruState = createFloatStatementForStateChecking(
            new int[] {1, 4}, PreparedStatementExpectedValues, cs1,
            "select i from intTable where i = ?");
        CallableStatement csruState = createFloatCallForStateChecking(
            new int[] {2, 12, 12}, CallableStatementExpectedValues, cs1,
            "CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?,?)");
        PreparedStatement psParams =
            cs1.prepareStatement("select * from intTable where i > ?");
        psParams.setCursorName("params");
        psParams.setInt(1, 2);
        // Params-local-1
        resultSetQuery("params", three, psParams.executeQuery());

        sruBatch.addBatch("insert into intTable values 4");
        // sru1-local-1
        queryOnStatement("SN1", onetwothree, cs1, sru1);
        cs1.commit(); // need to commit to switch to an global connection;

        // simple case - underlying connection is re-used for global.
        xar.start(xid, XAResource.TMNOFLAGS);
        // Expecting downgrade because global transaction sru1-global-2 is
        // using a statement with holdability true
        // sru1-global-2
        queryOnStatement("SN1", onetwothree, cs1, sru1);
        sruBatch.addBatch("insert into intTable values 5");
        Statement sru2 = cs1.createStatement();
        sru2.setCursorName("OAK2");
        //sru2-global-3
        queryOnStatement("OAK2", onetwothree, cs1, sru2);
        // Expecting downgrade because global transaction sru1-global-4 is
        // using a statement with holdability true
        // sru1-global-4
        queryOnStatement("SN1", onetwothree, cs1, sru1);
        // Global statement
        StatementExpectedValues[6] = ResultSet.CLOSE_CURSORS_AT_COMMIT;
        PreparedStatementExpectedValues[6] = ResultSet.CLOSE_CURSORS_AT_COMMIT;
        CallableStatementExpectedValues[6] = ResultSet.CLOSE_CURSORS_AT_COMMIT;
        assertStatementState(null, StatementExpectedValues ,sruState);
        // Global PreparedStatement
        assertStatementState(pspc, PreparedStatementExpectedValues, psruState);
        // Global CallableStatement
        assertStatementState(cspc, CallableStatementExpectedValues, csruState);
        // Params-global-1
        resultSetQuery("params", three, psParams.executeQuery());

        xar.end(xid, XAResource.TMSUCCESS);
        // now a new underlying connection is created
        // sru1-local-5
        queryOnStatement("SN1", onetwothree, cs1, sru1);
        // sru2-local-6
        queryOnStatement("OAK2", onetwothree, cs1, sru2);
        sruBatch.addBatch("insert into intTable values 6,7");
        Statement sru3 = cs1.createStatement();
        sru3.setCursorName("SF3");
        // sru3-local-7
        queryOnStatement("SF3", onetwothree, cs1, sru3);
        // Two transactions should hold locks (global and the current XA);
        // LOCAL
        StatementExpectedValues[6] = ResultSet.HOLD_CURSORS_OVER_COMMIT;
        PreparedStatementExpectedValues[6] = ResultSet.HOLD_CURSORS_OVER_COMMIT;
        CallableStatementExpectedValues[6] = ResultSet.HOLD_CURSORS_OVER_COMMIT;
        assertStatementState(null, StatementExpectedValues, sruState);
        assertStatementState(pspc, PreparedStatementExpectedValues, psruState);
        assertStatementState(cspc, CallableStatementExpectedValues, csruState);
        // Params-local-2
        resultSetQuery("params", three, psParams.executeQuery());
        assertLocks(new int[] {14,14}, cs1);
        cs1.commit();
        //Confirm - no connection closed event & connection error event
        assertFalse(aes6.didConnectionClosedEventHappen());
        assertFalse(aes6.didConnectionErrorEventHappen());
        aes6.resetState();

        // attach the XA transaction to another connection and see what happens
        XAConnection xac2 = dsx.getXAConnection();
        AssertEventCatcher aes5 = new AssertEventCatcher(5);
        xac2.addConnectionEventListener(aes5);
        XAResource xar2 = xac2.getXAResource();

        xar2.start(xid, XAResource.TMJOIN);
        Connection cs2 = xac2.getConnection();

        // these statements were generated by cs1 and thus are still
        // in a local connection.
        // sru1-local-8
        queryOnStatement("SN1", onetwothree, cs1, sru1);
        // sru2-local-9
        queryOnStatement("OAK2", onetwothree, cs1, sru2);
        // sru3-local-10
        queryOnStatement("SF3", onetwothree, cs1, sru3);
        sruBatch.addBatch("insert into intTable values 8");
        // LOCAL 2
        assertStatementState(null, StatementExpectedValues, sruState);
        assertStatementState(pspc, PreparedStatementExpectedValues, psruState);
        assertStatementState(cspc, CallableStatementExpectedValues, csruState);

        assertLocks(new int[] {14, 12}, cs1);

        int[] updateCounts = sruBatch.executeBatch();
        int[] expectedUpdateCounts = {1, 1, 2, 1};
        // sruBatch update counts:
        for (int i = 0; i < updateCounts.length; i++) {
            assertEquals(expectedUpdateCounts[i], updateCounts[i]);
        }
        // sruBatch
        queryOnStatement(
            "sruBatch", new int[] {1,2,3,4,5,6,7,8}, cs1, sruBatch);

        xar2.end(xid, XAResource.TMSUCCESS);
        //Confirm - no connection closed event & connection error event
        assertFalse(aes5.didConnectionClosedEventHappen());
        assertFalse(aes5.didConnectionErrorEventHappen());
        aes5.resetState();
        xac2.close();

        // allow close on already closed XAConnection
        xac2.close();
        xac2.addConnectionEventListener(null);
        xac2.removeConnectionEventListener(null);

        // test methods against a closed XAConnection and its resource
        try {
            xac2.getXAResource();
            // DERBY-2532
            // Network Server does not think this is worth an exception.
            if (usingEmbedded())
                fail("expected SQLException on " +
                    "closed XAConnection.getXAResource");
        } catch (SQLException sqle) {
            assertSQLState("08003", sqle);
        }
        try {
            xac2.getConnection();
            fail ("expected SQLException on XAConnection.getConnection");
        } catch (SQLException sqle) {
            assertSQLState("08003", sqle);
        }
        try {
            xar2.start(xid, XAResource.TMJOIN);
            fail ("expected XAException on XAResource.TMJOIN");
        } catch (XAException xae) {
            assertXAException("XAResource.start", xae);
        }
        try {
            xar2.end(xid, XAResource.TMJOIN);
            fail ("expected XAException on XAResource.TMJOIN");
        } catch (XAException xae) {
            assertXAException("XAResource.end", xae);
        }
        try {
            xar2.commit(xid, true);
            fail ("expected XAException on XAResource.commit");
        } catch (XAException xae) {
            assertXAException("XAResource.commit", xae);
        }
        try {
            xar2.prepare(xid);
            fail ("expected XAException on XAResource.prepare");
        } catch (XAException xae) {
            assertXAException("XAResource.prepare", xae);
        }
        try {
            xar2.recover(0);
            fail ("expected XAException on XAResource.recover");
        } catch (XAException xae) {
            assertXAException("XAResource.recover", xae);
        }
        try {
            xar2.prepare(xid);
            fail ("expected XAException on XAResource.prepare");
        } catch (XAException xae) {
            assertXAException("XAResource.prepare", xae);
        }
        try {
            xar2.isSameRM(xar2);
            fail ("expected XAException on XAResource.isSameRM");
        } catch (XAException xae) {
            assertXAException("XAResource.isSameRM", xae);
        }
       
        // close everything
        cs1.rollback();
        sruState.close();
        psruState.close();
        csruState.close();
        psParams.close();
        sruBatch.close();
        sru1.close();
        sru2.close();
        sru3.close();
        cs1.close();
        cs2.close();
        xac.removeConnectionEventListener(null);
        xac.close();
        xac2.close();
       
        // but, still not enough.
        // what with all the switching between global and local transactions
        // we still have a lock open on intTable, which will interfere with
        // our tearDown efforts. Bounce the database.
View Full Code Here

   
    public void testSetSchemaInXAConnection() throws SQLException {
        // tests that set schema works correctly in an XA connection.

        XADataSource dsx = J2EEDataSource.getXADataSource();
        XAConnection xac3 = dsx.getXAConnection();
        Connection conn3 = xac3.getConnection();
        Statement st3 = conn3.createStatement();
        st3.execute("SET SCHEMA SCHEMA_Patricio");
        st3.close();

        PreparedStatement ps3 =
            conn3.prepareStatement("INSERT INTO Patricio VALUES (?, ?)");
        ps3.setString(1, "Patricio");
        ps3.setInt(2, 3);
        ps3.executeUpdate();

        assertEquals(1, ps3.getUpdateCount());
        ps3.close();
        conn3.close();
        xac3.close();
    }
View Full Code Here

    // auto-commit is true did not implictly commit any transaction
    // Also tests DERBY-1025, same description, but for client.
    public void testAutoCommitOnXAResourceStart() throws SQLException, XAException {

        XADataSource dsx = J2EEDataSource.getXADataSource();
        XAConnection xac4 = dsx.getXAConnection();
        Xid xid4a= null;

        // We get an XAID_DUP error from networkserver when attempting
        // the XAResource.start below if we use the same xid.
        // Possibly because we're in the same jvm.
        // When the test is run with clientserverSuite, rather than default,
        // this wasn't needed, so just create a different id for client
        if (usingEmbedded())
            xid4a = new cdsXid(4, (byte) 23, (byte) 76);
        else if (usingDerbyNetClient())
            xid4a = new cdsXid(5, (byte) 23, (byte) 76);
           
        Connection conn4 = xac4.getConnection();
        assertTrue(conn4.getAutoCommit());

        Statement s4 = conn4.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT);
        ResultSet rs4 = s4.executeQuery("select i from autocommitxastart");
        rs4.next();
        assertEquals(1, rs4.getInt(1));
        rs4.next();
        assertEquals(2, rs4.getInt(1));

        // XAResource().start should commit the transaction
        xac4.getXAResource().start(xid4a, XAResource.TMNOFLAGS);
        xac4.getXAResource().end(xid4a, XAResource.TMSUCCESS);

        try {
            rs4.next();
            fail ("expected an exception indicating resultset is closed.");
        } catch (SQLException sqle) {
            // Embedded gets 08003. No current connection (DERBY-2620)         
          if (usingDerbyNetClient())
            assertSQLState("XCL16",sqle);
        }

        conn4.setAutoCommit(false);
        assertFalse(conn4.getAutoCommit());

        rs4 = s4.executeQuery("select i from autocommitxastart");
        rs4.next();
        assertEquals(1, rs4.getInt(1));
        rs4.next();
        assertEquals(2, rs4.getInt(1));
       
         // Get a new xid to begin another transaction.
        if (usingEmbedded())
            xid4a = new cdsXid(4, (byte) 93, (byte) 103);
        else if (usingDerbyNetClient())
            xid4a = new cdsXid(5, (byte) 93, (byte) 103);

        try {
            xac4.getXAResource().start(xid4a, XAResource.TMNOFLAGS);
        } catch (XAException xae) {
            if (usingEmbedded())
                assertNull(xae.getMessage());
            else if (usingDerbyNetClient())
            {
                // This should give XAER_OUTSIDE exception because
                // the resource manager is busy in the local transaction
                assertTrue(xae.getMessage().indexOf("XAER_OUTSIDE") >=0 );
            }
            assertEquals(-9, xae.errorCode);
        }
       
        rs4.next();
        assertEquals(3, rs4.getInt(1));
        rs4.close();

        conn4.rollback();
        conn4.close();
        xac4.close();
    }
View Full Code Here

        // 2)finish that read-only transaction
        // 3)start another global transaction

        XADataSource dsx = J2EEDataSource.getXADataSource();
        JDBCDataSource.setBeanProperty(dsx, "DatabaseName", dbName);
        XAConnection xac5 = dsx.getXAConnection();
        Xid xid5a = new cdsXid(5, (byte) 119, (byte) 129);
        Connection conn5 = xac5.getConnection();
        Statement sru5a = conn5.createStatement();
        XAResource xar = xac5.getXAResource();
        xar.start(xid5a, XAResource.TMNOFLAGS);
        conn5.setReadOnly(true);

        // Read-Only XA transaction;
        // holdability: (hold, or close cursors over commit) ,
        // transaction isolation: read-committed,
        // auto-commit false, read-only true (with embedded)
        if (usingEmbedded())
        {
            assertConnectionState(
                ResultSet.CLOSE_CURSORS_AT_COMMIT,
                Connection.TRANSACTION_READ_COMMITTED,
                false, true, conn5);
        }
        // Note: the original test had no comments about this difference
        //       between Embedded and DerbyNetClient, this has apparently
        //       been accepted behavior.
        else if (usingDerbyNetClient())
        {
            assertConnectionState(
                ResultSet.CLOSE_CURSORS_AT_COMMIT,
                Connection.TRANSACTION_READ_COMMITTED,
                false, false, conn5);
        }
       
        ResultSet rs5 = sru5a.executeQuery(
            "select count(*) from autocommitxastart");
        rs5.next();
        assertEquals(5, rs5.getInt(1));
        rs5.close();
        xar.end(xid5a, XAResource.TMSUCCESS);
        xar.commit(xid5a, true);
        conn5.close();
       
        //now start a new transaction
        conn5 = xac5.getConnection();
        sru5a = conn5.createStatement();
        xar.start(xid5a, XAResource.TMNOFLAGS);
       
        // Writeable XA transaction
        // holdability: (hold, or close cursors over commit) ,
        // transaction isolation: read-committed,
        // auto-commit false, read-only false
        assertConnectionState(
                ResultSet.CLOSE_CURSORS_AT_COMMIT,
                Connection.TRANSACTION_READ_COMMITTED,
                false, false, conn5);
        sru5a.executeUpdate("insert into autocommitxastart values 6,7");
        rs5 = sru5a.executeQuery("select count(*) from autocommitxastart");
        rs5.next();
        assertEquals(7, rs5.getInt(1));
        xar.end(xid5a, XAResource.TMSUCCESS);
        xar.commit(xid5a, true);
        conn5.close();
        xac5.close();
        sru5a.close();
    }
View Full Code Here

TOP

Related Classes of javax.sql.XAConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.