Muki
Muki

Reputation: 3641

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

Upvotes: 2

Views: 2460

Answers (2)

Mark Matthews
Mark Matthews

Reputation: 46

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.

Upvotes: 3

mthmulders
mthmulders

Reputation: 9705

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?

Upvotes: -1

Related Questions