Search in sources :

Example 56 with ConnectionPoolDataSource

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

the class cdsXid method testConnectionLeakInDatabaseMetaData.

/**
 * Tests that DatabaseMetaData.getConnection does not leak references to
 * physical connections or other logical connections.
 *
 * @throws SQLException if something goes wrong
 */
public void testConnectionLeakInDatabaseMetaData() throws SQLException {
    ConnectionPoolDataSource cpDs = J2EEDataSource.getConnectionPoolDataSource();
    PooledConnection pc = cpDs.getPooledConnection();
    // Get first logical connection and a meta data object.
    Connection con1 = pc.getConnection();
    DatabaseMetaData dmd1 = con1.getMetaData();
    assertSame(con1, dmd1.getConnection());
    con1.close();
    // Get second logical connection and a meta data object.
    Connection con2 = pc.getConnection();
    DatabaseMetaData dmd2 = con2.getMetaData();
    // The first meta data object should not return a reference to the
    // second logical connection.
    assertSame(con2, dmd2.getConnection());
    try {
        dmd1.getConnection();
        fail("Should have thrown no current connection exception");
    } catch (SQLException sqle) {
        assertSQLState("08003", sqle);
    }
    con2.close();
    pc.close();
    try {
        dmd2.getConnection();
        fail("Should have thrown no current connection exception");
    } catch (SQLException sqle) {
        assertSQLState("08003", sqle);
    }
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) PooledConnection(javax.sql.PooledConnection) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 57 with ConnectionPoolDataSource

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

the class cdsXid method testSchemaIsReset.

/**
 * Verifies that the schema is reset when creating a new logical connection.
 * <p>
 * The test is run in a non-statement pooling configuration first,
 * and then with statement pooling enabled if the environment supports it.
 * <p>
 * Relevant Jira issue: DERBY-3690.
 * <p>
 * The current role also needs to be reset, but can't be tested here since
 * we need to run with SQL authorization.
 *
 * @see org.apache.derbyTesting.functionTests.tests.lang.RolesTest#testCurrentRoleIsReset
 *
 * @throws SQLException if something goes wrong
 */
public void testSchemaIsReset() throws SQLException {
    final String userSchema = "USERSCHEMA";
    ConnectionPoolDataSource cpDs = J2EEDataSource.getConnectionPoolDataSource();
    J2EEDataSource.setBeanProperty(cpDs, "createDatabase", "create");
    // Connect with a user specified, which should cause the schema to be
    // set to the user name.
    // Test without statement pooling first.
    doTestSchemaIsReset(cpDs.getPooledConnection(userSchema, "secret"), userSchema);
    // This is currently only implemented in the client driver.
    if (usingDerbyNetClient()) {
        J2EEDataSource.setBeanProperty(cpDs, "maxStatements", 7);
        doTestSchemaIsReset(cpDs.getPooledConnection(userSchema, "secret"), userSchema);
    }
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource)

Example 58 with ConnectionPoolDataSource

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

the class cdsXid method testIsolationWithFourConnections.

/**
 * Check setTransactioIsolation and with four connection in connection pool
 * for DERBY-4343 case
 *
 * @throws SQLException
 */
public void testIsolationWithFourConnections() throws SQLException {
    ConnectionPoolDataSource ds = J2EEDataSource.getConnectionPoolDataSource();
    PooledConnection pc = ds.getPooledConnection();
    // First connection
    Connection conn = pc.getConnection();
    conn.setAutoCommit(false);
    conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
    Statement s = conn.createStatement();
    ResultSet rs = s.executeQuery("SELECT COUNT(*) FROM SYS.SYSTABLES");
    rs.next();
    int ri = rs.getInt(1);
    rs.close();
    conn.rollback();
    conn.close();
    // Second connection
    conn = pc.getConnection();
    conn.close();
    // Third connection
    conn = pc.getConnection();
    conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
    assertEquals(Connection.TRANSACTION_READ_COMMITTED, conn.getTransactionIsolation());
    conn.close();
    // Fourth connetion
    conn = pc.getConnection();
    conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
    assertEquals(Connection.TRANSACTION_READ_UNCOMMITTED, conn.getTransactionIsolation());
    conn.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) Savepoint(java.sql.Savepoint)

Example 59 with ConnectionPoolDataSource

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

the class cdsXid method testCloseActiveConnection_CP.

/**
 * Test that connections retrieved from {@code ConnectionPoolDataSource}
 * behave as expected when {@code close()} is called and the transaction is
 * active.
 */
public void testCloseActiveConnection_CP() throws SQLException {
    ConnectionPoolDataSource ds = J2EEDataSource.getConnectionPoolDataSource();
    PooledConnection pc = ds.getPooledConnection();
    testCloseActiveConnection(pc.getConnection(), true, false);
    Connection c = pc.getConnection();
    c.setAutoCommit(false);
    testCloseActiveConnection(c, false, false);
    pc.close();
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) PooledConnection(javax.sql.PooledConnection) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) PooledConnection(javax.sql.PooledConnection)

Example 60 with ConnectionPoolDataSource

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

the class ConnectionMethodsTest method testAbortPooled.

/**
 * Test the JDBC 4.1 Connection.abort(Executor) method on pooled connections.
 */
public void testAbortPooled() throws Exception {
    // 
    if (!TestConfiguration.loadingFromJars()) {
        return;
    }
    ConnectionPoolDataSource cpDs = J2EEDataSource.getConnectionPoolDataSource();
    PooledConnection conn0 = getPooledConnection(cpDs, "user0");
    PooledConnection conn1 = getPooledConnection(cpDs, "user1");
    PooledConnection conn2 = getPooledConnection(cpDs, "user2");
    abortVetter(conn0.getConnection(), conn1.getConnection(), conn2.getConnection());
    // verify that the underlying physical connection is closed
    try {
        conn1.getConnection();
        fail("Expected physical connection to be closed.");
    } catch (SQLException se) {
        assertSQLState(CLOSED_CONNECTION, se);
    }
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException)

Aggregations

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