Search in sources :

Example 26 with XAConnection

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

the class cdsXid method testSetIsolationWithStatement.

// really part of testGlobalLocalInterLeaf:
/**
 * @throws SQLException
 * @throws XAException
 */
public void testSetIsolationWithStatement() throws SQLException, XAException {
    // 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();
    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, true, false, conn);
    // Issue setTransactionIsolation in local transaction
    conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
    // setTransactionIsolation in local
    assertConnectionState(ResultSet.HOLD_CURSORS_OVER_COMMIT, Connection.TRANSACTION_READ_UNCOMMITTED, true, false, conn);
    Xid xid;
    // Issue SQL to change isolation in local transaction
    s.executeUpdate("set current isolation = RR");
    assertConnectionState(ResultSet.HOLD_CURSORS_OVER_COMMIT, Connection.TRANSACTION_SERIALIZABLE, true, false, conn);
    xid = new cdsXid(1, (byte) 35, (byte) 47);
    xar.start(xid, XAResource.TMNOFLAGS);
    // 1st global (new)
    assertConnectionState(ResultSet.CLOSE_CURSORS_AT_COMMIT, Connection.TRANSACTION_SERIALIZABLE, false, false, conn);
    xar.end(xid, XAResource.TMSUCCESS);
    // local
    assertConnectionState(ResultSet.HOLD_CURSORS_OVER_COMMIT, Connection.TRANSACTION_SERIALIZABLE, true, false, conn);
    // Issue SQL to change isolation in local transaction
    s.executeUpdate("set current isolation = RS");
    assertConnectionState(ResultSet.HOLD_CURSORS_OVER_COMMIT, Connection.TRANSACTION_REPEATABLE_READ, true, false, conn);
    // DERBY-1325 - Isolation level of local connection does not get reset after ending
    // a global transaction that was joined/resumed if the isolation level was changed
    // using SQL
    xar.start(xid, XAResource.TMJOIN);
    // 1st global(existing)
    assertConnectionState(ResultSet.CLOSE_CURSORS_AT_COMMIT, Connection.TRANSACTION_SERIALIZABLE, false, false, conn);
    xar.end(xid, XAResource.TMSUCCESS);
    // local
    assertConnectionState(ResultSet.HOLD_CURSORS_OVER_COMMIT, Connection.TRANSACTION_REPEATABLE_READ, true, false, conn);
    // DERBY-1325 end test
    Xid xid2 = new cdsXid(1, (byte) 93, (byte) 103);
    xar.start(xid2, XAResource.TMNOFLAGS);
    // 2nd global (new)
    assertConnectionState(ResultSet.CLOSE_CURSORS_AT_COMMIT, Connection.TRANSACTION_REPEATABLE_READ, false, false, conn);
    // DERBY-4314 create a statement(s2) and execute ddl sql
    Statement s2 = conn.createStatement();
    s2.executeUpdate("create table testglobal (i int)");
    // DERBY-4314 end test
    xar.end(xid2, XAResource.TMSUCCESS);
    xar.start(xid, XAResource.TMJOIN);
    // 1st global (existing)
    assertConnectionState(ResultSet.CLOSE_CURSORS_AT_COMMIT, Connection.TRANSACTION_SERIALIZABLE, false, false, conn);
    xar.end(xid, XAResource.TMSUCCESS);
    // local
    assertConnectionState(ResultSet.HOLD_CURSORS_OVER_COMMIT, Connection.TRANSACTION_REPEATABLE_READ, true, false, conn);
    xar.start(xid, XAResource.TMJOIN);
    // 1st global (existing)
    assertConnectionState(ResultSet.CLOSE_CURSORS_AT_COMMIT, Connection.TRANSACTION_SERIALIZABLE, false, false, conn);
    // Issue SQL to change isolation in 1st global transaction
    s.executeUpdate("set current isolation = UR");
    assertConnectionState(ResultSet.CLOSE_CURSORS_AT_COMMIT, Connection.TRANSACTION_READ_UNCOMMITTED, false, false, conn);
    xar.end(xid, XAResource.TMSUCCESS);
    // local
    assertConnectionState(ResultSet.HOLD_CURSORS_OVER_COMMIT, Connection.TRANSACTION_READ_UNCOMMITTED, true, false, conn);
    xar.start(xid2, XAResource.TMJOIN);
    // 2nd global (existing)
    assertConnectionState(ResultSet.CLOSE_CURSORS_AT_COMMIT, Connection.TRANSACTION_REPEATABLE_READ, false, false, conn);
    xar.end(xid2, XAResource.TMSUCCESS);
    xar.rollback(xid2);
    // (After 2nd global rollback ) local
    assertConnectionState(ResultSet.HOLD_CURSORS_OVER_COMMIT, Connection.TRANSACTION_READ_UNCOMMITTED, true, false, conn);
    xar.rollback(xid);
    // (After 1st global rollback) local
    assertConnectionState(ResultSet.HOLD_CURSORS_OVER_COMMIT, Connection.TRANSACTION_READ_UNCOMMITTED, true, false, conn);
    // Confirm - no connection closed event & connection error event
    assertFalse(aes6.didConnectionClosedEventHappen());
    assertFalse(aes6.didConnectionErrorEventHappen());
    aes6.resetState();
}
Also used : XADataSource(javax.sql.XADataSource) XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) PooledConnection(javax.sql.PooledConnection) XAConnection(javax.sql.XAConnection)

