Search in sources :

Example 6 with ConnectionPoolDataSource

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

the class cdsXid method testClosedCPDSConnection.

public void testClosedCPDSConnection() throws SQLException, Exception {
    // verify that outstanding updates from a closed connection, obtained
    // from a ConnectionPoolDataSource, are not committed, but rolled back.
    ConnectionPoolDataSource dsp = J2EEDataSource.getConnectionPoolDataSource();
    PooledConnection pc = dsp.getPooledConnection();
    Connection c1 = pc.getConnection();
    Statement s = c1.createStatement();
    // start by deleting all rows from intTable
    s.executeUpdate("delete from intTable");
    c1.setAutoCommit(false);
    // this update should get rolled back later
    s.executeUpdate("insert into intTable values(1)");
    // this should automatically close the original connection
    c1 = pc.getConnection();
    ResultSet rs = c1.createStatement().executeQuery("select count(*) from intTable");
    rs.next();
    assertEquals(0, rs.getInt(1));
    c1.close();
    // check connection objects are closed once connection is closed
    try {
        rs.next();
        fail("ResultSet is open for a closed connection obtained from PooledConnection");
    } catch (SQLException sqle) {
        // 08003 - No current connection; XCL16 - ResultSet not open
        if (usingEmbedded())
            assertSQLState("08003", sqle);
        else if (usingDerbyNetClient())
            assertSQLState("XCL16", sqle);
    }
    try {
        s.executeUpdate("update intTable set i = 1");
        fail("Statement is open for a closed connection " + "obtained from PooledConnection");
    } catch (SQLException sqle) {
        assertSQLState("08003", sqle);
    }
    pc.close();
    pc = null;
    PoolReset("ConnectionPoolDataSource", dsp.getPooledConnection());
    s.close();
    rs.close();
    c1.close();
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) PooledConnection(javax.sql.PooledConnection) ResultSet(java.sql.ResultSet)

Example 7 with ConnectionPoolDataSource

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

the class cdsXid method testBadConnectionAttributeSyntax.

// there is a corresponding fixture for datasources in DataSourceTest
public void testBadConnectionAttributeSyntax() throws SQLException {
    // ConnectionPoolDataSource - bad connatr syntax
    ConnectionPoolDataSource cpds = J2EEDataSource.getConnectionPoolDataSource();
    JDBCDataSource.setBeanProperty(cpds, "ConnectionAttributes", "bad");
    try {
        cpds.getPooledConnection();
        fail("should have seen an error");
    } catch (SQLException e) {
        assertSQLState("XJ028", e);
    }
    // XADataSource - bad connattr syntax");
    XADataSource xads = J2EEDataSource.getXADataSource();
    JDBCDataSource.setBeanProperty(xads, "ConnectionAttributes", "bad");
    try {
        xads.getXAConnection();
        fail("should have seen an error");
    } catch (SQLException e) {
        assertSQLState("XJ028", e);
    }
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) XADataSource(javax.sql.XADataSource) SQLException(java.sql.SQLException)

Example 8 with ConnectionPoolDataSource

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

the class cdsXid method testDerby3799.

/**
 * Regression test for a NullPointerException when trying to use the LOB
 * stored procedures after closing and then getting a new logical
 * connection. The problem was that the LOB stored procedure objects on the
 * server side were closed and not reprepared.
 * See Jira issue DERBY-3799.
 */
public void testDerby3799() throws SQLException {
    ConnectionPoolDataSource cpDs = J2EEDataSource.getConnectionPoolDataSource();
    PooledConnection pc = cpDs.getPooledConnection();
    // Get first logical connection.
    Connection con1 = pc.getConnection();
    Statement stmt = con1.createStatement();
    ResultSet rs = stmt.executeQuery("select dClob from derby3799");
    assertTrue(rs.next());
    rs.getString(1);
    rs.close();
    con1.close();
    // Get second logical connection.
    Connection con2 = pc.getConnection();
    stmt = con2.createStatement();
    rs = stmt.executeQuery("select dClob from derby3799");
    assertTrue(rs.next());
    // NPE happened here.
    rs.getString(1);
    con2.close();
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) PooledConnection(javax.sql.PooledConnection) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) PooledConnection(javax.sql.PooledConnection) ResultSet(java.sql.ResultSet)

Example 9 with ConnectionPoolDataSource

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

the class cdsXid method testAllDataSources.

