关ORACLE Cursor的问题(ORA-01000 maximum open cursors exceeded)的思考
来源:互联网 发布:js实现a标签跳转 编辑:程序博客网 时间:2024/06/05 20:47
在java应用中,我们会经常用oracle jdbc.在开发中我们有时候会遇到(ORA-01000: maximum open cursors exceeded)的错误。在ITPUB上juant曾发过一篇帖子:http://www.itpub.net/showthread.php?s=&threadid=122832&perpage=15&pagenumber=1
[TIP]关于Java开发中使用Oracle数据库的一点注意事项(原创)
很多朋友在Java开发中,使用Oracle数据库的时候,经常会碰到有ORA-01000: maximum open cursors exceeded.的错误。
实际上,这个错误的原因,主要还是代码问题引起的。
ora-01000: maximum open cursors exceeded.
表示已经达到一个进程打开的最大游标数。
这样的错误很容易出现在Java代码中的主要原因是:Java代码在执行conn.createStatement()和conn.prepareStatement()的时候,实际上都是相当与在数据库中打开了一个cursor。尤其是,如果你的createStatement和prepareStatement是在一个循环里面的话,就会非常容易出现这个问题。因为游标一直在不停的打开,而且没有关闭。
一般来说,我们在写Java代码的时候,createStatement和prepareStatement都应该要放在循环外面,而且使用了这些Statment后,及时关闭。最好是在执行了一次executeQuery、executeUpdate等之后,如果不需要使用结果集(ResultSet)的数据,就马上将Statment关闭。
对于出现ORA-01000错误这种情况,单纯的加大open_cursors并不是好办法,那只是治标不治本。实际上,代码中的隐患并没有解除。
而且,绝大部分情况下,open_cursors只需要设置一个比较小的值,就足够使用了,除非有非常特别的要求。
对这个问题专门研究了一下,并写了测试程序,首先说一些测试环境,我们的数据库最大可打开的cursor设置为600,操作的数据库表的记录数为30条记录。为了模拟同时打开600cursor,也就是说有600个statement在运行,必须用到java的多线程。下面是测试程序:
public class StatementTest extends Thread{
private Connection conn;
public StatementTest(Connection conn) {
this.conn = conn;
start();
}
public void run(){
try {
String strSQL = "SELECT * FROM TestTable"
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
int i = 0;
while(rs.next()){
System.out.println("----"+i+"------");
i = i+1;
}
rs.close();
stmt.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void main(String args[]){
try{
Connection conn = DBConnection.getConnection();
for(int i = 0;i < 800;i++){
new StatementTest(conn);
}
}catch(Exception e){
e.printStackTrace();
}
}
}
运行这个程序,很快就会抛出ORA-01000: maximum open cursors exceeded的错误。
这次最主要的不是测试上面的问题,而是下面的问题:
1. 我们通过JDBC查询,是否一次是把所有的结果集查询出来后放到ResultSet中,
2. 在调用ResultSet.next()方法的时候,是否还会和数据库交互.
其实在以前我一直认为当statement执行完sql语句后,它会把结果集保存到ResultSet中,然后关闭这个cursor,当实际上并不是这样的。我们把上面的程序修改一下就可以测试这个问题了。程序如下
public class StatementTest extends Thread{
private Connection conn;
public StatementTest(Connection conn) {
this.conn = conn;
start();
}
public void run(){
try {
String strSQL = "SELECT * FROM TestTable"
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
int i = 0;
while(rs.next()){
System.out.println("----"+i+"------");
i = i+1;
Thread.sleep(5000);
}
rs.close();
System.out.println("resultset has closed");
Thread.sleep(10000);
stmt.close();
System.out.println("resultset has closed");
Thread.sleep(10000);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void main(String args[]){
try{
Connection conn = DBConnection.getConnection();
new StatementTest(conn);
}catch(Exception e){
e.printStackTrace();
}
}
}
当statement执行完SQL语句的时候,循环resultset的时候让线程sleep5秒钟,这样做的目的是为了有时间从数据库那方面查询这个cursor是否关闭.运行程序,在java控制台中出现System.out.println("----"+i+"------")循环信息的时候,在sqlplus中运行下面SQL语句,其中SID是程序运行对应数据库的进程ID
select sql_text from v$open_cursor where sid = 35
你会ResultSet循环的时候,运行上面的SQL,一直有结果都是"SELECT * FROM TestTable"也就是说通过statement打开的这个cursor一直没有关。可以初步判断当statement执行完sql语句后返回给resultset的结果集不是实际的结果(结果数集据不是放到JVM内存中),可能是结果集的指针,当resultset.next()的时候再通过这个指针从oracle拿数据。
为了确认这个问题,我们选了一个记录数超过10000条记录的大表,来测试,发现在整个运行时,JVM的内存没有很大的变化。这就进一步确定了上面的判断。
小结:通过上面测试和分析,我们可以的出结论通过JDBC查询结果集不是直接放到ResultSet中,ResultSet存在的可能是结果集的指针。
我们在调用ResultSet.next()时仍然需要和数据库交互。
- 关ORACLE Cursor的问题(ORA-01000 maximum open cursors exceeded)的思考
- oracle ORA-01000: maximum open cursors exceeded问题的解决方法
- oracle ORA-01000: maximum open cursors exceeded问题的解决方法
- oracle ORA-01000: maximum open cursors exceeded问题的解决方法
- 关于ORACLE游标的问题(ORA-01000: maximum open cursors exceeded)
- 关于ORACLE游标的问题(ORA-01000: maximum open cursors exceeded)
- 关于ORACLE游标的问题(ORA-01000: maximum open cursors exceeded)
- oracle ORA-01000:maximum open cursors exceeded
- ORA-01000: maximum open cursors exceeded, 引发的JDBC, DBPool的思考
- 关于ORA-01000: maximum open cursors exceeded的注意
- ORACLE异常——ORA-01000: maximum open cursors exceeded
- ora-01000: maximum open cursors exceeded. 问题原因
- 出错"ORA-01000: maximum open cursors exceeded" 问题分析
- 关于ORA-01000: maximum open cursors exceeded" 问题分析总结
- ORA-01000: maximum open cursors exceeded.
- ora-01000:maximum open cursors exceeded ---dblink
- ORA-01000: maximum open cursors exceeded
- ORA-01000: maximum open cursors exceeded
- Web动态添加控件
- 高效的忽略大小写的字符串替换(Replace)函数(多种方法比较)
- Working with Packing Structures -----MSDN
- 如何理解数据对象的标识?
- 8259A编程
- 关ORACLE Cursor的问题(ORA-01000 maximum open cursors exceeded)的思考
- SOA是什么?
- 怎样判断一个字符是否为中文字
- 如何在 Microsoft Visual C# .NET 中实现 Microsoft Excel 自动化
- 使用Server.Transfer在两个ASPX页面间传递参数
- 通过Web Services上传和下载文件
- 什么是VoIP
- Structure Packing
- core jsf第四章