Search in sources :

Example 6 with StatementImpl

use of com.mysql.cj.jdbc.StatementImpl in project aws-mysql-jdbc by awslabs.

the class StatementRegressionTest method subTestBug68916ForHoldResultsOpenOverStatementClose.

private void subTestBug68916ForHoldResultsOpenOverStatementClose() throws Exception {
    Connection testConnection;
    String testStep;
    ResultSet testResultSet1, testResultSet2, testResultSet3;
    // We are testing against code that was compiled with Java 6, so methods isCloseOnCompletion() and
    // closeOnCompletion() aren't available in the Statement interface. We need to test directly our
    // implementations.
    StatementImpl testStatement = null;
    PreparedStatement testPrepStatement = null;
    CallableStatement testCallStatement = null;
    /*
         * Testing with connection property holdResultsOpenOverStatementClose=true
         */
    testStep = "Conn. Prop. 'holdResultsOpenOverStatementClose'";
    testConnection = getConnectionWithProps("holdResultsOpenOverStatementClose=true");
    /*
         * SUB-STEP 0: The basics (holdResultsOpenOverStatementClose=true)
         */
    // **testing Statement**
    // ResultSets should stay open when owning Statement is closed
    testStatement = (StatementImpl) testConnection.createStatement();
    assertFalse(testStatement.isCloseOnCompletion(), testStep + ".ST:0. Statement.isCloseOnCompletion(): false dy default.");
    assertFalse(testStatement.isClosed(), testStep + ".ST:0. Statement.isClosed(): false.");
    testStatement.closeOnCompletion();
    assertTrue(testStatement.isCloseOnCompletion(), testStep + ".ST:0. Statement.isCloseOnCompletion(): true after Statement.closeOnCompletion().");
    assertFalse(testStatement.isClosed(), testStep + ".ST:0. Statement.isClosed(): false.");
    testStatement.closeOnCompletion();
    assertTrue(testStatement.isCloseOnCompletion(), testStep + ".ST:0. Statement.isCloseOnCompletion(): true after 2nd Statement.closeOnCompletion().");
    // test Statement.close()
    testResultSet1 = testStatement.executeQuery("SELECT 1");
    assertFalse(testResultSet1.isClosed(), testStep + ".ST:0. ResultSet.isClosed(): false.");
    assertFalse(testStatement.isClosed(), testStep + ".ST:0. Statement.isClosed(): false.");
    testStatement.close();
    assertFalse(testResultSet1.isClosed(), testStep + ".ST:0. ResultSet.isClosed(): false after Statement.Close().");
    assertTrue(testStatement.isClosed(), testStep + ".ST:0. Statement.isClosed(): true after Statement.Close().");
    // **testing PreparedStatement**
    // ResultSets should stay open when owning PreparedStatement is closed
    testPrepStatement = testConnection.prepareStatement("SELECT 1");
    assertFalse(testPrepStatement.isCloseOnCompletion(), testStep + ".PS:0. PreparedStatement.isCloseOnCompletion(): false by default.");
    assertFalse(testPrepStatement.isClosed(), testStep + ".PS:0. PreparedStatement.isClosed(): false.");
    testPrepStatement.closeOnCompletion();
    assertTrue(testPrepStatement.isCloseOnCompletion(), testStep + ".PS:0. PreparedStatement.isCloseOnCompletion(): true after Statement.closeOnCompletion().");
    assertFalse(testPrepStatement.isClosed(), testStep + ".PS:0. PreparedStatement.isClosed(): false.");
    testPrepStatement.closeOnCompletion();
    assertTrue(testPrepStatement.isCloseOnCompletion(), testStep + ".PS:0. PreparedStatement.isCloseOnCompletion(): true after 2nd Statement.closeOnCompletion().");
    // test PreparedStatement.close()
    testPrepStatement.execute();
    testResultSet1 = testPrepStatement.getResultSet();
    assertFalse(testResultSet1.isClosed(), testStep + ".PS:0. ResultSet.isClosed(): false.");
    assertFalse(testPrepStatement.isClosed(), testStep + ".PS:0. PreparedStatement.isClosed(): false.");
    testPrepStatement.close();
    assertFalse(testResultSet1.isClosed(), testStep + ".PS:0. ResultSet.isClosed(): false after PreparedStatement.close().");
    assertTrue(testPrepStatement.isClosed(), testStep + ".PS:0. PreparedStatement.isClosed(): true after PreparedStatement.close().");
    /*
         * SUB-STEP 1: One ResultSet (holdResultsOpenOverStatementClose=true)
         */
    // **testing Statement**
    // Statement using closeOnCompletion should be closed when last ResultSet is closed
    testStatement = (StatementImpl) testConnection.createStatement();
    testStatement.closeOnCompletion();
    testResultSet1 = testStatement.executeQuery("SELECT 1");
    assertFalse(testResultSet1.isClosed(), testStep + ".ST:1. ResultSet.isClosed(): false.");
    assertFalse(testStatement.isClosed(), testStep + ".ST:1. Statement.isClosed(): false.");
    while (testResultSet1.next()) {
    }
    assertFalse(testResultSet1.isClosed(), testStep + ".ST:1. ResultSet.isClosed(): false after ResultSet have reached the end.");
    assertFalse(testStatement.isClosed(), testStep + ".ST:1. Statement.isClosed(): false.");
    // last open ResultSet, must close Statement
    testResultSet1.close();
    assertTrue(testResultSet1.isClosed(), testStep + ".ST:1. ResultSet.isClosed(): true.");
    assertTrue(testStatement.isClosed(), testStep + ".ST:1. Statement.isClosed(): true when last ResultSet is closed.");
    // test implicit resultset close keeping statement open, when following with an executeBatch()
    testStatement = (StatementImpl) testConnection.createStatement();
    testStatement.closeOnCompletion();
    testResultSet1 = testStatement.executeQuery("SELECT 1");
    testStatement.addBatch("INSERT INTO testBug68916_tbl (fld2) VALUES (1), (2), (3)");
    testStatement.executeBatch();
    assertFalse(testResultSet1.isClosed(), testStep + ".ST:1. ResultSet.isClosed(): false after executeBatch() in same Statement.");
    assertFalse(testStatement.isClosed(), testStep + ".ST:1. Statement.isClosed(): false.");
    testResultSet1 = testStatement.getGeneratedKeys();
    // last open ResultSet, must close Statement
    testResultSet1.close();
    assertTrue(testResultSet1.isClosed(), testStep + ".ST:1. ResultSet.isClosed(): true.");
    assertTrue(testStatement.isClosed(), testStep + ".ST:1. Statement.isClosed(): true when last ResultSet is closed.");
    // test implicit resultset close keeping statement open, when following with an executeUpdate()
    testStatement = (StatementImpl) testConnection.createStatement();
    testStatement.closeOnCompletion();
    testResultSet1 = testStatement.executeQuery("SELECT 1");
    testStatement.executeUpdate("INSERT INTO testBug68916_tbl (fld2) VALUES (1), (2), (3)", Statement.RETURN_GENERATED_KEYS);
    assertFalse(testResultSet1.isClosed(), testStep + ".ST:1. ResultSet.isClosed(): false after executeUpdate() in same Statement.");
    assertFalse(testStatement.isClosed(), testStep + ".ST:1. Statement.isClosed(): false.");
    testResultSet1 = testStatement.getGeneratedKeys();
    // last open ResultSet, must close Statement
    testResultSet1.close();
    assertTrue(testResultSet1.isClosed(), testStep + ".ST:1. ResultSet.isClosed(): true.");
    assertTrue(testStatement.isClosed(), testStep + ".ST:1. Statement.isClosed(): true when last ResultSet is closed.");
    // **testing PreparedStatement**
    // PreparedStatement using closeOnCompletion should be closed when last ResultSet is closed
    testPrepStatement = testConnection.prepareStatement("SELECT 1");
    testPrepStatement.closeOnCompletion();
    testResultSet1 = testPrepStatement.executeQuery();
    assertFalse(testResultSet1.isClosed(), testStep + ".PS:1. ResultSet.isClosed(): false.");
    assertFalse(testPrepStatement.isClosed(), testStep + ".PS:1. PreparedStatement.isClosed(): false.");
    while (testResultSet1.next()) {
    }
    assertFalse(testResultSet1.isClosed(), testStep + ".PS:1. ResultSet.isClosed(): false after ResultSet have reached the end.");
    assertFalse(testPrepStatement.isClosed(), testStep + ".PS:1. PreparedStatement.isClosed(): false.");
    // last open ResultSet, must close Statement
    testResultSet1.close();
    assertTrue(testResultSet1.isClosed(), testStep + ".PS:1. ResultSet.isClosed(): true.");
    assertTrue(testPrepStatement.isClosed(), testStep + ".PS:1. PreparedStatement.isClosed(): true when last ResultSet is closed.");
    /*
         * SUB-STEP 2: Multiple ResultSets, sequentially (holdResultsOpenOverStatementClose=true)
         */
    testStatement = (StatementImpl) testConnection.createStatement();
    testStatement.closeOnCompletion();
    testResultSet1 = testStatement.executeQuery("SELECT 1");
    // mustn't close testResultSet1
    testResultSet2 = testStatement.executeQuery("SELECT 2");
    assertFalse(testResultSet1.isClosed(), testStep + ".ST:2. ResultSet.isClosed(): false after 2nd Statement.executeQuery().");
    assertFalse(testResultSet2.isClosed(), testStep + ".ST:2. ResultSet.isClosed(): false.");
    assertFalse(testStatement.isClosed(), testStep + ".ST:2. Statement.isClosed(): false.");
    while (testResultSet2.next()) {
    }
    assertFalse(testResultSet1.isClosed(), testStep + ".ST:2. ResultSet.isClosed(): false.");
    assertFalse(testResultSet2.isClosed(), testStep + ".ST:2. ResultSet.isClosed(): false after ResultSet have reached the end.");
    assertFalse(testStatement.isClosed(), testStep + ".ST:2. Statement.isClosed(): false.");
    // mustn't close testResultSet1 nor testResultSet2
    testResultSet3 = testStatement.executeQuery("SELECT 3");
    assertFalse(testResultSet1.isClosed(), testStep + ".ST:2. ResultSet.isClosed(): false.");
    assertFalse(testResultSet2.isClosed(), testStep + ".ST:2. ResultSet.isClosed(): false after 3rd Statement.executeQuery().");
    assertFalse(testResultSet3.isClosed(), testStep + ".ST:2. ResultSet.isClosed(): false.");
    assertFalse(testStatement.isClosed(), testStep + ".ST:2. Statement.isClosed(): false.");
    testResultSet2.close();
    assertFalse(testResultSet1.isClosed(), testStep + ".ST:2. ResultSet.isClosed(): false.");
    assertTrue(testResultSet2.isClosed(), testStep + ".ST:2. ResultSet.isClosed(): true.");
    assertFalse(testResultSet3.isClosed(), testStep + ".ST:2. ResultSet.isClosed(): false.");
    assertFalse(testStatement.isClosed(), testStep + ".ST:2. Statement.isClosed(): false.");
    testResultSet1.close();
    // last open ResultSet, must close Statement
    testResultSet3.close();
    assertTrue(testResultSet1.isClosed(), testStep + ".ST:2. ResultSet.isClosed(): true.");
    assertTrue(testResultSet2.isClosed(), testStep + ".ST:2. ResultSet.isClosed(): true.");
    assertTrue(testResultSet3.isClosed(), testStep + ".ST:2. ResultSet.isClosed(): true.");
    assertTrue(testStatement.isClosed(), testStep + ".ST:2. Statement.isClosed(): true when last ResultSet is closed.");
    /*
         * SUB-STEP 3: Multiple ResultSets, returned at once (holdResultsOpenOverStatementClose=true)
         */
    // **testing Statement**
    // Statement using closeOnCompletion should be closed when last ResultSet is closed
    testStatement = (StatementImpl) testConnection.createStatement();
    testStatement.closeOnCompletion();
    assertTrue(testStatement.execute("CALL testBug68916_proc"), testStep + ".ST:3. There should be some ResultSets.");
    testResultSet1 = testStatement.getResultSet();
    assertFalse(testResultSet1.isClosed(), testStep + ".ST:3. ResultSet.isClosed(): false.");
    assertFalse(testStatement.isClosed(), testStep + ".ST:3. Statement.isClosed(): false.");
    assertTrue(testStatement.getMoreResults(Statement.KEEP_CURRENT_RESULT), testStep + ".ST:3. There should be more ResultSets.");
    testResultSet2 = testStatement.getResultSet();
    assertFalse(testResultSet1.isClosed(), testStep + ".ST:3. ResultSet.isClosed(): false after Statement.getMoreResults(Statement.KEEP_CURRENT_RESULT).");
    assertFalse(testResultSet2.isClosed(), testStep + ".ST:3. ResultSet.isClosed(): false.");
    assertFalse(testStatement.isClosed(), testStep + ".ST:3. Statement.isClosed(): false.");
    assertTrue(testStatement.getMoreResults(Statement.CLOSE_ALL_RESULTS), testStep + ".ST:3. There should be more ResultSets.");
    testResultSet3 = testStatement.getResultSet();
    assertTrue(testResultSet1.isClosed(), testStep + ".ST:3. ResultSet.isClosed(): true after Statement.getMoreResults(Statement.CLOSE_ALL_RESULTS).");
    assertTrue(testResultSet2.isClosed(), testStep + ".ST:3. ResultSet.isClosed(): true after Statement.getMoreResults(Statement.CLOSE_ALL_RESULTS).");
    assertFalse(testResultSet3.isClosed(), testStep + ".ST:3. ResultSet.isClosed(): false.");
    assertFalse(testStatement.isClosed(), testStep + ".ST:3. Statement.isClosed(): false.");
    // no more ResultSets, must close Statement
    assertFalse(testStatement.getMoreResults(), testStep + ".ST:3. There should be no more ResultSets.");
    assertTrue(testResultSet1.isClosed(), testStep + ".ST:3. ResultSet.isClosed(): true.");
    assertTrue(testResultSet2.isClosed(), testStep + ".ST:3. ResultSet.isClosed(): true.");
    assertTrue(testResultSet3.isClosed(), testStep + ".ST:3. ResultSet.isClosed(): true after last Satement.getMoreResults().");
    assertTrue(testStatement.isClosed(), testStep + ".ST:3. Statement.isClosed(): true when last ResultSet is closed.");
    // **testing CallableStatement**
    // CallableStatement using closeOnCompletion should be closed when last ResultSet is closed
    testCallStatement = testConnection.prepareCall("CALL testBug68916_proc");
    testCallStatement.closeOnCompletion();
    assertTrue(testCallStatement.execute(), testStep + ".CS:3. There should be some ResultSets.");
    testResultSet1 = testCallStatement.getResultSet();
    assertFalse(testResultSet1.isClosed(), testStep + ".CS:3. ResultSet.isClosed(): false.");
    assertFalse(testCallStatement.isClosed(), testStep + ".CS:3. CallableStatement.isClosed(): false.");
    assertTrue(testCallStatement.getMoreResults(Statement.KEEP_CURRENT_RESULT), testStep + ".CS:3. There should be more ResultSets.");
    testResultSet2 = testCallStatement.getResultSet();
    assertFalse(testResultSet1.isClosed(), testStep + ".CS:3. ResultSet.isClosed(): false after Statement.getMoreResults(Statement.KEEP_CURRENT_RESULT).");
    assertFalse(testResultSet2.isClosed(), testStep + ".CS:3. ResultSet.isClosed(): false.");
    assertFalse(testCallStatement.isClosed(), testStep + ".CS:3. CallableStatement.isClosed(): false.");
    assertTrue(testCallStatement.getMoreResults(Statement.CLOSE_ALL_RESULTS), testStep + ".CS:3. There should be more ResultSets.");
    testResultSet3 = testCallStatement.getResultSet();
    assertTrue(testResultSet1.isClosed(), testStep + ".CS:3. ResultSet.isClosed(): true after Statement.getMoreResults(Statement.CLOSE_ALL_RESULTS).");
    assertTrue(testResultSet2.isClosed(), testStep + ".CS:3. ResultSet.isClosed(): true after Statement.getMoreResults(Statement.CLOSE_ALL_RESULTS).");
    assertFalse(testResultSet3.isClosed(), testStep + ".CS:3. ResultSet.isClosed(): false.");
    assertFalse(testCallStatement.isClosed(), testStep + ".CS:3. CallableStatement.isClosed(): false.");
    // no more ResultSets, must close Statement
    assertFalse(testCallStatement.getMoreResults(), testStep + ".CS:3. There should be no more ResultSets.");
    assertTrue(testResultSet1.isClosed(), testStep + ".CS:3. ResultSet.isClosed(): true.");
    assertTrue(testResultSet2.isClosed(), testStep + ".CS:3. ResultSet.isClosed(): true.");
    assertTrue(testResultSet3.isClosed(), testStep + ".CS:3. ResultSet.isClosed(): true after last Satement.getMoreResults().");
    assertTrue(testCallStatement.isClosed(), testStep + ".CS:3. CallableStatement.isClosed(): true when last ResultSet is closed.");
    /*
         * SUB-STEP 4: Generated Keys ResultSet (holdResultsOpenOverStatementClose=true)
         */
    testStatement = (StatementImpl) testConnection.createStatement();
    testStatement.closeOnCompletion();
    testStatement.executeUpdate("INSERT INTO testBug68916_tbl (fld2) VALUES (1), (2), (3)", Statement.RETURN_GENERATED_KEYS);
    testResultSet1 = testStatement.getGeneratedKeys();
    assertTrue(testResultSet1.next(), testStep + ".ST:4. Statement.getGeneratedKeys(): should return some values.");
    assertFalse(testResultSet1.isClosed(), testStep + ".ST:4. ResultSet.isClosed(): false.");
    assertFalse(testStatement.isClosed(), testStep + ".ST:4. Statement.isClosed(): false.");
    // last open ResultSet, must close Statement
    testResultSet1.close();
    assertTrue(testResultSet1.isClosed(), testStep + ".ST:4. ResultSet.isClosed(): true.");
    assertTrue(testStatement.isClosed(), testStep + ".ST:4. Statement.isClosed(): true when last ResultSet is closed.");
    // test again and combine with simple query
    testStatement = (StatementImpl) testConnection.createStatement();
    testStatement.closeOnCompletion();
    testStatement.executeUpdate("INSERT INTO testBug68916_tbl (fld2) VALUES (4), (5), (6)", Statement.RETURN_GENERATED_KEYS);
    testResultSet1 = testStatement.getGeneratedKeys();
    assertTrue(testResultSet1.next(), testStep + ".ST:4. Statement.getGeneratedKeys(): should return some values.");
    testResultSet2 = testStatement.executeQuery("SELECT 2");
    assertFalse(testResultSet1.isClosed(), testStep + ".ST:4. ResultSet.isClosed(): false after executeQuery() in same Statement.");
    assertFalse(testResultSet2.isClosed(), testStep + ".ST:4. ResultSet.isClosed(): false.");
    assertFalse(testStatement.isClosed(), testStep + ".ST:4. Statement.isClosed(): false.");
    testResultSet2.close();
    assertFalse(testResultSet1.isClosed(), testStep + ".ST:4. ResultSet.isClosed(): false.");
    assertTrue(testResultSet2.isClosed(), testStep + ".ST:4. ResultSet.isClosed(): true.");
    assertFalse(testStatement.isClosed(), testStep + ".ST:4. Statement.isClosed(): false when last ResultSet is closed (still one open).");
    // last open ResultSet, must close Statement
    testResultSet1.close();
    assertTrue(testResultSet1.isClosed(), testStep + ".ST:4. ResultSet.isClosed(): true.");
    assertTrue(testResultSet2.isClosed(), testStep + ".ST:4. ResultSet.isClosed(): true.");
    assertTrue(testStatement.isClosed(), testStep + ".ST:4. Statement.isClosed(): true when last ResultSet is closed.");
    testConnection.close();
}
Also used : CallableStatement(java.sql.CallableStatement) StatementImpl(com.mysql.cj.jdbc.StatementImpl) ReplicationConnection(com.mysql.cj.jdbc.ha.ReplicationConnection) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) MysqlConnection(com.mysql.cj.MysqlConnection) ResultSet(java.sql.ResultSet) JdbcPreparedStatement(com.mysql.cj.jdbc.JdbcPreparedStatement) PreparedStatement(java.sql.PreparedStatement) ServerPreparedStatement(com.mysql.cj.jdbc.ServerPreparedStatement) ClientPreparedStatement(com.mysql.cj.jdbc.ClientPreparedStatement)

