Java Code Examples for javax.sql.XAConnection#getConnection()

The following examples show how to use javax.sql.XAConnection#getConnection() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: J2EEDataSourceTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Test that connections retrieved from {@code XADataSource} that are part
 * of a global XA transaction, behave as expected when {@code close()} is
 * called and the transaction is active.
 */
public void testCloseActiveConnection_XA_global()
    throws SQLException, XAException
{
    XADataSource ds = J2EEDataSource.getXADataSource();
    XAConnection xa = ds.getXAConnection();
    XAResource xar = xa.getXAResource();
    Xid xid = new cdsXid(1, (byte) 2, (byte) 3);
    xar.start(xid, XAResource.TMNOFLAGS);
    // auto-commit is always off in XA transactions, so we expect
    // getAutoCommit() to return false without having set it explicitly
    testCloseActiveConnection(xa.getConnection(), false, true);
    Connection c = xa.getConnection();
    c.setAutoCommit(false);
    testCloseActiveConnection(c, false, true);
    xar.end(xid, XAResource.TMSUCCESS);
}
 
Example 2
Source File: J2EEDataSourceTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
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();
    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());
}
 
Example 3
Source File: XATransactionTest.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * This fixture triggers DERBY-1016. It creates an XA transaction, executes
 * an update over it and then prepares the transaction. Trying to forget
 * after preparing should throw XAER_PROTO and not XAER_NOTA.
 */
public void testForgetExceptionDerby1016PROTO() throws XAException, SQLException {      
    XADataSource xads = J2EEDataSource.getXADataSource();
    J2EEDataSource.setBeanProperty(xads, "databaseName", "wombat");
    
    XAConnection xaconn = xads.getXAConnection();
    XAResource xar = xaconn.getXAResource();
    Xid xid = createXid(93,18);
    xar.start(xid, XAResource.TMNOFLAGS);
    Connection conn = xaconn.getConnection();
    Statement s = conn.createStatement();
    s.executeUpdate("CREATE TABLE Derby1016 (I INT)");
    xar.end(xid, XAResource.TMSUCCESS);
    xar.prepare(xid);
    try {
        xar.forget(xid);
        fail("FAIL: prepared XA-Transaction forgotten");
    } catch (XAException XAeForget) {
        assertEquals("FAIL: Got unexpected exception "
                      + XAeForget.getMessage()   + " errorCode: " 
                      + XAeForget.errorCode  + "  calling forget on a prepared transaction",
                    XAException.XAER_PROTO, XAeForget.errorCode);
    } finally {
        s.close();
        xar.rollback(xid);
        conn.close(); 
        xaconn.close();
    }
}
 
Example 4
Source File: XATransactionTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public void testSimpleXATransaction() throws Exception {
  Statement stm = getConnection().createStatement();
  stm.execute("create table XATT2 (i int, text char(10))");

  XADataSource xaDataSource = (XADataSource)TestUtil.getXADataSource(TestUtil.EmbeddedeXADsClassName);
  // 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 = new ClientXid(0x1234, gid, bid);

  // get the stuff required to execute the global transaction
  XAConnection xaConn = xaDataSource.getXAConnection();
  XAResource xaRes = xaConn.getXAResource();
  Connection conn = xaConn.getConnection();
  conn.setTransactionIsolation(getIsolationLevel());
  // 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();

  stm = getConnection().createStatement();
  stm.execute("select * from XATT2");
  ResultSet rs = stm.getResultSet();
  assertFalse(rs.next());
  // end the work on the transaction branch
  xaRes.end(xid, XAResource.TMSUCCESS);
  xaRes.prepare(xid);
  xaRes.commit(xid, false);
  stm.execute("select * from XATT2");
  rs = stm.getResultSet();
  assertTrue(rs.next());
}
 
Example 5
Source File: TransactionUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/** Enlists the given XAConnection and if a transaction is active in the current thread, returns a plain JDBC Connection */
public static Connection enlistConnection(XAConnection xacon) throws GenericTransactionException {
    if (xacon == null) {
        return null;
    }
    try {
        XAResource resource = xacon.getXAResource();
        TransactionUtil.enlistResource(resource);
        return xacon.getConnection();
    } catch (SQLException e) {
        throw new GenericTransactionException("SQL error, could not enlist connection in transaction even though transactions are available", e);
    }
}
 
Example 6
Source File: J2EEDataSourceTest.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Test that connections retrieved from {@code XADataSource} that are not
 * part of a global XA transaction, behave as expected when {@code close()}
 * is called and the transaction is active.
 */
