Java:ResultSet关闭策略,除了finally关闭之外

发布于 2024-08-17 08:46:36 字数 436 浏览 11 评论 0原文

我面临 ORA-01000: 超出最大打开游标数,尽管我正在finally 块中关闭结果集。但我怀疑我的遗留代码有一些问题,下面是我的伪代码

while (someCondition) {
rs1=executePreparedStatementNew(query1,param1,""); 
//do something with rs1
rs1=executePreparedStatementNew(query2,param2,""); 
}
  1. 如果循环运行 5 次,这段代码将打开多少个光标?
  2. 如果我在finally中关闭rs1,将会关闭多少个游标,有人说query1的rs1实例不会被关闭,因为它被query2实例屏蔽了。
  3. 结果集真的会以这种方式被屏蔽吗?如果是,如何确保所有实例都关闭。

感谢任何帮助。

I am facing ORA-01000: maximum open cursors exceeded although I am closing the resultsets in finally block. But I suspect there is some trouble with my legacy code, below is my pseudo-code

while (someCondition) {
rs1=executePreparedStatementNew(query1,param1,""); 
//do something with rs1
rs1=executePreparedStatementNew(query2,param2,""); 
}
  1. If the loops runs 5 times, how many cursor will be opened by this code ?
  2. If I close rs1 in finally, how many cursors will be closed, some people say that rs1 instance for query1 will not be closed as it is masked by query2 instance.
  3. Does resultsets really gets masked this way , if so, how to ensure that all the instances are closed.

Appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

妞丶爷亲个 2024-08-24 08:46:36

你还没有说你的finally块在哪里,但如果它在while循环之外,那么是的,你将拥有未封闭的结果集。 rs1 变量将引用获取的“最新”结果集 - 因此这是唯一一个将被关闭的结果集。这里没有什么神奇的事情发生——这只是变量的正常行为。

我建议您将每个“获取结果集并使用它”的情况分离到其自己的方法中,并在该方法内的 try/finally 块中关闭结果集。这将使发生的事情变得相当清楚。

You haven't said where your finally block is, but if it's outside the while loop, then yes you will have unclosed result sets. The rs1 variable will refer to the "latest" result set fetched - so that's the only one which will be closed. There's nothing magical going on here - it's just the normal behaviour of variables.

I would suggest that you separate each "fetch result set and use it" case into its own method, and close the result set in a try/finally block within that method. That will make it fairly clear what's going on.

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