Example 27 with XAConnection

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

the class cdsXid method testDerby2026LoginTimeout.

/**
 * DERBY-2026 - Make sure login timeout does not impact
 * queries.
 */
public void testDerby2026LoginTimeout() throws SQLException {
    DataSource jds = null;
    try {
        jds = JDBCDataSource.getDataSource();
        jds.setLoginTimeout(10);
        Connection conn = jds.getConnection();
        CallableStatement cs = conn.prepareCall("CALL TESTROUTINE.SLEEP(20000)");
        cs.execute();
        // rollback to make sure our connection is ok.
        conn.rollback();
    } finally {
        if (jds != null)
            jds.setLoginTimeout(0);
    }
    ConnectionPoolDataSource cpds = null;
    try {
        cpds = J2EEDataSource.getConnectionPoolDataSource();
        cpds.setLoginTimeout(10);
        PooledConnection pc = cpds.getPooledConnection();
        Connection conn = pc.getConnection();
        CallableStatement cs = conn.prepareCall("CALL TESTROUTINE.SLEEP(20000)");
        cs.execute();
        // rollback to make sure our connection is ok.
        conn.rollback();
        // Close the logical connection and get a new one.
        // This will invoke reset which also needs its timeout reset
        conn.close();
        conn = pc.getConnection();
        cs = conn.prepareCall("CALL TESTROUTINE.SLEEP(20000)");
        cs.execute();
        // rollback to make sure our connection is ok.
        conn.rollback();
    } finally {
        if (cpds != null)
            cpds.setLoginTimeout(0);
    }
    XADataSource xads = null;
    try {
        xads = J2EEDataSource.getXADataSource();
        xads.setLoginTimeout(10);
        XAConnection xac = xads.getXAConnection();
        Connection conn = xac.getConnection();
        CallableStatement cs = conn.prepareCall("CALL TESTROUTINE.SLEEP(20000)");
        cs.execute();
        // rollback to make sure our connection is ok.
        conn.rollback();
        // Close the logical connection and get a new one.
        // This will invoke reset which also needs its timeout reset
        conn.close();
        conn = xac.getConnection();
        cs = conn.prepareCall("CALL TESTROUTINE.SLEEP(20000)");
        cs.execute();
        // rollback to make sure our connection is ok.
        conn.rollback();
    } finally {
        if (xads != null)
            xads.setLoginTimeout(0);
    }
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) PooledConnection(javax.sql.PooledConnection) XADataSource(javax.sql.XADataSource) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) PooledConnection(javax.sql.PooledConnection) XADataSource(javax.sql.XADataSource) J2EEDataSource(org.apache.derbyTesting.junit.J2EEDataSource) DataSource(javax.sql.DataSource) ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) JDBCDataSource(org.apache.derbyTesting.junit.JDBCDataSource) XAConnection(javax.sql.XAConnection)

Example 28 with XAConnection

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

the class cdsXid method testCreateInDatabaseName_XA.

/**
 * Test that a connection to XADataSource can be established
 * successfully while creating a database using setDatabaseName()
 * with create=true
 *
 * @throws SQLException
 * @throws XAException
 */
public void testCreateInDatabaseName_XA() throws SQLException, XAException {
    // test with XADataSource
    XADataSource xads = J2EEDataSource.getXADataSource();
    String dbName = TestConfiguration.getCurrent().getDefaultDatabaseName();
    J2EEDataSource.setBeanProperty(xads, "databaseName", dbName + ";create=true");
    XAConnection xa = xads.getXAConnection();
    Connection c = xa.getConnection();
    c.setAutoCommit(false);
    c.close();
}
Also used : XADataSource(javax.sql.XADataSource) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) PooledConnection(javax.sql.PooledConnection) XAConnection(javax.sql.XAConnection)

Example 29 with XAConnection

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

