简体   繁体   English

MySQL JDBC closeOnCompletion对PreparedStatement不起作用

[英]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. JDBC引入了一个名为closeOnAutoCompletion的方法,该方法声明当所有相关的resultSets关闭时,它关闭该语句。

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? 这是由于mysql实现引起的问题吗? Or did I missunderstand the JavaDoc. 还是我误解了JavaDoc。

Update : I'm using version 5.1.24 更新 :我正在使用版本5.1.24

thanks, Muki 谢谢,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. 请注意,Statement的API文档说,当所有从属结果集都关闭时,closeOnCompletion()会关闭该语句,而不是滚动到末尾,因此我不确定在您的情况下实际上假定发生了什么行为。

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 . 根据此公告 ,MySQL Connector / J的5.1.21版本(MySQL的JDBC驱动程序的正式名称)应支持closeOnAutoCompletion Can you check you're using that version? 您可以检查您是否正在使用该版本?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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