public void testCloseActiveConnection_XA_local() throws SQLException {
    XADataSource ds = J2EEDataSource.getXADataSource();
    XAConnection xa = ds.getXAConnection();
    testCloseActiveConnection(xa.getConnection(), true, false);
    Connection c = xa.getConnection();
    c.setAutoCommit(false);
    testCloseActiveConnection(c, false, false);
    xa.close();
}
 
Example 7
Source File: DataSourcePropertiesTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that the default password is not sent as an attribute string when
 * <code>attributesAsPassword</code> is <code>true</code>. The test is run
 * with an <code>XADataSource</code>.
 */
public void embeddedTestAttributesAsPasswordWithoutPassword_xa()
    throws Exception
{
    XADataSource ds = J2EEDataSource.getXADataSource();
    JDBCDataSource.setBeanProperty(ds, "password",  "mypassword");
    JDBCDataSource.setBeanProperty(ds, "attributesAsPassword", Boolean.TRUE);
    XAConnection xa = ds.getXAConnection();
    Connection c = xa.getConnection();
    c.close();
}
 
Example 8
Source File: DataSourcePropertiesTest.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Tests that the default password is not sent as an attribute string when
 * <code>attributesAsPassword</code> is <code>true</code>. The test is run
 * with an <code>XADataSource</code>.
 */
public void embeddedTestAttributesAsPasswordWithoutPassword_xa()
    throws Exception
{
    XADataSource ds = J2EEDataSource.getXADataSource();
    JDBCDataSource.setBeanProperty(ds, "password",  "mypassword");
    JDBCDataSource.setBeanProperty(ds, "attributesAsPassword", Boolean.TRUE);
    XAConnection xa = ds.getXAConnection();
    Connection c = xa.getConnection();
    c.close();
}
 
Example 9
Source File: Main.java    From genericconnector with Apache License 2.0 5 votes vote down vote up
private static void runSql(XAConnection xamysql, String username) throws SQLException {
    try(Connection conn = xamysql.getConnection()){
    	try(PreparedStatement stmt = conn.prepareStatement("insert into person(id, name) select max(id)+1, ? from person")){
    		stmt.setString(1, username);
    		stmt.executeUpdate();
    	}
    }
}
 
Example 10
Source File: XATransactionTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public void testFromNetClient() throws Exception {    
  setupConnection();
  int netport = startNetserverAndReturnPort("create table XATT2 "
      + "(i int, text char(10))");
  ClientXADataSource xaDataSource = (ClientXADataSource)TestUtil
      .getXADataSource(TestUtil.NetClientXADsClassName);
  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 = new ClientXid(0x1234, gid, bid);

  String localhost = SocketCreator.getLocalHost().getHostName(); 
  xaDataSource.setServerName(localhost);
  xaDataSource.setPortNumber(netport);

  xaDataSource.setDatabaseName("gemfirexd");
  // get the stuff required to execute the global transaction
  XAConnection xaConn = xaDataSource.getXAConnection();
  XAResource xaRes = xaConn.getXAResource();
  Connection conn = xaConn.getConnection();

  // start the transaction with that xid
  xaRes.start(xid, XAResource.TMNOFLAGS);

  conn.setTransactionIsolation(getIsolationLevel());
  // do some work
  Statement stm = conn.createStatement();
  stm.execute("insert into XATT2 values (1234, 'Test_Entry')");
  stm.close();

  stm = getConnection().createStatement();
  stm.execute("select * from XATT2");
  ResultSet rs = stm.getResultSet();
  assertFalse(rs.next());
  // end the work on the transaction branch
  xaRes.end(xid, XAResource.TMSUCCESS);
  xaRes.prepare(xid);
  xaRes.commit(xid, false);
  stm.execute("select * from XATT2");
  rs = stm.getResultSet();
  assertTrue(rs.next());
}
 
Example 11
Source File: J2EEDataSourceTest.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
public void testReadOnlyToWritableTran() throws SQLException, Exception
{
    // This fixture will run twice, once with embedded, once with client,
    // and insert 2 rows in addition to the 5 rows inserted during setup. 
    // The fixture tests a commit, so before running, try to remove row 
    // 6 and 7 in case this is the second run of the fixture.
    Statement s = createStatement();
    s.executeUpdate("delete from autocommitxastart where i = 6");
    s.executeUpdate("delete from autocommitxastart where i = 7");

    // TESTING READ_ONLY TRANSACTION FOLLOWED BY WRITABLE TRANSACTION
    // Test following sequence of steps
    // 1)start a read-only global transaction 
    // 2)finish that read-only transaction
    // 3)start another global transaction 

    XADataSource dsx = J2EEDataSource.getXADataSource();
    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();
}
 
