本文介绍了当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首先关闭时会发生什么?

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会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 06:59