JDBC 结果集未关闭。结果?
HI:我们有一些纯 JDBC 准备好的语句,在执行查询后,我们处理结果集。但尽管我们关闭了声明,但我们从未关闭结果集。当一次又一次地执行这个preparedstatement时,例如一百万次,会发生内存泄漏吗?
HI: We have some pure JDBC preparedstatement, after it execute query, we process resultset. But we never close resultset though we closed statement. When execute this preparedstatement again and again, million times for example, would be memory leak occured?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关闭语句应该允许对结果集进行垃圾收集。但是,关闭该语句将隐式关闭结果集,那么如何继续使用它呢?整个事情是否包装在 try-catch-finally 块中,该块将finally 块中的语句和结果集设置为 null?
否则,如果抛出异常,您可能会破坏连接,并且语句和结果集可能无法连接。也就是说,只要您保持快乐的一天场景,一切都会好起来的,但如果出现问题,则可能会导致一切崩溃,因为缺少将finally块重置为null可能会阻止垃圾收集并耗尽您的所有资源(例如连接)。
Closing the statement should allow the resultset to get garbage collected. however, closing the statement will implicitly close the resultset, so how can you continue to use it? Is the whole thing wrapped in a try-catch-finally block, which sets the statement and resultset to null in the finally block?
Otherwise if you throw an exception you may chew up connections, and the statement and resultset may not get connected. I.e. as long as you stay with the happy day scenario everything will be okay, but it could bring everything down if one thing goes wrong as the lack of the finally block resetting to null may prevent garbage collection and chew up all your resources (e.g. connections).
理论上,关闭Statement应该关闭ResultSet。但现实世界的实践是另一回事。
是的,有理由显式关闭 ResultSet 对象(或使用新的 try-with-resources 语法来关闭它们)。有关详细信息,请参阅此答案。 JDBC 驱动程序、连接池和数据库中常见的设计限制和错误可能会导致问题。
In theory, closing the Statement should close the ResultSet. But real-world practice is another matter.
Yes there are reasons to explicitly close your ResultSet objects (or use the new try-with-resources syntax to close them). See this answer for more info. Design limitations and bugs all too commonly found in JDBC drivers, connection pools, and database can cause problems.