有没有办法在Java中找出数据库游标是否打开?
JDBC中有没有办法找出JAVA中打开或未打开的游标。 当尝试访问未打开的游标时,我们遇到异常。 java.sql.SQLException:游标已关闭。
这就是正在发生的事情。
存储过程就是这样设计的。该过程返回光标和其他几个列。在某些情况下,数据库人员不会打开游标,而仅返回游标之外的其他剩余列。因此,当 Java 代码尝试获取光标时,它会抛出此异常。我知道,我可以捕获异常并进一步处理剩余的列。但我想知道是否有不同的(可能更好)的方法来处理这个问题? 或者只需要在存储过程中打开游标,即使没有任何内容作为游标的一部分返回?
Is there a way in JDBC to find out the cursor opened or not in JAVA.
We are getting an exception when tried to access an un opened cursor.
java.sql.SQLException: Cursor is closed.
Here is what is happening.
The Stored proc is designed this way. the proc returns the cursor and a couple of other columns. For some conditions, the DB guy is not opening the cursor and returns only the other remaining column outside of the cursor. So when the Java code tries to get cursor, it throws this exception. I know, I can catch the exception and proceed further with the remaining columns. But I was wondering is there a different (may be better) way to handle this?
Or just need to open the cursor in the stored proc, even if there is nothing to return as part of the cursor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我之前在Oracle中也遇到过这个问题。它与ResultSet无关,它是开放的。该错误是由 STP 引起的,STP 可能会返回空游标,或者在某些条件下关闭游标。
请检查您使用的所有 STP,并确保它始终返回打开的游标。
I encountered this issue in Oracle before. It has nothing to do with the ResultSet, it's open. The error is caused by the STP which may return an empty cursor, or close the cursor under certain conditions.
Please check all the STPs you use and make sure it always returns an open cursor.
也许可以更改存储过程以返回一个指示游标是否打开的值。类似下面的内容可能有用:
如果该过程打开游标,则应将 bCursor_is_open 设置为 TRUE;如果未打开游标,则应将 bCursor_is_open 设置为 FALSE。
Perhaps the stored proc could be changed to return a value indicating if the cursor is open. Something like the following might be useful:
The procedure should set bCursor_is_open to TRUE if it opens the cursor, or FALSE if the cursor is not opened.
这意味着您没有正确处理结果集。 (ResultSet 是游标的对象表示形式。)
不必担心如何检查游标是否打开;只需考虑如何检查游标是否打开即可。弄清楚为什么你的 ResultSet 超出了范围,你就会回答你自己的问题。
发布一些代码或更详细地解释正在发生的事情。
我的猜测是,您将 ResultSet 传递出持久层,而将其映射到对象或集合并在方法范围内关闭它会更好。返回该对象或集合而不是 ResultSet。
数据库游标是稀缺资源。您应该让它们在获取数据所需的最短时间内保持打开状态,然后关闭它们,以便其他人可以使用它们。它将有助于提高可扩展性 - 并作为附带好处解决此异常。
This means you're not handling your ResultSet properly. (ResultSet is the object representation of the cursor.)
Don't worry about figuring out how to check if a cursor is open or not; figure out why your ResultSet has gone out of scope and you'll answer your own question.
Post some code or a more detailed explanation of what's going on.
My guess is that you're passing a ResultSet out of the persistence tier when you'd be a lot better off mapping it into an object or collection and closing it within method scope. Return that object or collection instead of a ResultSet.
Database cursors are scarce resources. You should keep them open for the minimum time required to get your data and then close them so someone else can use them. It'll help with scalability - and solve this exception as a side benefit.
每当您尝试访问 ResultSet、其语句或连接已关闭时,您都会收到此异常。请注意,重复使用语句打开其他 ResultSet 也会导致所有先前打开的结果集被关闭。
检查 JDBC 代码中的流程。确保它遵循 Sun 教程。要了解如何正确使用基本 JDBC 的更多信息,您可以找到这个文章也很有用。
You will get this exception whenever you attempt to access a
ResultSet
while it, its statement or its connection has been closed. Note that reusing a statement to open otherResultSet
s will also cause all previously opened resultsets being closed.Review the flow in your JDBC code. Ensure that it follows the standard JDBC idioms as outlined in the Sun tutorial. To learn more how to use basic JDBC properly, you may find this article useful as well.
从 1.6 开始,ResultSet 有一个 isClosed 方法。我假设您的异常发生在结果集上。话虽这么说,我同意其他答案,如果您看到 ResultSet 从您下方关闭,则您的使用可能存在缺陷,并且仅测试
isClosed
不会对您有帮助摆脱这个问题——当你需要它打开时,你会发现它是关闭的。As of 1.6, ResultSet has an isClosed method. I'm assuming your exception is happening on a ResultSet. That being said, I agree with the other answers that if you are seeing your ResultSet close out from under you, you likely have a flaw in your usage and just testing for
isClosed
isn't going to help you get out of that problem - you will just find that it is closed when you need it to be open.