Example 7 with StatementImpl

use of com.mysql.cj.jdbc.StatementImpl in project aws-mysql-jdbc by awslabs.

the class ResultSetFactory method createFromResultsetRows.

/**
 * Build ResultSet from ResultsetRows
 *
 * @param resultSetType
 *            scrollability (TYPE_FORWARD_ONLY, TYPE_SCROLL_????)
 * @param resultSetConcurrency
 *            the type of result set (CONCUR_UPDATABLE or READ_ONLY)
 * @param rows
 *            {@link ResultsetRows}
 * @return ResultSetImpl
 * @throws SQLException
 *             if an error occurs
 */
public ResultSetImpl createFromResultsetRows(int resultSetConcurrency, int resultSetType, ResultsetRows rows) throws SQLException {
    ResultSetImpl rs;
    StatementImpl st = this.stmt;
    if (rows.getOwner() != null) {
        st = ((ResultSetImpl) rows.getOwner()).getOwningStatement();
    }
    switch(resultSetConcurrency) {
        case java.sql.ResultSet.CONCUR_UPDATABLE:
            rs = new UpdatableResultSet(rows, this.conn, st);
            break;
        default:
            // CONCUR_READ_ONLY
            rs = new ResultSetImpl(rows, this.conn, st);
            break;
    }
    rs.setResultSetType(resultSetType);
    rs.setResultSetConcurrency(resultSetConcurrency);
    if (rows instanceof ResultsetRowsCursor && st != null) {
        rs.setFetchSize(st.getFetchSize());
    }
    return rs;
}
Also used : StatementImpl(com.mysql.cj.jdbc.StatementImpl) ResultsetRowsCursor(com.mysql.cj.protocol.a.result.ResultsetRowsCursor)