public void testAllDataSources() throws SQLException, Exception {
    Connection dmc = getConnection();
    CallableStatement cs = dmc.prepareCall("call checkConn2(?)");
    cs.setString(1, "Nested");
    try {
        cs.execute();
        fail("SQLException of 40XC0 should be thrown!");
    } catch (SQLException sqle) {
        assertSQLState("40XC0", sqle);
    }
    cs.setString(1, "Nested2");
    cs.execute();
    String EmptyMapValue = "OK";
    String NullMapValue = "XJ081";
    String MapMapValue = "0A000";
    Object[] expectedValues = { ResultSet.HOLD_CURSORS_OVER_COMMIT, "XJ010", 2, true, false, EmptyMapValue, NullMapValue, MapMapValue };
    assertConnectionOK(expectedValues, "DriverManager ", dmc);
    if (usingEmbedded())
        assertTenConnectionsUnique();
    DataSource dscs = JDBCDataSource.getDataSource();
    if (usingEmbedded())
        assertToString(dscs);
    DataSource ds = dscs;
    assertConnectionOK(expectedValues, "DataSource", ds.getConnection());
    ConnectionPoolDataSource dsp = J2EEDataSource.getConnectionPoolDataSource();
    if (usingEmbedded())
        assertToString(dsp);
    PooledConnection pc = dsp.getPooledConnection();
    // checks currently only implemented for embedded
    if (usingEmbedded()) {
        SecurityCheck.assertSourceSecurity(pc, "javax.sql.PooledConnection");
    }
    AssertEventCatcher aes1 = new AssertEventCatcher(1);
    pc.addConnectionEventListener(aes1);
    // DERBY-2531
    // with Network Server / DerbyNetClient, the assertConnectionOK check
    // returns a different connection object...
    assertConnectionOK(expectedValues, "ConnectionPoolDataSource", pc.getConnection());
    // Check if got connection closed event but not connection error event
    assertTrue(aes1.didConnectionClosedEventHappen());
    assertFalse(aes1.didConnectionErrorEventHappen());
    aes1.resetState();
    assertConnectionOK(expectedValues, "ConnectionPoolDataSource", pc.getConnection());
    // Check if got connection closed event but not connection error event
    assertTrue(aes1.didConnectionClosedEventHappen());
    assertFalse(aes1.didConnectionErrorEventHappen());
    aes1.resetState();
    XADataSource dsx = J2EEDataSource.getXADataSource();
    if (usingEmbedded())
        assertToString(dsx);
    // shutdown db and check all's still ok thereafter
    TestConfiguration.getCurrent().shutdownDatabase();
    dmc = getConnection();
    cs = dmc.prepareCall("call checkConn2(?)");
    // checks currently only implemented for embedded
    if (usingEmbedded()) {
        SecurityCheck.assertSourceSecurity(cs, "java.sql.CallableStatement");
    }
    cs.setString(1, "Nested");
    try {
        cs.execute();
        fail("SQLException of 40XC0 should be thrown!");
    } catch (SQLException sqle) {
        assertSQLState("40XC0", sqle);
    }
    cs.setString(1, "Nested2");
    cs.execute();
    XAConnection xac = dsx.getXAConnection();
    // checks currently only implemented for embedded
    if (usingEmbedded()) {
        SecurityCheck.assertSourceSecurity(xac, "javax.sql.XAConnection");
    }
    AssertEventCatcher aes3 = new AssertEventCatcher(3);
    xac.addConnectionEventListener(aes3);
    assertConnectionOK(expectedValues, "XADataSource", xac.getConnection());
    // Check if got connection closed event but not connection error event
    assertTrue(aes3.didConnectionClosedEventHappen());
    assertFalse(aes3.didConnectionErrorEventHappen());
    aes3.resetState();
    pc = dsp.getPooledConnection();
    AssertEventCatcher aes2 = new AssertEventCatcher(2);
    pc.addConnectionEventListener(aes2);
    assertConnectionOK(expectedValues, "ConnectionPoolDataSource", pc.getConnection());
    // Check if got connection closed event but not connection error event
    assertTrue(aes2.didConnectionClosedEventHappen());
    assertFalse(aes2.didConnectionErrorEventHappen());
    aes2.resetState();
    // test "local" XAConnections
    xac = dsx.getXAConnection();
    AssertEventCatcher aes4 = new AssertEventCatcher(4);
    xac.addConnectionEventListener(aes4);
    assertConnectionOK(expectedValues, "XADataSource", xac.getConnection());
    // Check if got connection closed event but not connection error event
    assertTrue(aes4.didConnectionClosedEventHappen());
    assertFalse(aes4.didConnectionErrorEventHappen());
    aes4.resetState();
    assertConnectionOK(expectedValues, "XADataSource", xac.getConnection());
    // Check if got connection closed event but not connection error event
    assertTrue(aes4.didConnectionClosedEventHappen());
    assertFalse(aes4.didConnectionErrorEventHappen());
    aes4.resetState();
    xac.close();
    // test "global" XAConnections
    xac = dsx.getXAConnection();
    AssertEventCatcher aes5 = new AssertEventCatcher(5);
    xac.addConnectionEventListener(aes5);
    XAResource xar = xac.getXAResource();
    // checks currently only implemented for embedded
    if (usingEmbedded()) {
        SecurityCheck.assertSourceSecurity(xar, "javax.transaction.xa.XAResource");
    }
    Xid xid = new cdsXid(1, (byte) 35, (byte) 47);
    xar.start(xid, XAResource.TMNOFLAGS);
    Connection xacc = xac.getConnection();
    xacc.close();
    expectedValues[0] = ResultSet.CLOSE_CURSORS_AT_COMMIT;
    if (usingEmbedded())
        expectedValues[1] = "XJ058";
    expectedValues[3] = false;
    assertConnectionOK(expectedValues, "Global XADataSource", xac.getConnection());
    // Check if got connection closed event but not connection error event
    assertTrue(aes5.didConnectionClosedEventHappen());
    assertFalse(aes5.didConnectionErrorEventHappen());
    aes5.resetState();
    assertConnectionOK(expectedValues, "Global XADataSource", xac.getConnection());
    // Check if got connection closed event but not connection error event
    assertTrue(aes5.didConnectionClosedEventHappen());
    assertFalse(aes5.didConnectionErrorEventHappen());
    aes5.resetState();
    xar.end(xid, XAResource.TMSUCCESS);
    expectedValues[0] = ResultSet.HOLD_CURSORS_OVER_COMMIT;
    expectedValues[3] = true;
    assertConnectionOK(expectedValues, "Switch to local XADataSource", xac.getConnection());
    // Check if got connection closed event but not connection error event
    assertTrue(aes5.didConnectionClosedEventHappen());
    assertFalse(aes5.didConnectionErrorEventHappen());
    aes5.resetState();
    assertConnectionOK(expectedValues, "Switch to local XADataSource", xac.getConnection());
    // Check if got connection closed event but not connection error event
    assertTrue(aes5.didConnectionClosedEventHappen());
    assertFalse(aes5.didConnectionErrorEventHappen());
    aes5.resetState();
    Connection backtoGlobal = xac.getConnection();
    xar.start(xid, XAResource.TMJOIN);
    expectedValues[0] = ResultSet.CLOSE_CURSORS_AT_COMMIT;
    expectedValues[3] = false;
    assertConnectionOK(expectedValues, "Switch to global XADataSource", backtoGlobal);
    // Check if got connection closed event but not connection error event
    assertTrue(aes5.didConnectionClosedEventHappen());
    assertFalse(aes5.didConnectionErrorEventHappen());
    aes5.resetState();
    assertConnectionOK(expectedValues, "Switch to global XADataSource", xac.getConnection());
    // Check if got connection closed event but not connection error event
    assertTrue(aes5.didConnectionClosedEventHappen());
    assertFalse(aes5.didConnectionErrorEventHappen());
    aes5.resetState();
    xar.end(xid, XAResource.TMSUCCESS);
    xar.commit(xid, true);
    xac.close();
}
Also used : XADataSource(javax.sql.XADataSource) SQLException(java.sql.SQLException) 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) ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) PooledConnection(javax.sql.PooledConnection) XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) CallableStatement(java.sql.CallableStatement) XAConnection(javax.sql.XAConnection)

Example 10 with ConnectionPoolDataSource

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

the class cdsXid method testCreateInDatabaseName_Pooled.

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

Aggregations

ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)71 PooledConnection (javax.sql.PooledConnection)33 SQLException (java.sql.SQLException)28 Connection (java.sql.Connection)19 XAConnection (javax.sql.XAConnection)15 XADataSource (javax.sql.XADataSource)12 Statement (java.sql.Statement)8 CallableStatement (java.sql.CallableStatement)7 DataSource (javax.sql.DataSource)7 PreparedStatement (java.sql.PreparedStatement)6 J2EEDataSource (org.apache.derbyTesting.junit.J2EEDataSource)6 JDBCDataSource (org.apache.derbyTesting.junit.JDBCDataSource)6 ResultSet (java.sql.ResultSet)5 Context (javax.naming.Context)5 InitialContext (javax.naming.InitialContext)5 Test (org.junit.Test)3 Test (org.junit.jupiter.api.Test)3 Savepoint (java.sql.Savepoint)2 Properties (java.util.Properties)2 SQLiteConnectionPoolDataSource (org.sqlite.javax.SQLiteConnectionPoolDataSource)2