Example 12
Source File: TransactionDUnit.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public void testXATransactionFromClient_rollback() throws Exception {
  startServerVMs(2, 0, null);
  startClientVMs(1, 0, null);
  final int netport = startNetworkServer(1, null, null);
  serverSQLExecute(1, "create schema test");
  serverSQLExecute(1, "create table test.XATT2 (intcol int not null, text varchar(100) not null)"+ getSuffix());
  serverSQLExecute(1, "insert into test.XATT2 values (1, 'ONE')");
  
  ClientXADataSource xaDataSource = (ClientXADataSource)TestUtil
      .getXADataSource(TestUtil.NetClientXADsClassName);
  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 = new ClientXid(0x1234, gid, bid);

  String localhost = SocketCreator.getLocalHost().getHostName();
  xaDataSource.setServerName(localhost);
  xaDataSource.setPortNumber(netport);

  xaDataSource.setDatabaseName("gemfirexd");
  // get the stuff required to execute the global transaction
  XAConnection xaConn = xaDataSource.getXAConnection();
  XAResource xaRes = xaConn.getXAResource();
  Connection conn = xaConn.getConnection();

  // start the transaction with that xid
  xaRes.start(xid, XAResource.TMNOFLAGS);

  conn.setTransactionIsolation(getIsolationLevel());
  // do some work
  Statement stm = conn.createStatement();
  stm.execute("insert into test.XATT2 values (2, 'TWO')");
  
  String jdbcSQL = "select * from test.XATT2";
  String xmlFile = TestUtil.getResourcesDir() + "/lib/checkQuery.xml";
  
  sqlExecuteVerify(null, new int[] {1}, jdbcSQL, xmlFile, "before_xa_commit");
  
  stm.execute("select * from test.XATT2");
  ResultSet r = stm.getResultSet();
  int cnt = 0;
  while(r.next()) {
    cnt++;
  }
  assertEquals(2, cnt);
  xaRes.end(xid, XAResource.TMSUCCESS);
  xaRes.prepare(xid);
  xaRes.rollback(xid);
  
  VM servervm = getServerVM(1);
  servervm.invoke(TransactionDUnit.class, "waitForPendingCommit");
  sqlExecuteVerify(null, new int[] {1}, jdbcSQL, xmlFile, "before_xa_commit");
}
 
Example 13
Source File: XaTest.java    From jTDS with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Demonstrate interleaving local transactions and distributed
 * transactions.
 *
 * @throws Exception if an error condition occurs
 */
public void testLocalTran() throws Exception {
    if ("true".equalsIgnoreCase(props.getProperty(Messages.get(Driver.XAEMULATION)))) {
        // Emulation mode does not support suspending transactions.
        return;
    }
    Connection con2 = null;
    XAConnection xaCon = null;

    try {
        dropTable("jTDS_XATEST");
        dropTable("jTDS_XATEST2");

        Statement stmt = con.createStatement();
        stmt.execute("CREATE TABLE jTDS_XATEST (id int primary key, data varchar(255))");
        stmt.execute("CREATE TABLE jTDS_XATEST2 (id int primary key, data varchar(255))");
        assertNotNull(stmt.executeQuery("SELECT * FROM jTDS_XATEST"));
        assertNotNull(stmt.executeQuery("SELECT * FROM jTDS_XATEST2"));
        stmt.close();

        XADataSource xaDS = getDataSource();
        XAResource xaRes;
        Xid  xid;
        xaCon = xaDS.getXAConnection();
        xaRes = xaCon.getXAResource();
        con2 = xaCon.getConnection();
        stmt = con2.createStatement();
        xid = new JtdsXid(new byte[]{0x01}, new byte[]{0x02});

        xaRes.start(xid, XAResource.TMNOFLAGS);
        stmt.executeUpdate("INSERT INTO jTDS_XATEST VALUES (1, 'TEST LINE')");
        xaRes.end(xid, XAResource.TMSUSPEND);
        stmt.executeUpdate("INSERT INTO jTDS_XATEST2 VALUES (1, 'TEST LINE')");
        xaRes.start(xid, XAResource.TMRESUME);
        stmt.executeUpdate("INSERT INTO jTDS_XATEST VALUES (2, 'TEST LINE 2')");
        xaRes.end(xid, XAResource.TMSUCCESS);
        xaRes.rollback(xid);
        stmt.close();
        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM jTDS_XATEST");
        assertNotNull(rs);
        assertFalse(rs.next());
        rs = stmt.executeQuery("SELECT * FROM jTDS_XATEST2");
        assertNotNull(rs);
        assertTrue(rs.next());
        stmt.close();
    } finally {
        if (con2 != null) {
            con2.close();
        }
        if (xaCon != null) {
            xaCon.close();
        }

        dropTable("jTDS_XATEST");
        dropTable("jTDS_XATEST2");
    }
}
 
