JDBC 引入了一种称为closeOnAutoCompletion的方法,它声明当所有相关的结果集都关闭时,它会关闭语句。
我有一种创建准备好的语句的方法
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;
}
不,我按如下方式调用此方法
@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());
}
最后一次检查失败,因为语句没有关闭。
这是由于mysql实现的问题吗?还是我误解了 JavaDoc。
更新:我使用的是 5.1.24 版
谢谢,穆基