JDBC 垃圾收集
如果我不关闭结果集或准备好的语句会发生什么。
它们会被垃圾收集器关闭并释放吗?
我要求函数内的局部变量。
你知道这方面的任何文件吗?
What happens if i don't close resultset or preparedstatements.
Will they be closed and released by the garbage collector.
I'm asking this for local variables inside a function.
Do you know any documentation about this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您的代码在使用完毕后没有关闭
ResultSet
或PreparedStatement
,您的应用程序将占用数据库中的稀缺资源(例如游标)。例如,请参阅:垃圾收集器不知道有关关闭
ResultSet
或PreparedStatement
的任何信息,因此GC 不会自动为你处理这个问题。会怎样? Java 7 的try
-with-resources 语句< /a>!If your code does not close
ResultSet
s orPreparedStatement
s when done using then, your application will hog scarce resources — like cursors — in the database. See, for example:The garbage collector does not know anything about closing
ResultSet
s orPreparedStatement
s, so GC won't automagically take care of that for you. What will? Java 7'stry
-with-resources statement!我不是这方面的专家,但我不认为 GC 可以收集它,因为 PS 与数据库连接相关联,因此不会被垃圾收集。
你可以在这里看看:
http:// /www.theserverside.com/news/1365244/Why-Prepared-Statements-are-important-and-how-to-use-them-properly
I am not an expert here, but I don't think GC can collect that as PSs are associated with database connections, so that won't get garbage collected.
Yo can have a look here:
http://www.theserverside.com/news/1365244/Why-Prepared-Statements-are-important-and-how-to-use-them-properly
使用完 ResultSet 和 Statement 对象后,必须显式关闭它们。这适用于您在使用 JDBC 驱动程序时创建的所有 ResultSet 和 Statement 对象。驱动程序没有终结器方法;清理例程由 ResultSet 和 Statement 类的 close() 方法执行。如果不显式关闭 ResultSet 和 Statement 对象,可能会发生严重的内存泄漏。您还可能用完数据库中的游标。关闭结果集和语句都会释放数据库中相应的游标;如果仅关闭结果集,则不会释放游标
You must explicitly close the ResultSet and Statement objects after you finish using them. This applies to all ResultSet and Statement objects you create when using the JDBC drivers. The drivers do not have finalizer methods; cleanup routines are performed by the close() method of the ResultSet and Statement classes. If you do not explicitly close your ResultSet and Statement objects, serious memory leaks could occur. You could also run out of cursors in the database. Closing both the result set and the statement releases the corresponding cursor in the database; if you close only the result set, the cursor is not released
resultset
和preparedstatment
通过显式调用close
方法关闭。垃圾收集器不会关闭这些。如果你不调用close
,那么oracle游标不会在oracle端释放。通常,在以下情况下,对象将有资格在 Java 中进行垃圾收集:
在方法内创建的 ResultSet 对象,未关闭且引用超出范围,一旦控件退出该方法。,则引用将设置为 null,并且对象有资格进行垃圾回收。我确实说过不保证合格。底层的oracle游标仍然存在于数据库中。因为你没有调用close。
resultset
andpreparedstatment
are closed ,by explicitly callingclose
method. Garbage collector will not close these. I you do not callclose
, then the oracle cursor is not released at the oracle end.Generally an object becomes eligible for garbage collection in Java on following cases:
ResultSet Object created inside a method, not closed and reference goes out scope ,once control exit that method . , then the reference is set to null and object is eligible for garbage collection. I did say eligible not guaranteed.The underlying oracle cursor is still there in the database.because u did not call close.
->如果我不关闭结果集或准备好的语句会发生什么?它们会被垃圾收集器关闭并释放吗?
这样就会降低数据库和应用程序的性能。您必须正确关闭或处置 JDBC 资源。
关闭或处置(JDBC 资源)对象意味着这些对象现在可用于垃圾回收,并且 GC 将释放已获取的任何 JDBC 资源。
对于
ResultSet
对象,当调用Statement.close()
方法时,它将自动关闭。如果您想显式关闭ResultSet
对象,您可以调用 ResultSet.close() 方法(阅读文章 - 5.1.20 关闭 ResultSet 对象)。查看文章 - 最佳实践:关闭和释放 JDBC 资源 和 Java SE 7 和 JDBC 4.1 中的增强(本文文本 - 功能:使用 try-with-resources 语句自动关闭 Connection、ResultSet 和 Statement 类型资源的能力)
-> What happens if i don't close resultset or preparedstatements? Will they be closed and released by the garbage collector?.
That way you are degrading the database and application performance. You must have to close or dispose JDBC resource properly.
To
close or dispose
(JDBC resource) objects means these objects are now available for garbage collection and GC will releases anyJDBC
resources which had been acquired.In case of
ResultSet
object, it will be closed automatically whenStatement.close()
method is called. You may call ResultSet.close() method if you want to closeResultSet
object explicitly (Read Article - 5.1.20 Closing a ResultSet Object).Have a look at articles - Best Practice: Closing and releasing JDBC resources and Enhancements in Java SE 7 and JDBC 4.1 (Text from this article - Feature: The ability to use a try-with-resources statement to automatically close resources of type Connection, ResultSet, and Statement)