Package javax.sql

Examples of javax.sql.XAConnection


        // Test chinese database name.
        XADataSource ds = J2EEDataSource.getXADataSource();
        J2EEDataSource.setBeanProperty(ds, "databaseName", "\u4e10");
        J2EEDataSource.setBeanProperty(ds, "createDatabase", "create");       

        XAConnection xaconn = ds.getXAConnection();
        Connection conn = xaconn.getConnection();
        conn.close();
 
        // Chinese user
        J2EEDataSource.setBeanProperty(ds, "user", "\u4e10");
        xaconn = ds.getXAConnection();
        conn = xaconn.getConnection();
        conn.close();

        // Chinese password
        J2EEDataSource.setBeanProperty(ds, "password", "\u4e10");
        xaconn = ds.getXAConnection();
        conn = xaconn.getConnection();
        conn.close();
       
        /* Add the created database for cleanup by tearDown() */
        databasesForCleanup.add("\u4e10");
    }
View Full Code Here


        new XATransactionTester().run(args);
    }

    public void run(String[] args) throws Exception {
        ds = getDataSource(args);
        XAConnection xaConn = ds.getXAConnection("test", "test");
        XAResource xaRes = xaConn.getXAResource();
        manager = new TransactionManagerImpl(10, new DummyLog(), null);
        Connection c = xaConn.getConnection();
        Statement s = c.createStatement();

        manager.begin();
        manager.getTransaction().enlistResource(xaRes);
        s.execute("UPDATE XA_TEST SET X=X+1");
View Full Code Here

     * @throws XAException
     */
    private static void singleConnectionOnePhaseCommit(XADataSource xads) {
        System.out.println("singleConnectionOnePhaseCommit");
        try {
            XAConnection xac = xads.getXAConnection();

            XAResource xar = xac.getXAResource();

            Xid xid = XATestUtil.getXid(0, 32, 46);

            xar.start(xid, XAResource.TMNOFLAGS);

            Connection conn = xac.getConnection();
           
            showHoldStatus("XA ", conn);

            Statement s = conn.createStatement();
            showHoldStatus("XA ", s);

            s.execute("create table foo (a int)");
            s.executeUpdate("insert into foo values (0)");

            ResultSet rs = s.executeQuery("select * from foo");
            JDBCDisplayUtil.DisplayResults(System.out, rs, conn);
            rs.close();

            XATestUtil.showXATransactionView(conn);

            s.close();
            xar.end(xid, XAResource.TMSUCCESS);

            // 1 phase commit
            xar.commit(xid, true);

            conn.close();
            xac.close();

        } catch (SQLException sqle) {
            TestUtil.dumpSQLExceptions(sqle);
            sqle.printStackTrace(System.out);
        } catch (XAException e) {
View Full Code Here

     </code>
     */
    private static void interleavingTransactions(XADataSource xads) {
        System.out.println("interleavingTransactions");
        try {
            XAConnection xac = xads.getXAConnection("sku", "testxa");
            XAResource xar = xac.getXAResource();

            Xid xid1 = XATestUtil.getXid(1, 93, 18);
            Xid xid2 = XATestUtil.getXid(2, 45, 77);

            xar.start(xid1, XAResource.TMNOFLAGS);

            Connection conn = xac.getConnection();

            Statement s = conn.createStatement();
            s.executeUpdate("insert into APP.foo values (1)");
            xar.end(xid1, XAResource.TMSUSPEND);

            xar.start(xid2, XAResource.TMNOFLAGS);
            s.executeUpdate("insert into APP.foo values (2)");
            xar.end(xid2, XAResource.TMSUSPEND);

            xar.start(xid1, XAResource.TMRESUME);
            s.executeUpdate("insert into APP.foo values (3)");
            xar.end(xid1, XAResource.TMSUSPEND);

            xar.start(xid2, XAResource.TMRESUME);
            s.executeUpdate("insert into APP.foo values (4)");

            XATestUtil.showXATransactionView(conn);

            // this prepare won't work since
            // transaction 1 has been suspended - XA_PROTO
            try {
                xar.prepare(xid1);
                System.out.println("FAIL - prepare on suspended transaction");
            } catch (XAException e) {
                if (e.errorCode != XAException.XAER_PROTO)
                    XATestUtil.dumpXAException(
                            "FAIL - prepare on suspended transaction", e);

            }

            // check it was not prepared
            XATestUtil.showXATransactionView(conn);

            xar.end(xid2, XAResource.TMSUCCESS);

            xar.end(xid1, XAResource.TMSUCCESS);

            xar.prepare(xid1);
            xar.prepare(xid2);

            // both should be prepared.
            XATestUtil.showXATransactionView(conn);

            Xid[] recoveredStart = xar.recover(XAResource.TMSTARTRSCAN);
            System.out.println("recovered start " + recoveredStart.length);
            Xid[] recovered = xar.recover(XAResource.TMNOFLAGS);
            System.out.println("recovered " + recovered.length);
            Xid[] recoveredEnd = xar.recover(XAResource.TMENDRSCAN);
            System.out.println("recovered end " + recoveredEnd.length);

            for (int i = 0; i < recoveredStart.length; i++) {
                Xid xid = recoveredStart[i];
                if (xid.getFormatId() == 1) {
                    // commit 1 with 2pc
                    xar.commit(xid, false);
                } else if (xid.getFormatId() == 2) {
                    xar.rollback(xid);
                } else {
                    System.out.println("FAIL: unknown xact");
                }
            }

            // check the results
            Xid xid3 = XATestUtil.getXid(3, 2, 101);
            xar.start(xid3, XAResource.TMNOFLAGS);
            XATestUtil.showXATransactionView(conn);
            ResultSet rs = s.executeQuery("select * from APP.foo");
            JDBCDisplayUtil.DisplayResults(System.out, rs, conn);
            rs.close();
            xar.end(xid3, XAResource.TMSUCCESS);

            int pr = xar.prepare(xid3);
            if (pr != XAResource.XA_RDONLY)
                System.out.println("FAIL - prepare on read only xact returned "
                        + pr);

            try {
                xar.commit(xid3, true);
                System.out.println("FAIL - 2pc commit on read-only xact");
            } catch (XAException e) {
                if (e.errorCode != XAException.XAER_NOTA)
                    throw e;
            }

            s.close();
            conn.close();
            xac.close();
        } catch (SQLException sqle) {
            TestUtil.dumpSQLExceptions(sqle);
        } catch (XAException e) {
            XATestUtil.dumpXAException("interleavingTransactions", e);
        }
View Full Code Here

     </code>
     */
    private static void noTransaction(XADataSource xads) {
        System.out.println("noTransaction");
        try {
            XAConnection xac = xads.getXAConnection();
            XAResource xar = xac.getXAResource();

            Xid xid11 = XATestUtil.getXid(11, 3, 128);

            try {
                xar.start(xid11, XAResource.TMJOIN);
View Full Code Here

     */
    private static void morph(XADataSource xads) {
        System.out.println("morph");

        try {
            XAConnection xac = xads.getXAConnection();

            XAResource xar = xac.getXAResource();

            Connection conn = xac.getConnection();

            /*
             autocommit off;
             insert into foo values (1);
             select * from global_xactTable where gxid is not null order by gxid,username;
             commit;
             */
            conn.setAutoCommit(false);
            Statement s = conn.createStatement();
            s.executeUpdate("insert into APP.foo values (2001)");
            XATestUtil.showXATransactionView(conn);
            conn.commit();

            /*
             autocommit on;
             insert into foo values (2);
             select * from global_xactTable where gxid is not null order by gxid,username;
            
             */

            conn.setAutoCommit(true);
            s.executeUpdate("insert into APP.foo values (2002)");
            XATestUtil.showXATransactionView(conn);

            /*
             -- morph the connection to a global transaction
             xa_start xa_noflags 1;
             select * from global_xactTable where gxid is not null order by gxid,username;
             insert into foo values (3);
             */

            Xid xid = XATestUtil.getXid(1001, 66, 13);
            xar.start(xid, XAResource.TMNOFLAGS);
            XATestUtil.showXATransactionView(conn);
            s.executeUpdate("insert into APP.foo values (2003)");

            /*
             -- disallowed
             commit;
             -- disallowed
             rollback;
             -- disallowed
             autocommit on;
             -- OK
             autocommit off;
             */
            try {
                conn.commit();
                System.out.println("FAIL: commit allowed in global xact");
            } catch (SQLException e) {
            }

            try {
                conn.rollback();
                System.out.println("FAIL: roll back allowed in global xact");
            } catch (SQLException e) {
            }
            try {
                conn.setAutoCommit(true);
                System.out
                        .println("FAIL: setAutoCommit(true) allowed "+
         "in global xact");
            } catch (SQLException e) {
            }
            try {
                conn.setSavepoint();
                System.out
                    .println("FAIL: setSavepoint() allowed in global xact");
            } catch (SQLException e) {}
            try {
                conn.setSavepoint("badsavepoint");
                System.out
                    .println("FAIL: setAutoCommit(String) allowed in "+
                             "global xact");
            } catch (SQLException e) {}

            conn.setAutoCommit(false);

            // s was created in local mode so it has holdibilty
            // set, will execute but ResultSet will have close on commit

            if (TestUtil.isDerbyNetClientFramework()) { // DERBY-1158
            s.executeQuery("select * from APP.foo where A >= 2000").close();
            System.out.println("OK: query with holdable statement");
            }
            s.close();
           
           
            s = conn.createStatement();
            boolean holdable = s.getResultSetHoldability() == ResultSet.HOLD_CURSORS_OVER_COMMIT;
            System.out.println("Statement created in global has holdabilty: "
                    + holdable);

            /*
             select * from foo;
             xa_end xa_success 1;
             xa_prepare 1;
             */
            ResultSet rs = s
                    .executeQuery("select * from APP.foo where A >= 2000");
            JDBCDisplayUtil.DisplayResults(System.out, rs, conn);
            rs.close();

            xar.end(xid, XAResource.TMSUCCESS);
            xar.prepare(xid);

            /*
             -- dup id
             xa_start xa_noflags 1;
             */
            try {
                xar.start(xid, XAResource.TMNOFLAGS);
                System.out.println("FAIL - start with duplicate XID");
            } catch (XAException e) {
                if (e.errorCode != XAException.XAER_DUPID)
                    throw e;
            }

            /*
             xa_start xa_noflags 2;
             -- still should disallow autommit;
             autocommit on;
             -- still should disallow commit and rollback
             commit;
             rollback;
             select * from global_xactTable where gxid is not null order by gxid,username;
             xa_end xa_suspend 2;
             */

            Xid xid2 = XATestUtil.getXid(1002, 23, 3);
            xar.start(xid2, XAResource.TMNOFLAGS);
            try {
                conn.commit();
                System.out.println("FAIL: commit allowed in global xact");
            } catch (SQLException e) {
            }
            try {
                conn.rollback();
                System.out.println("FAIL: roll back allowed in global xact");
            } catch (SQLException e) {
            }
            try {
                conn.setAutoCommit(true);
                System.out
                        .println("FAIL: setAutoCommit(true) allowed in global xact");
            } catch (SQLException e) {
            }
            conn.setAutoCommit(false);

            xar.end(xid2, XAResource.TMSUSPEND);

            /*
             -- get local connection again
             xa_getconnection;

             insert into foo values (5);

             -- autocommit should be on by default;
             commit;

             autocommit off;
             insert into foo values (6);

             -- commit and rollback is allowed on local connection
             rollback;

             insert into foo values (6);
             commit;
             */
            conn = xac.getConnection();
            s = conn.createStatement();
            s.executeUpdate("insert into APP.foo values (2005)");
            conn.commit();
            conn.setAutoCommit(false);
            s.executeUpdate("insert into APP.foo values (2006)");
            conn.rollback();
            s.executeUpdate("insert into APP.foo values (2007)");
            conn.commit();

            XATestUtil.showXATransactionView(conn);
            /*
             -- I am still able to commit other global transactions while I am attached to a
             -- local transaction.
             xa_commit xa_2phase 1;
             xa_end xa_success 2;
             xa_rollback 2;
             */
            xar.commit(xid, false);
            xar.end(xid2, XAResource.TMSUCCESS);
            xar.rollback(xid2);

            XATestUtil.showXATransactionView(conn);
            rs = s.executeQuery("select * from APP.foo where A >= 2000");
            JDBCDisplayUtil.DisplayResults(System.out, rs, conn);
            rs.close();

            conn.close();

            /*
             xa_getconnection;
             select * from global_xactTable where gxid is not null order by gxid,username;
             select * from foo;
             autocommit off;
             delete from foo;
             */
            conn = xac.getConnection();
            conn.setAutoCommit(false);
            s = conn.createStatement();
            s.executeUpdate("delete from app.foo");
            rs = s.executeQuery("select * from APP.foo");
            JDBCDisplayUtil.DisplayResults(System.out, rs, conn);
            rs.close();

            // DERBY-1004
            if (TestUtil.isDerbyNetClientFramework()) {
                System.out.println("DERBY-1004 Call conn.rollback to avoid exception with client");
                conn.rollback();
            }
            /*
             -- yanking a local connection away should rollback the changes
             */
            conn = xac.getConnection();
            conn.setAutoCommit(false);
            s = conn.createStatement();
            rs = s.executeQuery("select * from APP.foo where A >= 2000");
            JDBCDisplayUtil.DisplayResults(System.out, rs, conn);
            rs.close();

            /*
             -- cannot morph it if the local transaction is not idle
             xa_start xa_noflags 3;
             commit;
             -- now morph it to a global transaction
             xa_start xa_noflags 3;
             */
            Xid xid3 = XATestUtil.getXid(1003, 27, 9);
            try {
                xar.start(xid3, XAResource.TMNOFLAGS);
                System.out.println("FAIL XAResource.start on a global transaction with an active local transaction (autocommit false)");
            } catch (XAException xae) {
                if (xae.errorCode != XAException.XAER_OUTSIDE)
                    throw xae;
                System.out.println("Correct XAException on starting a global transaction with an active local transaction (autocommit false)");
            }
            conn.commit();
            xar.start(xid3, XAResource.TMNOFLAGS);

            /*
             -- now I shouldn't be able to yank it
             xa_getconnection;
             */
            if (TestUtil.isDerbyNetClientFramework()) {
                System.out.println("DERBY-341 - Client skipping XAConnection with active local transaction");             
            } else {
            try {
                xac.getConnection();
                System.out
                        .println("FAIL: getConnection with active global xact");
            } catch (SQLException sqle) {
                TestUtil.dumpSQLExceptions(sqle, true);
            }
            }
            /*
             select * from foo;
             delete from foo;

             xa_end xa_fail 3;
             xa_rollback 3;

             -- local connection again
             xa_getconnection;
             select * from global_xactTable where gxid is not null order by gxid,username;
             select * from foo;
             */
            s = conn.createStatement();
            s.executeUpdate("delete from APP.foo");
            rs = s.executeQuery("select * from APP.foo where A >= 2000");
            JDBCDisplayUtil.DisplayResults(System.out, rs, conn);
            rs.close();
            try {
                xar.end(xid3, XAResource.TMFAIL);
            } catch (XAException e) {
                if (e.errorCode != XAException.XA_RBROLLBACK)
                    throw e;
            }
            xar.rollback(xid3);

            conn = xac.getConnection();
            s = conn.createStatement();
            rs = s.executeQuery("select * from APP.foo where A >= 2000");
            JDBCDisplayUtil.DisplayResults(System.out, rs, conn);
            rs.close();

View Full Code Here

    private static void derby966(XADataSource xads)
    {
        System.out.println("derby966");
       
        try {
            XAConnection xac = xads.getXAConnection();
            XAResource xar = xac.getXAResource();

            Xid xid = XATestUtil.getXid(996, 9, 48);
           
            Connection conn = xac.getConnection();
           
            // Obtain Statements and PreparedStatements
            // with all the holdability options.
           
            showHoldStatus("Local ", conn);
View Full Code Here

     */
    private static void interleavingTransactions5(XADataSource xads) throws SQLException
    {
        System.out.println("interleavingTransactions5");
       
        XAConnection xac = xads.getXAConnection("mamta", "mamtapwd");
       
    }
View Full Code Here

    {
        XADataSource ds = TestDataSourceFactory.getXADataSource();
        setDataSourceProperty(ds, "password", "mypassword");
        setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE,
                              Boolean.TYPE);
        XAConnection xa = ds.getXAConnection();
        Connection c = xa.getConnection();
        c.close();
    }
View Full Code Here

    {
        XADataSource ds = TestDataSourceFactory.getXADataSource();
        setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE,
                              Boolean.TYPE);
        try {
            XAConnection xa = ds.getXAConnection("username", "mypassword");
            fail("Expected getXAConnection to fail.");
        } catch (SQLException e) {
            // expect error because of malformed url
            assertSQLState("XJ028", e);
        }
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.