我遇到的情况是我使用的第三方开源产品在Oracle中没有游标并且收到错误:java.sql.SQLException:ORA-01000:超出最大打开游标数我的最大游标设置为1000,我试图弄清楚达到此限制的代码是否正在执行错误操作,或者我是...

我遇到的情况是我使用的第三方开源产品在Oracle中没有游标并且收到错误:java.sql.SQLException:ORA-01000:超出最大打开游标数
我的最大游标设置为1000,我试图弄清楚达到此限制的代码是否正在执行错误操作,或者我是否只需要增加限制.
经过一些调查后,我在代码中找到了一个创建ResultSet的点,从而使我的打开游标数增加1.但是,该ResultSet在使用后很快就会关闭….但是光标数仍保持原样.我能够在第三方开源项目之外的简单JDBC应用程序中重现逻辑.
package gov.nyc.doitt.cursor;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class CursorTest {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@myhost:1537:mydb", "username", "password");
// as expected: there are 0 cursors associated with my session at this point
ps = conn.prepareStatement("select my_column from my_table where my_id = ?");
ps.setInt(1, 86);
// as expected: there are 0 cursors associated with my session at this point
rs = ps.executeQuery(); // opens 1 cursor
// as expected: there is 1 open cursor associated with my session at this point
} catch (Throwable t) {
t.printStackTrace();
} finally {
// as expected: there is 1 open cursor associated with my session at this point
try {
rs.close();
} catch (SQLException e) {
System.err.println("Unable to close rs");
}
// not expected: there is still 1 open cursor associated with my session at this point
try {
ps.close();
} catch (SQLException e) {
System.err.println("Unable to close simplePs");
}
// not expected: there is still 1 open cursor associated with my session at this point
try {
conn.close();
} catch (SQLException e) {
System.err.println("Unable to close conn");
}
// as expected: at this point my session is dead and so are all the associated cursors
}
}
}
我发现一些Oracle文档让我觉得如果你关闭了我们的ResultSet和PreparedStatements就会关闭所有打开的游标,但是我的开放游标似乎在闲逛.请参阅此常见问题解答(http://download.oracle.com/docs/cd/B10501_01/java.920/a96654/basic.htm#1006509),其中说明“关闭结果集或语句会在数据库中释放相应的游标”.只是基于我的测试似乎没有发生,所以我必须缺乏一些基本的理解.
任何人都可以解释Oracle如何处理游标或指向一些可以启发我的文档吗?
谢谢!
解决方法:
很久以前我遇到过类似的问题.据我记忆,问题在于延迟垃圾收集.在垃圾收集器找到并释放适当的对象之前,数据库游标不会关闭.如果经常创建语句,则可能会遇到此问题.尝试不时手动调用垃圾收集器:
Runtime r = Runtime.getRuntime();
r.gc();
只是为了检查这个假设.
本文标题为:java – 使用jdbc澄清oracle中的游标


基础教程推荐
- SpringBoot读取自定义配置文件方式(properties,yaml) 2023-02-26
- SpringBoot @RestControllerAdvice注解对返回值统一封装的处理方法 2023-06-01
- SpringBoot实现在webapp下直接访问html,jsp 2023-06-30
- 详谈StringUtils3之StringUtils.isEmpty()和StringUtils.isB的区别 2023-02-10
- JSP 中response.setContentType()的作用及参数 2023-07-31
- SpringMVC中请求参数的获取方式 2022-11-28
- mybatis中使用InsertProvider注解报错解决全过程 2023-02-18
- SpringBoot整合Quartz实现定时任务详解 2023-04-06
- Java设计模式之状态模式State Pattern详解 2023-07-01
- 基于Process#waitFor()阻塞问题的解决 2023-08-07