有没有从Connection生成的Statement和ResultSet的生成方法外部进行close的方法?
我是日本人。英语不熟练。对不起。 有没有从Connection生成的Statement和ResultSet的生成方法外部进行close的方法?
但是,我想在不调用 Connection#close() 的情况下
我从方法的外部了解 Connection 的实例。close()
它。
I am Japanese. English is unskilled. I'm sorry.
Is there a method of doing close from the outside of the method of generating Statement and ResultSet generated from Connection?
However, I want to close()
it these one without calling Connection#close().
I learn the instance of Connection from the outside of the method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不可以。JDBC 要求您自己
关闭
所有这些对象(并在finally 块中以便能够处理异常)。仅关闭连接应该关闭从其派生的所有对象,但我不会依赖于此,而且最好尽快关闭这些对象。相反的情况肯定不正确(关闭 ResultSet 不会关闭连接),并且也无法从 ResultSet 获取父连接(我认为这是您问题的要点)。
我建议不要直接使用 JDBC,而是在上面使用更友好的框架,至少像 Commons DBUtils 这样最小的框架,它负责清理这些资源。
No. JDBC requires you to
close
all these objects by yourself (and in finally blocks to be able to handle exceptions).Closing just the connection should close all objects derived from it, but I would not depend on that, and it is better to close those as soon as possible anyway. The opposite is certainly not true (closing a ResultSet will not close the connection), and there is also no way to get the parent Connection from a ResultSet (which I think was the gist of your question).
I would suggest to not use JDBC directly, but a more friendly framework on top, at least something as minimal like Commons DBUtils, which takes care of cleaning up these resources.
当您打开任何连接或创建任何语句/结果集时,显式关闭它们。
完成 JDBC 代码后,始终尝试调用此方法
When ever you open any connection or creating any Statement/ResultSet explicitly close them.
Always try to call this method after you done with your JDBC code