Package javax.sql

Examples of javax.sql.XAConnection


      throw new TransactionRuntimeException(
        "Program Error: DataSource is not mapped to Identifier "
          + identifier);
  }

  XAConnection conn = ds.getXAConnection();

  if (conn != null)
      return conn;

  ConnectionStrategy cs = null;
View Full Code Here


        Connection dm = ds.getConnection();
        Statement stat = dm.createStatement();
        stat.execute("CREATE TABLE IF NOT EXISTS TEST(ID INT PRIMARY KEY, VAL INT)");
        stat.execute("INSERT INTO TEST(ID,VAL) VALUES (1,1)");
        dm.close();
        XAConnection c = ds.getXAConnection();
        XAResource xa = c.getXAResource();
        Connection connection = c.getConnection();
        xa.start(xid, XAResource.TMJOIN);
        PreparedStatement ps = connection.prepareStatement("UPDATE TEST SET VAL=? WHERE ID=?");
        ps.setInt(1, new Random().nextInt());
        ps.setInt(2, 1);
        ps.close();
        xa.rollback(xid);
        connection.close();
        c.close();
        deleteDb("xa");
    }
View Full Code Here

    private void testMixedXaNormal() throws Exception {
        JdbcDataSource ds = new JdbcDataSource();
        ds.setURL("jdbc:h2:mem:test");
        ds.setUser("sa");
        ds.setPassword("");
        XAConnection xa = ds.getXAConnection();
        Connection c = xa.getConnection();
        assertTrue(c.getAutoCommit());
        MyXid xid = new MyXid();
        XAResource res = xa.getXAResource();

        res.start(xid, XAResource.TMNOFLAGS);
        assertTrue(!c.getAutoCommit());
        res.end(xid, XAResource.TMSUCCESS);
        res.commit(xid, true);
        assertTrue(c.getAutoCommit());

        res.start(xid, XAResource.TMNOFLAGS);
        assertTrue(!c.getAutoCommit());
        res.end(xid, XAResource.TMFAIL);
        res.rollback(xid);
        assertTrue(c.getAutoCommit());

        c.close();
        xa.close();
    }
View Full Code Here

    private void testXAAutoCommit() throws Exception {
        JdbcDataSource ds = new JdbcDataSource();
        ds.setURL("jdbc:h2:mem:test");
        ds.setUser("sa");
        ds.setPassword("");
        XAConnection xa = ds.getXAConnection();
        MyXid xid = new MyXid();
        xa.getXAResource().start(xid,
                XAResource.TMNOFLAGS);
        Connection c = xa.getConnection();
        assertTrue(!c.getAutoCommit());
        c.close();
        xa.close();
    }
