简体   繁体   中英

MySQL JDBC closeOnCompletion has no effect on PreparedStatement

JDBC introduced a method called closeOnAutoCompletion , which states that it closes the statement, when all dependent resultSets are close.

I have a method to create prepared statements

public final PreparedStatement statement(Connection connection) throws SQLException {
    PreparedStatement stmt = connection.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY,
            ResultSet.CONCUR_READ_ONLY);
    stmt.closeOnCompletion();
    return stmt;
}

No I'm calling this method as follows

@Test
public void testCloseOnCompletionSemiManually() throws SQLException {
    PreparedStatement stmt = shards.statement(db);
    assertTrue("Statement must be closeOnAutoCompletion", stmt.isCloseOnCompletion());

    try (ResultSet rs = stmt.executeQuery()) {
        while (rs.next()) {
            //System.out.println("Shard id: " + rs.getInt("SHARD_ID"));
        }
    }
    assertTrue("Statement must be closed after all results sets have been processed", stmt.isClosed());
}

The last check fails as the statement is not closed.

Is this a problem due to the mysql implementation? Or did I missunderstand the JavaDoc.

Update : I'm using version 5.1.24

thanks, Muki

The driver doesn't support it yet, however the test is not correct either. Note that the API docs for Statement say that closeOnCompletion() closes the statement when all dependent result sets are closed , not scrolled past the end, so I'm not sure what behavior is actually assumed to be happening in your case.

According to this announcement , version 5.1.21 of the MySQL Connector/J (the official name of the JDBC driver for MySQL) should support closeOnAutoCompletion . Can you check you're using that version?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM