java 为什么我应该在 ResultSet 和 Connection 实例上调用 close() ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11731651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why should I call close() on a ResultSet and Connection instance?
提问by sweet dreams
When I don't need to use instances of those ResultSet and Connection anymore in my program, why should I call the .close() method on both of them ?
当我不再需要在我的程序中使用那些 ResultSet 和 Connection 的实例时,为什么要对它们都调用 .close() 方法?
What are the dangers (if any) of not doing so ?
不这样做有什么危险(如果有的话)?
采纳答案by Bohemian
There are two questions here:
这里有两个问题:
Database connections
数据库连接
Holding a database connection open consumes resources on the database; it uses memory and databases are configured to have a maximum number of connections, so you increase to likelihood of running out of connections. Also the state of the session is maintained, so you can run into trouble with locks being accidentally held beyond their intended scope.
保持数据库连接打开会消耗数据库上的资源;它使用内存,并且数据库配置为具有最大连接数,因此您增加了连接用完的可能性。此外,会话的状态也会得到维护,因此您可能会遇到意外持有超出其预期范围的锁的麻烦。
On the positive side, prepared statements stay compiled and ready for use, so if you code and use your SQL correctly, you can gain significant performance benefits from reusing prepared statements. However, doing so may complicate your code and care is advised.
从积极的方面来说,准备好的语句保持编译状态并可以使用,因此如果您正确编码和使用 SQL,您可以通过重用准备好的语句获得显着的性能优势。但是,这样做可能会使您的代码复杂化,建议小心。
Also, obtaining a connection is quite expensive, so that's why connection poolsexist. These leave the connections open, but the client gets connections, uses them, then releases them back to the pool when done.
此外,获得连接的成本非常高,这就是连接池存在的原因。这些使连接保持打开状态,但客户端获取连接,使用它们,然后在完成后将它们释放回池中。
Result set
结果集
Holding result sets open will also hold certain locks open if you don't commit
(which closes then result set), thus depending on your application, you can quickly hit deadlocks or severe liveliness issues. Regardless of whether you hold connections open, always close your result sets as soon as possible to release as much resource back to the database as you can.
如果您不这样做,保持结果集打开也会保持某些锁打开commit
(这会关闭然后结果集),因此根据您的应用程序,您可能会很快遇到死锁或严重的活跃度问题。无论您是否保持连接打开,始终尽快关闭您的结果集,以将尽可能多的资源释放回数据库。
回答by Richard Sitze
*.close()
allows the object instance to release references to resources it may hold.
*.close()
允许对象实例释放对它可能持有的资源的引用。
In the case of a ResultSet
this could be a fairly significant number of other objects. The GC will get around to cleaning them up eventually - if you "let go of" the ResultSet
. It's a good practice to get into.
在 a 的情况下,ResultSet
这可能是相当多的其他对象。GC 最终会开始清理它们——如果你“放手”了ResultSet
. 进入是一个很好的做法。
A Connection
object will be holding resources associated with the connection. In this case the GC will never directly recover those resources (possibly indirectly if the Connection
implements a finalize
method). You want to *.close()
anything that might be holding up resources of any limited nature, so those resource can be reused.
一个Connection
对象将持有与连接相关的资源。在这种情况下,GC 永远不会直接恢复这些资源(如果Connection
实现了finalize
方法,可能会间接恢复)。您想要*.close()
任何可能占用任何有限性质的资源的东西,以便可以重用这些资源。
It's not uncommon to have some cap on the # of connections, for example. You'll have better response & less potential for failure if you release those resources as soon as you can.
例如,对连接数设置上限的情况并不少见。如果您尽快释放这些资源,您将获得更好的响应并减少失败的可能性。
In BOTHcases, closing mayimprove overall responsiveness of your code; it's a good bet to make.
在BOTH情况下,关闭可能会提高代码的总体响应; 这是一个很好的选择。
回答by Reimeus
By not closing JDBC connections you increase the likelihood of long waits for idle JDBC connections to be reaped where connection pooling is used. Closing JDBC connections will allow quicker reuse and improve performance.
通过不关闭 JDBC 连接,您会增加在使用连接池的地方长时间等待空闲 JDBC 连接被回收的可能性。关闭 JDBC 连接将允许更快地重用并提高性能。
Failure to close resultsets, can lead to some databases not freeing up cursor resources.
未能关闭结果集,可能会导致某些数据库无法释放游标资源。
回答by java geek
If you didn't close result set object, you may face out of memory error on long run of your code. I faced out of memory error when I missed closing it and directly made result set reference as null. I have used MySQL database.
如果您没有关闭结果集对象,您可能会在长时间运行代码时面临内存不足错误。当我错过关闭它并直接将结果集引用设为空时,我面临内存不足错误。我使用过 MySQL 数据库。