Aggregations

StatementImpl (com.mysql.cj.jdbc.StatementImpl)7 MysqlConnection (com.mysql.cj.MysqlConnection)6 JdbcConnection (com.mysql.cj.jdbc.JdbcConnection)6 ServerPreparedStatement (com.mysql.cj.jdbc.ServerPreparedStatement)6 CallableStatement (java.sql.CallableStatement)6 Connection (java.sql.Connection)6 PreparedStatement (java.sql.PreparedStatement)6 ClientPreparedStatement (com.mysql.cj.jdbc.ClientPreparedStatement)5 JdbcPreparedStatement (com.mysql.cj.jdbc.JdbcPreparedStatement)5 ReplicationConnection (com.mysql.cj.jdbc.ha.ReplicationConnection)5 ResultSet (java.sql.ResultSet)5 XAConnection (javax.sql.XAConnection)5 Statement (java.sql.Statement)2 Properties (java.util.Properties)2 Test (org.junit.jupiter.api.Test)2 ExceptionInterceptor (com.mysql.cj.exceptions.ExceptionInterceptor)1 ExceptionInterceptorChain (com.mysql.cj.exceptions.ExceptionInterceptorChain)1 JdbcStatement (com.mysql.cj.jdbc.JdbcStatement)1 UpdatableResultSet (com.mysql.cj.jdbc.result.UpdatableResultSet)1 ResultsetRowsCursor (com.mysql.cj.protocol.a.result.ResultsetRowsCursor)1