the class cdsXid method testSetSchemaInXAConnection.

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();
}
Also used : XADataSource(javax.sql.XADataSource) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) PooledConnection(javax.sql.PooledConnection) PreparedStatement(java.sql.PreparedStatement) XAConnection(javax.sql.XAConnection)

Example 30 with XAConnection

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

the class cdsXid method testClientMessageTextConnectionAttribute.

/**
 * Check that messageText connection attribute functions correctly.
 * retrievemessagetext was tested in checkdriver, and derbynet/testij,
 * but not tested for datasources, and in datasourcepermissions_net,
 * but as it has nothing to do with permissions/authentication,
 * this test seems a better place for it.
 *
 * There is a corresponding fixture for clientDataSource in DataSourceTest
 *
 * @throws SQLException
 */
public void testClientMessageTextConnectionAttribute() throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
    String retrieveMessageTextProperty = "retrieveMessageText";
    // with ConnectionPoolDataSource
    // ConnectionPoolDataSource - retrieveMessageTextProperty
    ClientConnectionPoolDataSourceInterface cpds;
    Class<?> clazz;
    if (JDBC.vmSupportsJNDI()) {
        clazz = Class.forName("org.apache.derby.jdbc.ClientConnectionPoolDataSource");
        cpds = (ClientConnectionPoolDataSourceInterface) clazz.getConstructor().newInstance();
    } else {
        clazz = Class.forName("org.apache.derby.jdbc.BasicClientConnectionPoolDataSource40");
        cpds = (ClientConnectionPoolDataSourceInterface) clazz.getConstructor().newInstance();
    }
    cpds.setPortNumber(TestConfiguration.getCurrent().getPort());
    cpds.setDatabaseName(dbName);
    cpds.setConnectionAttributes(retrieveMessageTextProperty + "=false");
    PooledConnection cpConn = cpds.getPooledConnection();
    assertMessageText(cpConn.getConnection(), "false");
    cpConn.close();
    cpds.setConnectionAttributes(retrieveMessageTextProperty + "=true");
    cpConn = cpds.getPooledConnection();
    assertMessageText(cpConn.getConnection(), "true");
    cpds.setConnectionAttributes(null);
    cpConn.close();
    // now with XADataSource
    ClientXADataSourceInterface xads;
    if (JDBC.vmSupportsJNDI()) {
        clazz = Class.forName("org.apache.derby.jdbc.ClientXADataSource");
        xads = (ClientXADataSourceInterface) clazz.getConstructor().newInstance();
    } else {
        clazz = Class.forName("org.apache.derby.jdbc.BasicClientXADataSource40");
        xads = (ClientXADataSourceInterface) clazz.getConstructor().newInstance();
    }
    // XADataSource - retrieveMessageTextProperty
    xads.setPortNumber(TestConfiguration.getCurrent().getPort());
    xads.setDatabaseName(dbName);
    xads.setConnectionAttributes(retrieveMessageTextProperty + "=false");
    XAConnection xaConn = xads.getXAConnection();
    assertMessageText(xaConn.getConnection(), "false");
    xaConn.close();
    xads.setConnectionAttributes(retrieveMessageTextProperty + "=true");
    xaConn = xads.getXAConnection();
    assertMessageText(xaConn.getConnection(), "true");
    xaConn.close();
    xads.setConnectionAttributes(null);
}
Also used : PooledConnection(javax.sql.PooledConnection) ClientXADataSourceInterface(org.apache.derby.client.ClientXADataSourceInterface) ClientConnectionPoolDataSourceInterface(org.apache.derby.client.ClientConnectionPoolDataSourceInterface) XAConnection(javax.sql.XAConnection)

Aggregations

XAConnection (javax.sql.XAConnection)118 Connection (java.sql.Connection)78 XAResource (javax.transaction.xa.XAResource)57 XADataSource (javax.sql.XADataSource)52 Xid (javax.transaction.xa.Xid)44 Statement (java.sql.Statement)34 SQLException (java.sql.SQLException)31 PooledConnection (javax.sql.PooledConnection)24 PreparedStatement (java.sql.PreparedStatement)23 ResultSet (java.sql.ResultSet)22 Test (org.junit.Test)19 CallableStatement (java.sql.CallableStatement)18 XAException (javax.transaction.xa.XAException)16 FirebirdConnection (org.firebirdsql.jdbc.FirebirdConnection)9 Transaction (javax.transaction.Transaction)8 DataSource (javax.sql.DataSource)7 TestFBXAResource (org.firebirdsql.jaybird.xca.TestFBXAResource)7 XidImpl (org.firebirdsql.jaybird.xca.TestXABase.XidImpl)7 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)7 ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)4