java 除了resultset_Java:ResultSet关闭策略,除了最终关闭它

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

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

while (someCondition) {

rs1=executePreparedStatementNew(query1,param1,"");

//do something with rs1

rs1=executePreparedStatementNew(query2,param2,"");

}

If the loops runs 5 times, how many cursor will be opened by this code ?

如果循环运行5次,此代码将打开多少光标?

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.

如果我最后关闭rs1,将关闭多少游标,有人会说query1的rs1实例不会因为被query2实例屏蔽而关闭。

Does resultsets really gets masked this way , if so, how to ensure that all the instances are closed.

结果集是否真的以这种方式被屏蔽,如果是这样,如何确保所有实例都被关闭。

Appreciate any help.

感谢任何帮助。

1 个解决方案

#1

6

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.

你还没有说过你的finally块在哪里,但是如果它在while循环之外,那么是的,你将有未闭合的结果集。 rs1变量将引用所提取的“最新”结果集 - 因此这是唯一将被关闭的结果集。这里没有什么神奇的东西 - 它只是变量的正常行为。

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.

我建议你将每个“获取结果集并使用它”的情况分成它自己的方法,并在该方法中的try / finally块中关闭结果集。这将使相当清楚的是发生了什么。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值