JDBC 垃圾收集

发布于 2024-12-23 06:16:00 字数 97 浏览 1 评论 0原文

如果我不关闭结果集或准备好的语句会发生什么。

它们会被垃圾收集器关闭并释放吗?

我要求函数内的局部变量。

你知道这方面的任何文件吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

乙白 2024-12-30 06:16:00

如果您的代码在使用完毕后没有关闭 ResultSetPreparedStatement,您的应用程序将占用数据库中的稀缺资源(例如游标)。例如,请参阅:

垃圾收集器不知道有关关闭 ResultSetPreparedStatement 的任何信息,因此GC 不会自动为你处理这个问题。会怎样? Java 7 的 try-with-resources 语句< /a>!

If your code does not close ResultSets or PreparedStatements 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 ResultSets or PreparedStatements, so GC won't automagically take care of that for you. What will? Java 7's try-with-resources statement!

小巷里的女流氓 2024-12-30 06:16:00

我不是这方面的专家,但我不认为 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

吾性傲以野 2024-12-30 06:16:00

使用完 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

梦太阳 2024-12-30 06:16:00

如果我不关闭结果集或准备好的语句。它们会被关闭吗
并由垃圾收集器释放。

resultsetpreparedstatment 通过显式调用 close 方法关闭。垃圾收集器不会关闭这些。如果你不调用 close ,那么oracle游标不会在oracle端释放。

Will they be released by the garbage collector.

通常,在以下情况下,对象将有资格在 Java 中进行垃圾收集:

  • 该对象的所有引用显式设置为 null,例如 object = null
  • 对象在块内创建,一旦控制退出该块,引用就会超出范围。
  • 父对象设置为 null,如果一个对象持有另一个对象的引用,并且当您将容器对象的引用设置为 null 时,子对象或包含的对象将自动符合垃圾回收的条件。
  • 如果一个对象仅通过 WeakHashMap 具有实时引用,则它将有资格进行垃圾回收。

对于你的问题:我要问的是a中的局部变量
功能。

在方法内创建的 ResultSet 对象,未关闭且引用超出范围,一旦控件退出该方法。,则引用将设置为 null,并且对象有资格进行垃圾回收。我确实说过不保证合格。底层的oracle游标仍然存在于数据库中。因为你没有调用close。

if i don't close result set or prepared statements.Will they be closed
and released by the garbage collector.

resultset and preparedstatment are closed ,by explicitly calling close method. Garbage collector will not close these. I you do not call close , then the oracle cursor is not released at the oracle end.

Will they be released by the garbage collector.

Generally an object becomes eligible for garbage collection in Java on following cases:

  • All references of that object explicitly set to null e.g. object = null
  • Object is created inside a block and reference goes out scope once control exit that block.
  • Parent object set to null, if an object holds reference of another object and when you set container object's reference null, child or contained object automatically becomes eligible for garbage collection.
  • If an object has only live references via WeakHashMap it will be eligible for garbage collection.

for your question : I'm asking this for local variables inside a
function.

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.

瑕疵 2024-12-30 06:16:00

->如果我不关闭结果集或准备好的语句会发生什么?它们会被垃圾收集器关闭并释放吗?

这样就会降低数据库和应用程序的性能。您必须正确关闭或处置 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 any JDBC resources which had been acquired.

In case of ResultSet object, it will be closed automatically when Statement.close() method is called. You may call ResultSet.close() method if you want to close ResultSet 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文