1、查看ORACLE最大游标数
C:/Documents and Settings/Administrator>sqlplus "sys/admin@test151 as sysdba" (sys以dba登录test151服务)
SQL*Plus: Release 9.2.0.1.0 - Production on 星期四 11月 5 09:08:04 2009
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
连接到:
Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.1.0 - Production
SQL> show parameter open_cursors;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
open_cursors integer 300
2、查看当前打开的游标数目
SQL> select count(*) from v$open_cursor;
COUNT(*)
----------
17494
3、修改ORACLE最大游标数
SQL> alter system set open_cursors=1000 scope=both;
系统已更改。
SQL> show parameter open_cursors;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
open_cursors integer 1000
关于 ORA-01000: maximum open cursors exceeded 这个问题的相关东西,我搜了一些文章贴一下。
ORA-01000 maximum open cursors exceeded
Cause: A host language program attempted to open too many cursors. The initialization parameter OPEN_CURSORS determines the maximum number of cursors per user.
Action: Modify the program to use fewer cursors. If this error occurs often, shut down Oracle, increase the value of OPEN_CURSORS, and then restart Oracle.
关于cursor 的参数有这么几个:
SQL> show parameter cursor
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
cursor_sharing string EXACT
cursor_space_for_time boolean FALSE
open_cursors integer 800
session_cached_cursors integer 0
这里,cursor_sharing跟open_cursors的数量没有关系。open_cursors的数量,它包括了oracle服务器端session_cached_cursors的数量,以及应用服务器端cursor cache size的数量。
关于session_cached_cursors这个参数,可以看 汪海 写的2篇文章:
v$open_cursor与session_cached_cursor
session_cached_cursors,cursor_space_for_time,gets,pin
这个问题的根源,除了上面3个参数,主要在于程序的问题,在itpub 上我们可以看到:
很多朋友在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只需要设置一个比较小的值,就足够使用了,除非有非常特别的要求。