Search in sources :

Example 1 with ClientConnectionPoolDataSourceInterface

use of org.apache.derby.client.ClientConnectionPoolDataSourceInterface 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)

Example 2 with ClientConnectionPoolDataSourceInterface

use of org.apache.derby.client.ClientConnectionPoolDataSourceInterface in project derby by apache.

the class cdsXid method testConnectionFlowCommitRollback.

/**
 * Performs a test sequence accessing the server, then parses the client
 * connection trace file to obtain the number of commit or rollback
 * commands flowed from the client to the server.
 *
 * @param ds data source used to obtain a connection to the database
 *      (must be using the test framework defaults)
 * @param invokeExtra if {@code true} extra invocations of either commit or
 *      rollback are performed (depending on value of {@code isCommit})
 * @param isCommit if {@code true}, commits are invoked, otherwise
 *      rollbacks are invoked
 * @return The number of wire flows detected (depending on value of
 *      {@code isCommit}).
 * @throws IOException if reading/parsing the trace file fails
 * @throws SQLException if something goes wrong
 */
private int testConnectionFlowCommitRollback(Object ds, boolean invokeExtra, boolean isCommit) throws IOException, SQLException {
    final int extraInvokations = invokeExtra ? 25 : 0;
    final int rowCount = 10;
    final boolean isXA = ds instanceof XADataSource;
    final boolean isCP = ds instanceof ConnectionPoolDataSource;
    // Generate trace file name and define trace behavior.
    String dsType = (isXA ? "xa_" : (isCP ? "cp_" : ""));
    String tbl = "ds_" + dsType + (invokeExtra ? "base_" : "extra_") + (isCommit ? "commit" : "rollback");
    File traceFile = SupportFilesSetup.getReadWrite(tbl + ".trace");
    J2EEDataSource.setBeanProperty(ds, "traceFile", PrivilegedFileOpsForTests.getAbsolutePath(traceFile));
    J2EEDataSource.setBeanProperty(ds, "traceFileAppend", Boolean.FALSE);
    J2EEDataSource.setBeanProperty(ds, "traceLevel", BasicClientDataSource40.TRACE_ALL);
    // Obtain connection.
    PooledConnection physicalCon = null;
    Connection con;
    if (isXA) {
        physicalCon = ((XADataSource) ds).getXAConnection();
        con = physicalCon.getConnection();
    } else if (isCP) {
        physicalCon = ((ClientConnectionPoolDataSourceInterface) ds).getPooledConnection();
        con = physicalCon.getConnection();
    } else {
        con = ((DataSource) ds).getConnection();
    }
    con.setAutoCommit(false);
    // Run test sequence.
    // step 0: create table
    Statement stmt = con.createStatement();
    stmt.executeUpdate("create table " + tbl + " (id int)");
    // Unconditional commit to persist table
    con.commit();
    endTranscation(con, isCommit, extraInvokations);
    // step 1: insert data
    PreparedStatement ps = con.prepareStatement("insert into " + tbl + " values (?)");
    for (int i = 0; i < rowCount; i++) {
        ps.setInt(1, i);
        ps.executeUpdate();
        endTranscation(con, isCommit, extraInvokations);
    }
    ps.close();
    // Unconditional commit, should catch "missed" rollbacks above when we
    // do a select with another connection at the end.
    con.commit();
    // step 2: select data
    ResultSet rs = stmt.executeQuery("select count(*) from " + tbl);
    rs.next();
    rs.getInt(1);
    rs.close();
    endTranscation(con, isCommit, extraInvokations);
    // step 3: values clause
    rs = stmt.executeQuery("values 7");
    assertTrue(rs.next());
    assertEquals(7, rs.getInt(1));
    rs.close();
    stmt.close();
    endTranscation(con, isCommit, extraInvokations);
    con.close();
    if (physicalCon != null) {
        physicalCon.close();
    }
    // step 4: table content validation
    stmt = createStatement();
    rs = stmt.executeQuery("select count(*) from " + tbl);
    rs.next();
    assertEquals("Potential COMMIT/ROLLBACK protocol error", isCommit ? rowCount : 0, rs.getInt(1));
    // Parse the trace file for commits or rollbacks.
    String token = "SEND BUFFER: " + (isXA ? "SYNCCTL" : (isCommit ? "RDBCMM" : "RDBRLLBCK"));
    int tokenCount = 0;
    BufferedReader r = new BufferedReader(PrivilegedFileOpsForTests.getFileReader(traceFile));
    String line;
    while ((line = r.readLine()) != null) {
        if (line.startsWith("[derby]") && line.indexOf(token) != -1) {
            println((isCommit ? "COMMIT: " : "ROLLBACK: ") + line);
            tokenCount++;
        }
    }
    r.close();
    assertTrue("Parsing failed, no COMMITS/ROLLBACKS detected", tokenCount > 0);
    println(ds.getClass().getName() + ", invokeExtra=" + invokeExtra + ", isCommit=" + isCommit + ", tokenCount=" + tokenCount);
    return tokenCount;
}
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) Savepoint(java.sql.Savepoint) 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) ResultSet(java.sql.ResultSet) BufferedReader(java.io.BufferedReader) File(java.io.File) ClientConnectionPoolDataSourceInterface(org.apache.derby.client.ClientConnectionPoolDataSourceInterface)

