Search in sources :

Example 71 with XAConnection

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

the class TransactionLogTest method testRequired2PCNoRecovery.

@Test
public void testRequired2PCNoRecovery() throws Exception {
    XAConnection xaConn = dataSource.getXAConnection();
    XAConnection xaConn2 = dataSource.getXAConnection();
    try {
        txControl.required(() -> {
            txControl.getCurrentContext().registerXAResource(xaConn.getXAResource(), null);
            txControl.getCurrentContext().registerXAResource(xaConn2.getXAResource(), null);
            Connection conn = xaConn.getConnection();
            // conn.setAutoCommit(false);
            Connection conn2 = xaConn2.getConnection();
            conn2.setAutoCommit(false);
            conn.createStatement().execute("Insert into TEST_TABLE values ( 'Hello World!' )");
            return conn2.createStatement().execute("Insert into TEST_TABLE values ( 'Hello World 2!' )");
        });
    } finally {
        xaConn.close();
    }
    try (Connection conn = dataSource.getConnection()) {
        ResultSet rs = conn.createStatement().executeQuery("Select * from TEST_TABLE order by message DESC");
        rs.next();
        assertEquals("Hello World!", rs.getString(1));
        rs.next();
        assertEquals("Hello World 2!", rs.getString(1));
        assertFalse(rs.next());
    }
}
Also used : Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) ResultSet(java.sql.ResultSet) XAConnection(javax.sql.XAConnection) Test(org.junit.Test)

Example 72 with XAConnection

use of javax.sql.XAConnection in project warn-report by saaavsaaa.

the class DistributeTransaction method getDatabaseConnection.

Connection getDatabaseConnection(XAConnection xacon) {
    Connection con = null;
    try {
        System.out.print("Establish database connection: ");
        con = xacon.getConnection();
        con.setAutoCommit(false);
        System.out.println("Okay.");
    } catch (SQLException e) {
        sqlerr(e);
    }
    return con;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection)

Example 73 with XAConnection

use of javax.sql.XAConnection in project jaybird by FirebirdSQL.

the class TestFBXADataSource method testInDistributed_setAutoCommit_true_inAutoCommit.

/**
 * Tests if setting autoCommit(true) when autoCommit is true throws an exception when participating in a distributed transaction (JDBC 4.0 section 12.4).
 */
@Test
public void testInDistributed_setAutoCommit_true_inAutoCommit() throws Exception {
    XAConnection pc = getXAConnection();
    XAResource xa = pc.getXAResource();
    Xid xid = new XidImpl();
    try (Connection con = pc.getConnection()) {
        con.setAutoCommit(true);
        xa.start(xid, XAResource.TMNOFLAGS);
        expectedException.expect(SQLException.class);
        expectedException.expect(sqlStateEquals(SQLStateConstants.SQL_STATE_INVALID_TX_STATE));
        con.setAutoCommit(true);
    } finally {
        xa.end(xid, XAResource.TMSUCCESS);
        xa.rollback(xid);
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) XidImpl(org.firebirdsql.jca.TestXABase.XidImpl) XAConnection(javax.sql.XAConnection) XAConnection(javax.sql.XAConnection)

Example 74 with XAConnection

use of javax.sql.XAConnection in project jaybird by FirebirdSQL.

the class TestFBXADataSource method testInDistributed_setSavepoint.

/**
 * Test if calling setSavePoint (no param) throws an exception when participating in a distributed transaction (JDBC 4.0 section 12.4).
 */
@Test
public void testInDistributed_setSavepoint() throws Exception {
    XAConnection pc = getXAConnection();
    XAResource xa = pc.getXAResource();
    Xid xid = new XidImpl();
    try (Connection con = pc.getConnection()) {
        con.setAutoCommit(false);
        xa.start(xid, XAResource.TMNOFLAGS);
        expectedException.expect(SQLException.class);
        expectedException.expect(sqlStateEquals(SQLStateConstants.SQL_STATE_INVALID_TX_STATE));
        con.setSavepoint();
    } finally {
        xa.end(xid, XAResource.TMSUCCESS);
        xa.rollback(xid);
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) XidImpl(org.firebirdsql.jca.TestXABase.XidImpl) XAConnection(javax.sql.XAConnection) XAConnection(javax.sql.XAConnection)

Example 75 with XAConnection

use of javax.sql.XAConnection in project jaybird by FirebirdSQL.

the class TestFBXADataSource method testInDistributed_commit.

/**
 * Test if calling commit throws an exception when participating in a distributed transaction (JDBC 4.0 section 12.4).
 */
@Test
public void testInDistributed_commit() throws Exception {
    XAConnection pc = getXAConnection();
    XAResource xa = pc.getXAResource();
    Xid xid = new XidImpl();
    try (Connection con = pc.getConnection()) {
        con.setAutoCommit(false);
        xa.start(xid, XAResource.TMNOFLAGS);
        expectedException.expect(SQLException.class);
        expectedException.expect(sqlStateEquals(SQLStateConstants.SQL_STATE_INVALID_TX_STATE));
        con.commit();
    } finally {
        xa.end(xid, XAResource.TMSUCCESS);
        xa.rollback(xid);
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) XidImpl(org.firebirdsql.jca.TestXABase.XidImpl) XAConnection(javax.sql.XAConnection) XAConnection(javax.sql.XAConnection)

Aggregations

XAConnection (javax.sql.XAConnection)115 Connection (java.sql.Connection)78 XAResource (javax.transaction.xa.XAResource)56 XADataSource (javax.sql.XADataSource)52 Xid (javax.transaction.xa.Xid)44 Statement (java.sql.Statement)34 SQLException (java.sql.SQLException)30 PreparedStatement (java.sql.PreparedStatement)23 PooledConnection (javax.sql.PooledConnection)23 ResultSet (java.sql.ResultSet)22 CallableStatement (java.sql.CallableStatement)18 Test (org.junit.Test)18 XAException (javax.transaction.xa.XAException)16 Transaction (javax.transaction.Transaction)8 DataSource (javax.sql.DataSource)7 XidImpl (org.firebirdsql.jca.TestXABase.XidImpl)7 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)7 NotSupportedException (javax.transaction.NotSupportedException)5 ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)4 J2EEDataSource (org.apache.derbyTesting.junit.J2EEDataSource)4