Example 14
Source File: TransactionDUnit.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public void testXATransactionFromClient_rollback() throws Exception {
  startServerVMs(2, 0, null);
  startClientVMs(1, 0, null);
  final int netport = startNetworkServer(1, null, null);
  serverSQLExecute(1, "create schema test");
  serverSQLExecute(1, "create table test.XATT2 (intcol int not null, text varchar(100) not null)"+ getSuffix());
  serverSQLExecute(1, "insert into test.XATT2 values (1, 'ONE')");
  
  ClientXADataSource xaDataSource = (ClientXADataSource)TestUtil
      .getXADataSource(TestUtil.NetClientXADsClassName);
  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 = new ClientXid(0x1234, gid, bid);

  String localhost = SocketCreator.getLocalHost().getHostName();
  xaDataSource.setServerName(localhost);
  xaDataSource.setPortNumber(netport);

  xaDataSource.setDatabaseName("gemfirexd");
  // get the stuff required to execute the global transaction
  XAConnection xaConn = xaDataSource.getXAConnection();
  XAResource xaRes = xaConn.getXAResource();
  Connection conn = xaConn.getConnection();

  // start the transaction with that xid
  xaRes.start(xid, XAResource.TMNOFLAGS);

  conn.setTransactionIsolation(getIsolationLevel());
  // do some work
  Statement stm = conn.createStatement();
  stm.execute("insert into test.XATT2 values (2, 'TWO')");
  
  String jdbcSQL = "select * from test.XATT2";
  String xmlFile = TestUtil.getResourcesDir() + "/lib/checkQuery.xml";
  
  sqlExecuteVerify(null, new int[] {1}, jdbcSQL, xmlFile, "before_xa_commit");
  
  stm.execute("select * from test.XATT2");
  ResultSet r = stm.getResultSet();
  int cnt = 0;
  while(r.next()) {
    cnt++;
  }
  assertEquals(2, cnt);
  xaRes.end(xid, XAResource.TMSUCCESS);
  xaRes.prepare(xid);
  xaRes.rollback(xid);
  
  VM servervm = getServerVM(1);
  servervm.invoke(TransactionDUnit.class, "waitForPendingCommit");
  sqlExecuteVerify(null, new int[] {1}, jdbcSQL, xmlFile, "before_xa_commit");
}
 
Example 15
Source File: TransactionDUnit.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public void testXATransactionFromPeerClient_commit() throws Exception {
  startServerVMs(2, 0, null);
  startClientVMs(1, 0, null);

  serverSQLExecute(1, "create schema test");
  serverSQLExecute(1, "create table test.XATT2 (intcol int not null, text varchar(100) not null)"+ getSuffix());
  serverSQLExecute(1, "insert into test.XATT2 values (1, 'ONE')");
  
  EmbeddedXADataSource xaDataSource = (EmbeddedXADataSource)TestUtil
      .getXADataSource(TestUtil.EmbeddedeXADsClassName);
  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 = new ClientXid(0x1234, gid, bid);

  // get the stuff required to execute the global transaction
  XAConnection xaConn = xaDataSource.getXAConnection();
  XAResource xaRes = xaConn.getXAResource();
  Connection conn = xaConn.getConnection();

  // start the transaction with that xid
  xaRes.start(xid, XAResource.TMNOFLAGS);

  conn.setTransactionIsolation(getIsolationLevel());
  // do some work
  Statement stm = conn.createStatement();
  stm.execute("insert into test.XATT2 values (2, 'TWO')");
  
  String jdbcSQL = "select * from test.XATT2";
  String xmlFile = TestUtil.getResourcesDir() + "/lib/checkQuery.xml";
  
  sqlExecuteVerify(null, new int[] {1}, jdbcSQL, xmlFile, "before_xa_commit");
  stm.execute("select * from test.XATT2");
  ResultSet r = stm.getResultSet();
  int cnt = 0;
  while(r.next()) {
    cnt++;
  }
  assertEquals(2, cnt);
  xaRes.end(xid, XAResource.TMSUCCESS);
  xaRes.prepare(xid);
  xaRes.commit(xid, false);
  VM servervm = getServerVM(1);
  servervm.invoke(TransactionDUnit.class, "waitForPendingCommit");
  TXManagerImpl.waitForPendingCommitForTest();

  sqlExecuteVerify(null, new int[] { 1 }, jdbcSQL, xmlFile, "after_xa_commit");
}
 