Example 3 with ClientConnectionPoolDataSourceInterface

use of org.apache.derby.client.ClientConnectionPoolDataSourceInterface in project derby by apache.

the class ClientConnectionPoolDataSourceTest method testMaxStatementsProperty.

/**
 * Verify that handling of the {@code maxStatements} property is working.
 */
public void testMaxStatementsProperty() throws Exception {
    ClientConnectionPoolDataSourceInterface cDs;
    Class<?> clazz;
    if (JDBC.vmSupportsJNDI()) {
        clazz = Class.forName("org.apache.derby.jdbc.ClientConnectionPoolDataSource");
        cDs = (ClientConnectionPoolDataSourceInterface) clazz.getConstructor().newInstance();
    } else {
        clazz = Class.forName("org.apache.derby.jdbc.BasicClientConnectionPoolDataSource40");
        cDs = (ClientConnectionPoolDataSourceInterface) clazz.getConstructor().newInstance();
    }
    // Check the default value.
    assertEquals("Unexpected default value", 0, cDs.getMaxStatements());
    cDs.setMaxStatements(25);
    // Verify that the new value has been set.
    assertEquals("New value not set", 25, cDs.getMaxStatements());
    // Try a negative value
    try {
        cDs.setMaxStatements(-99);
        fail("Negative values should not be allowed: " + cDs.getMaxStatements());
    } catch (IllegalArgumentException iae) {
    // As expected, continue the test.
    }
    // Try setting it to zero to disable statement pooling.
    cDs.setMaxStatements(0);
    assertEquals("New value not set", 0, cDs.getMaxStatements());
}
Also used : ClientConnectionPoolDataSourceInterface(org.apache.derby.client.ClientConnectionPoolDataSourceInterface)

Example 4 with ClientConnectionPoolDataSourceInterface

use of org.apache.derby.client.ClientConnectionPoolDataSourceInterface in project derby by apache.

the class ClientConnectionPoolDataSourceTest method testGetConnectionWithStatementPooling.

/**
 * Tests basic connectivity when connection is obtained from a connection
 * pool data source with statement pooling enabled.
 *
 * @throws SQLException if database operations fail
 */
public void testGetConnectionWithStatementPooling() throws SQLException {
    ClientConnectionPoolDataSourceInterface cDs = (ClientConnectionPoolDataSourceInterface) J2EEDataSource.getConnectionPoolDataSource();
    // Enable statement pooling.
    cDs.setMaxStatements(27);
    assertTrue(cDs.getMaxStatements() > 0);
    verifyConnection(cDs);
}
Also used : ClientConnectionPoolDataSourceInterface(org.apache.derby.client.ClientConnectionPoolDataSourceInterface)

Example 5 with ClientConnectionPoolDataSourceInterface

use of org.apache.derby.client.ClientConnectionPoolDataSourceInterface in project derby by apache.

the class ClientConnectionPoolDataSourceTest method testGetConnectionNoStatementPooling.

/**
 * Tests basic connectivity when connection is obtained from a connection
 * pool data source without statement pooling.
 *
 * @throws SQLException if database operations fail
 */
public void testGetConnectionNoStatementPooling() throws SQLException {
    ClientConnectionPoolDataSourceInterface cDs = (ClientConnectionPoolDataSourceInterface) J2EEDataSource.getConnectionPoolDataSource();
    // Make sure statement pooling is disabled.
    cDs.setMaxStatements(0);
    assertEquals(0, cDs.getMaxStatements());
    verifyConnection(cDs);
}
Also used : ClientConnectionPoolDataSourceInterface(org.apache.derby.client.ClientConnectionPoolDataSourceInterface)

Aggregations

ClientConnectionPoolDataSourceInterface (org.apache.derby.client.ClientConnectionPoolDataSourceInterface)6 PooledConnection (javax.sql.PooledConnection)2 XAConnection (javax.sql.XAConnection)2 ClientXADataSourceInterface (org.apache.derby.client.ClientXADataSourceInterface)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 CallableStatement (java.sql.CallableStatement)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Savepoint (java.sql.Savepoint)1 Statement (java.sql.Statement)1 ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)1 DataSource (javax.sql.DataSource)1 XADataSource (javax.sql.XADataSource)1 J2EEDataSource (org.apache.derbyTesting.junit.J2EEDataSource)1 JDBCDataSource (org.apache.derbyTesting.junit.JDBCDataSource)1