当ResultSet关闭时,Statement会发生什么?

编程入门 行业动态 更新时间:2024-10-20 16:00:53
本文介绍了当ResultSet关闭时,Statement会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

结果集关闭时语句会发生什么?

What happens with the statement when result set is closed?

Statement stmt = null; ResultSet rs = null; try { stmt = con.createStatement(); rs = stmt.executeQuery(query.toString()); ... } // lots of code rs.close()

注意:一个ResultSet对象由Statement 对象自动关闭,该对象在该Statement对象关闭时生成它, 重新执行,或用于从多个结果序列中检索下一个结果。

Note: A ResultSet object is automatically closed by the Statement object that generated it when that Statement object is closed, re-executed, or is used to retrieve the next result from a sequence of multiple results.

但是当ResultSet首先关闭时会发生什么?

But what happens when ResultSet is closed first?

对于什么问题,首先应该发生什么?

For what matter, what should happen first?

推荐答案

您应该按照创建它们的相反顺序关闭对象。 首先, ResultSet ,然后是 Statement ,最后,连接。

You should close the objects in the reverse order of creating them. So first, the ResultSet, then the Statement and in the end, Connection.

如果你没有关闭声明它会保持打开状态可以用它执行另一个查询(如果它是 PreparedStatement 或 CallableStatement 你可以更改查询参数)。

If you don't close the Statement it stays open and you can execute another query with it (if it's a PreparedStatement or CallableStatement you are able to change query parameters).

另请注意,您引用的是JDBC规范,其实现取决于JDBC驱动程序提供程序。通常,您不应该信任它们,并手动关闭这些对象。

Also note, that what you quoted is a JDBC specification, and the implementation of it is up to a JDBC driver provider. Usually, you should not trust them, and manually close those objects.

另一件事 - 我认为更重要的是 - 如果你正在使用连接池(比如在JBoss上),关闭连接只是将它释放回池中,以及底层对象没有被释放。因此,建议始终手动释放您创建的所有对象。

The other thing - I think more important - if you are using connection pools (like on JBoss), closing connection just releases it back to the pool, and the underlying objects are not released. And because of this, its recommended to always manually release all the objects you created.

更多推荐

当ResultSet关闭时,Statement会发生什么?

本文发布于:2023-10-18 11:02:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504049.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发生   ResultSet   Statement

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!