Example 16
Source File: JtaXAConnectionWrapper.java    From kumuluzee with MIT License 4 votes vote down vote up
public JtaXAConnectionWrapper(XAConnection xaConnection) throws SQLException {
    this.xaResource = xaConnection.getXAResource();
    this.parentConnection = xaConnection;
    this.xaConnection = xaConnection.getConnection();
}
 
Example 17
Source File: XATransactionTest.java    From gemfirexd-oss with Apache License 2.0 2 votes vote down vote up
public void testSingleConnectionOnePhaseCommit() throws SQLException,
    XAException {

  Statement stm = getConnection().createStatement();
  stm.execute("create table XATT2 (i int, text char(10))");

  XADataSource xads = (XADataSource)TestUtil.getXADataSource(TestUtil.EmbeddedeXADsClassName);

  XAConnection xac = xads.getXAConnection();

  XAResource xar = xac.getXAResource();

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

  xar.start(xid, XAResource.TMNOFLAGS);

  Connection conn = xac.getConnection();
  conn.setTransactionIsolation(getIsolationLevel());
  assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, conn.getHoldability());

  Statement s = conn.createStatement();
  assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, s.getResultSetHoldability());

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

  ResultSet rs = s.executeQuery("select * from foo");
  assertTrue(rs.next());
  assertFalse(rs.next());

  String[][] expectedRows = { { "(0", "ACTIVE", "false", "APP",
      "UserTransaction" } };

  // TODO: what to check
  // XATestUtil.checkXATransactionView(conn, expectedRows);

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

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

  conn.close();
  xac.close();
}
 
Example 18
Source File: ClosedObjectTest.java    From gemfirexd-oss with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new connection using an <code>XADataSource</code>.
 *
 * @return a <code>Connection</code> value
 * @exception SQLException if an error occurs
 */
protected Connection newConnection_() throws SQLException {
    XADataSource ds = J2EEDataSource.getXADataSource();
    XAConnection xac = ds.getXAConnection();
    return xac.getConnection();
}
 
Example 19
Source File: XATest.java    From spliceengine with GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * A single connection and 1 phase commit.
 * 
 * 
 * Original "SQL" from xaSimplePositive.sql <code>
 xa_connect ;
 xa_start xa_noflags 0;
 xa_getconnection;
 drop table foo;
 create table foo (a int);
 insert into foo values (0);
 select * from foo;
 run resource '/org/apache/derbyTesting/functionTests/tests/store/global_xactTable.view';
 select * from global_xactTable where gxid is not null order by gxid;
 xa_end xa_success 0;
 xa_commit xa_1phase 0;
 
 xa_datasource 'wombat' shutdown;
 </code>
 * 
 * @throws SQLException
 * @throws XAException
 * @throws XAException
 */
public void testSingleConnectionOnePhaseCommit() throws SQLException,
        XAException {

    XADataSource xads = J2EEDataSource.getXADataSource();
    J2EEDataSource.setBeanProperty(xads, "databaseName", "wombat");

    XAConnection xac = xads.getXAConnection();

    XAResource xar = xac.getXAResource();

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

    xar.start(xid, XAResource.TMNOFLAGS);

    Connection conn = xac.getConnection();
    assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, conn.getHoldability());

    Statement s = conn.createStatement();
    assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, s
            .getResultSetHoldability());

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

    ResultSet rs = s.executeQuery("select * from foo");
    JDBC.assertDrainResults(rs, 1);

    String[][] expectedRows = { { "(0", "ACTIVE", "false", "SPLICE",
            "UserTransaction" } };

    XATestUtil.checkXATransactionView(conn, expectedRows);

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

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

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

}
 
Example 20
Source File: ClosedObjectTest.java    From gemfirexd-oss with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new connection using an <code>XADataSource</code>.
 *
 * @return a <code>Connection</code> value
 * @exception SQLException if an error occurs
 */
protected Connection newConnection_() throws SQLException {
    XADataSource ds = J2EEDataSource.getXADataSource();
    XAConnection xac = ds.getXAConnection();
    return xac.getConnection();
}