View Full Code Here

    private void testXA(boolean useOneDatabase) throws SQLException {
        String url1 = getURL(DB_NAME1, true);
        String url2 = getURL(DB_NAME2, true);

        XAConnection xaConn1 = null;
        XAConnection xaConn2 = null;
        Connection conn1 = null;
        Connection conn2 = null;
        Statement stat1 = null;
        Statement stat2 = null;
        try {
            trace("xads1 = createXADatasource1()");
            XADataSource xaDs1 = createXADatasource(useOneDatabase, url1);
            trace("xads2 = createXADatasource2()");
            XADataSource xaDs2 = createXADatasource(useOneDatabase, url2);

            trace("xacon1 = xads1.getXAConnection()");
            xaConn1 = xaDs1.getXAConnection();
            trace("xacon2 = xads2.getXAConnection()");
            xaConn2 = xaDs2.getXAConnection();

            trace("xares1 = xacon1.getXAResource()");
            XAResource xares1 = xaConn1.getXAResource();
            trace("xares2 = xacon2.getXAResource()");
            XAResource xares2 = xaConn2.getXAResource();

            trace("xares1.recover(XAResource.TMSTARTRSCAN)");
            Xid[] xids1 = xares1.recover(XAResource.TMSTARTRSCAN);
            if ((xids1 == null) || (xids1.length == 0)) {
                trace("xares1.recover(XAResource.TMSTARTRSCAN): 0");
            } else {
                trace("xares1.recover(XAResource.TMSTARTRSCAN): " + xids1.length);
            }

            trace("xares2.recover(XAResource.TMSTARTRSCAN)");
            Xid[] xids2 = xares2.recover(XAResource.TMSTARTRSCAN);
            if ((xids2 == null) || (xids2.length == 0)) {
                trace("xares2.recover(XAResource.TMSTARTRSCAN): 0");
            } else {
                trace("xares2.recover(XAResource.TMSTARTRSCAN): " + xids2.length);
            }

            trace("con1 = xacon1.getConnection()");
            conn1 = xaConn1.getConnection();
            trace("stmt1 = con1.createStatement()");
            stat1 = conn1.createStatement();

            trace("con2 = xacon2.getConnection()");
            conn2 = xaConn2.getConnection();
            trace("stmt2 = con2.createStatement()");
            stat2 = conn2.createStatement();

            if (useOneDatabase) {
                trace("stmt1.executeUpdate(\"DROP TABLE xatest1\")");
                try {
                    stat1.executeUpdate("DROP TABLE xatest1");
                } catch (SQLException e) {
                    // ignore
                }
                trace("stmt2.executeUpdate(\"DROP TABLE xatest2\")");
                try {
                    stat2.executeUpdate("DROP TABLE xatest2");
                } catch (SQLException e) {
                    // ignore
                }
            } else {
                trace("stmt1.executeUpdate(\"DROP TABLE xatest\")");
                try {
                    stat1.executeUpdate("DROP TABLE xatest");
                } catch (SQLException e) {
                    // ignore
                }
                trace("stmt2.executeUpdate(\"DROP TABLE xatest\")");
                try {
                    stat2.executeUpdate("DROP TABLE xatest");
                } catch (SQLException e) {
                    // ignore
                }
            }

            if (useOneDatabase) {
                trace("stmt1.executeUpdate(\"CREATE TABLE xatest1 (id INT PRIMARY KEY, value INT)\")");
                stat1.executeUpdate("CREATE TABLE xatest1 (id INT PRIMARY KEY, value INT)");
                trace("stmt2.executeUpdate(\"CREATE TABLE xatest2 (id INT PRIMARY KEY, value INT)\")");
                stat2.executeUpdate("CREATE TABLE xatest2 (id INT PRIMARY KEY, value INT)");
            } else {
                trace("stmt1.executeUpdate(\"CREATE TABLE xatest (id INT PRIMARY KEY, value INT)\")");
                stat1.executeUpdate("CREATE TABLE xatest (id INT PRIMARY KEY, value INT)");
                trace("stmt2.executeUpdate(\"CREATE TABLE xatest (id INT PRIMARY KEY, value INT)\")");
                stat2.executeUpdate("CREATE TABLE xatest (id INT PRIMARY KEY, value INT)");
            }

            if (useOneDatabase) {
                trace("stmt1.executeUpdate(\"INSERT INTO xatest1 VALUES (1, 0)\")");
                stat1.executeUpdate("INSERT INTO xatest1 VALUES (1, 0)");
                trace("stmt2.executeUpdate(\"INSERT INTO xatest2 VALUES (2, 0)\")");
                stat2.executeUpdate("INSERT INTO xatest2 VALUES (2, 0)");
            } else {
                trace("stmt1.executeUpdate(\"INSERT INTO xatest VALUES (1, 0)\")");
                stat1.executeUpdate("INSERT INTO xatest VALUES (1, 0)");
                trace("stmt2.executeUpdate(\"INSERT INTO xatest VALUES (2, 0)\")");
                stat2.executeUpdate("INSERT INTO xatest VALUES (2, 0)");
            }

            Xid xid1 = null;
            Xid xid2 = null;

            if (useOneDatabase) {
                xid1 = SimpleXid.createRandom();
                xid2 = SimpleXid.createRandom();
            } else {
                xid1 = SimpleXid.createRandom();
                xid2 = xid1;
            }

            if (useOneDatabase) {
                trace("xares1.start(xid1, XAResource.TMNOFLAGS)");
                xares1.start(xid1, XAResource.TMNOFLAGS);
                trace("xares2.start(xid2, XAResource.TMJOIN)");
                xares2.start(xid2, XAResource.TMJOIN);
            } else {
                trace("xares1.start(xid1, XAResource.TMNOFLAGS)");
                xares1.start(xid1, XAResource.TMNOFLAGS);
                trace("xares2.start(xid2, XAResource.TMNOFLAGS)");
                xares2.start(xid2, XAResource.TMNOFLAGS);
            }

            if (useOneDatabase) {
                trace("stmt1.executeUpdate(\"UPDATE xatest1 SET value=1 WHERE id=1\")");
                stat1.executeUpdate("UPDATE xatest1 SET value=1 WHERE id=1");
                trace("stmt2.executeUpdate(\"UPDATE xatest2 SET value=1 WHERE id=2\")");
                stat2.executeUpdate("UPDATE xatest2 SET value=1 WHERE id=2");
            } else {
                trace("stmt1.executeUpdate(\"UPDATE xatest SET value=1 WHERE id=1\")");
                stat1.executeUpdate("UPDATE xatest SET value=1 WHERE id=1");
                trace("stmt2.executeUpdate(\"UPDATE xatest SET value=1 WHERE id=2\")");
                stat2.executeUpdate("UPDATE xatest SET value=1 WHERE id=2");
            }

            trace("xares1.end(xid1, XAResource.TMSUCCESS)");
            xares1.end(xid1, XAResource.TMSUCCESS);
            trace("xares2.end(xid2, XAResource.TMSUCCESS)");
            xares2.end(xid2, XAResource.TMSUCCESS);

            int ret1;
            int ret2;

            trace("ret1 = xares1.prepare(xid1)");
            ret1 = xares1.prepare(xid1);
            trace("xares1.prepare(xid1): " + ret1);
            trace("ret2 = xares2.prepare(xid2)");
            ret2 = xares2.prepare(xid2);
            trace("xares2.prepare(xid2): " + ret2);

            if ((ret1 != XAResource.XA_OK) && (ret1 != XAResource.XA_RDONLY)) {
                throw new IllegalStateException("xares1.prepare(xid1) must return XA_OK or XA_RDONLY");
            }
            if ((ret2 != XAResource.XA_OK) && (ret2 != XAResource.XA_RDONLY)) {
                throw new IllegalStateException("xares2.prepare(xid2) must return XA_OK or XA_RDONLY");
            }

            if (ret1 == XAResource.XA_OK) {
                trace("xares1.commit(xid1, false)");
                xares1.commit(xid1, false);
            }
            if (ret2 == XAResource.XA_OK) {
                trace("xares2.commit(xid2, false)");
                xares2.commit(xid2, false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.closeSilently(stat1);
            JdbcUtils.closeSilently(stat2);
            JdbcUtils.closeSilently(conn1);
            JdbcUtils.closeSilently(conn2);
            xaConn1.close();
            xaConn2.close();
        }
    }
View Full Code Here

        if (userInDataSource) {
            assertEquals("ds" + ds.getTraceId() + ": url=" + url + " user=" + user, ds.toString());
        } else {
            assertEquals("ds" + ds.getTraceId() + ": url=" + url + " user=", ds.toString());
        }
        XAConnection xaConn;
        if (userInDataSource) {
            xaConn = ds.getXAConnection();
        } else {
            xaConn = ds.getXAConnection(user, getPassword());
        }

        int traceId = ((JdbcXAConnection) xaConn).getTraceId();
        assertTrue(xaConn.toString().startsWith("xads" + traceId + ": conn"));

        xaConn.addConnectionEventListener(new ConnectionEventListener() {
            public void connectionClosed(ConnectionEvent event) {
                // nothing to do
            }

            public void connectionErrorOccurred(ConnectionEvent event) {
                // nothing to do
            }
        });
        XAResource res = xaConn.getXAResource();

        assertFalse(res.setTransactionTimeout(1));
        assertEquals(0, res.getTransactionTimeout());
        assertTrue(res.isSameRM(res));
        assertFalse(res.isSameRM(null));

        Connection conn = xaConn.getConnection();
        assertEquals(user.toUpperCase(), conn.getMetaData().getUserName());
        Xid[] list = res.recover(XAResource.TMSTARTRSCAN);
        assertEquals(0, list.length);
        Statement stat = conn.createStatement();
        stat.execute("SELECT * FROM DUAL");
        conn.close();
        xaConn.close();
    }
View Full Code Here

        ds.setPassword(getPassword());
        ds.setUser("sa");
        // ds.setURL(getURL("xaSimple", true) + ";trace_level_system_out=3");
        ds.setURL(getURL(db, true));

        XAConnection xa;
        xa = ds.getXAConnection();
        Connection conn;

        conn = xa.getConnection();
        Statement stat = conn.createStatement();
        stat.execute("create table test(id int primary key, name varchar(255))");
        Xid xid = SimpleXid.createRandom();
        xa.getXAResource().start(xid, XAResource.TMNOFLAGS);
        conn.setAutoCommit(false);
        stat.execute("insert into test values(1, 'Hello')");
        xa.getXAResource().end(xid, XAResource.TMSUCCESS);
        xa.getXAResource().prepare(xid);
        if (shutdown) {
            shutdown(ds);
        }

        xa = ds.getXAConnection();
        Xid[] list = xa.getXAResource().recover(XAResource.TMSTARTRSCAN);
        assertEquals(1, list.length);
        assertTrue(xid.equals(list[0]));
        if (commit) {
            xa.getXAResource().commit(list[0], false);
        } else {
            xa.getXAResource().rollback(list[0]);
        }
        if (shutdown) {
            shutdown(ds);
        }

        xa = ds.getXAConnection();
        list = xa.getXAResource().recover(XAResource.TMSTARTRSCAN);
        assertEquals(0, list.length);
        conn = xa.getConnection();
        ResultSet rs;
        rs = conn.createStatement().executeQuery("select * from test");
        if (commit) {
            assertTrue(rs.next());
        } else {
            assertFalse(rs.next());
        }
        xa.close();
    }
