正如标题所述,我想知道为什么准备好的语句.isClosed()方法在关闭底层流(使用JDBC)后将返回false。
public void someTest() throws SQLException, InterruptedException {
Connection conn = DBHelper.newConnection();
PreparedStatement statement = conn.prepareCall(CLEANUP_DELETE);
conn.close();
System.out.println(statement.isClosed());
Thread.sleep(1000);
System.out.println(statement.isClosed());
}输出
false
false但是,如果试图调用set方法或execute,则会得到以下结果:
java.sql.SQLException: org.apache.commons.dbcp.DelegatingCallableStatement with address: "SQLServerCallableStatement:32" is closed.
at org.apache.commons.dbcp.DelegatingStatement.checkOpen(DelegatingStatement.java:137)
at org.apache.commons.dbcp.DelegatingPreparedStatement.setInt(DelegatingPreparedStatement.java:120)
at org.apache.commons.dbcp.DelegatingPreparedStatement.setInt(DelegatingPreparedStatement.java:120)
at com.ipti.ptl.common.messagetable.MessageHandlerTest.someTest(MessageHandlerTest.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606) .....发布于 2014-04-09 19:04:18
这可能是DBCP库中的一个bug,尽管我必须查看JDBC规范。您是否尝试过在驱动程序中使用本地JDBC PreparedStatement?
编辑
查看MySQL连接器源代码(可用这里),看起来Connection.close()的实现实际上关闭了与连接相关的所有语句。因此,当使用MySQL JDBC驱动程序时,如果连接关闭,PreparedStatement.isClosed() 将返回。不过,我不太确定JDBC规范对此有何看法。
在您的例子中,我不确定DBCP库是否将对isClosed()的调用委托给底层驱动程序,这就是为什么我建议删除这个额外的层,看看会发生什么。如果DBCP中有错误,我会报告它。如果您正在使用的底层驱动程序中有错误,我也会报告。假设这确实是个错误。
编辑2
根据注释,JDBC4.1规范指出(9.4.4.1节):
Connection.close 应用程序调用方法Connection.close()来指示它已经使用了一个连接。调用连接对象的close方法时,从给定连接对象创建的所有语句对象都将被关闭。
因此,我预计声明将确实结束。因此,无论是在DBCP库中还是在使用驱动程序中都存在一个bug。
https://stackoverflow.com/questions/22971699
复制相似问题