View Full Code Here

        // UserTransaction ut = (UserTransaction)
        // context.lookup("UserTransaction");
        // ut.begin();

        XAConnection xa1 = ds1.getXAConnection();
        Connection c1 = xa1.getConnection();
        c1.setAutoCommit(false);
        XAConnection xa2 = ds2.getXAConnection();
        Connection c2 = xa2.getConnection();
        c2.setAutoCommit(false);

        c1.createStatement().executeUpdate("create table test(id int, test varchar(255))");
        c2.createStatement().executeUpdate("create table test(id int, test varchar(255))");

        // ut.rollback();
        c1.close();
        c2.close();

        xa1.close();
        xa2.close();

        // j.stop();
        // System.exit(0);
        deleteDb("xaSimple1");
        deleteDb("xaSimple2");
View Full Code Here

   }
  
   public ManagedConnection getXAManagedConnection(Subject subject, ConnectionRequestInfo cri)
       throws javax.resource.ResourceException
  {
        XAConnection xaConnection = null;
    Properties props = getConnectionProperties(subject, cri);
    try
    {
       final String user = props.getProperty("user");
       final String password = props.getProperty("password");
   
       xaConnection = (user != null)
         ? getXADataSource().getXAConnection(user, password)
         : getXADataSource().getXAConnection();
   
       return newXAManagedConnection(props, xaConnection);
    }
    catch (Throwable e)
    {
       try
       {
          if (xaConnection != null)
             xaConnection.close();
       }
       catch (Throwable ignored)
       {
       }
       throw new JBossResourceException("Could not create connection", e);
View Full Code Here

        return transactionRegistry;
    }

    public Connection createConnection() throws SQLException {
        // create a new XAConection
        XAConnection xaConnection;
        if (username == null) {
            xaConnection = xaDataSource.getXAConnection();
        } else {
            xaConnection = xaDataSource.getXAConnection(username, password);
        }

        // get the real connection and XAResource from the connection
        Connection connection = xaConnection.getConnection();
        XAResource xaResource = xaConnection.getXAResource();

        // register the xa resource for the connection
        transactionRegistry.registerConnection(connection, xaResource);

        return connection;
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.