记录一下今天遇到的一个Oracle 错误 :ORA-00604: 递归 SQL 级别 1 出现错误,ORA-01000: 超出打开游标的最大数
医院运行的接口,最近频繁出现这个错误,一开始以为是游标最大数不够大经过以下SQL查询结果为 300,第一想到的就是修改最游标最大数
方式一
从数据库游标最大数修改入手,不过这个方法治标不治本。
--结果 300
select value from v$parameter where name = 'open_cursors'
--加大游标数
alter system set open_cursors=2000 scope=both;
方式二
从程序代码入手查找问题,发现有一段循环处理如下:
问题就出在 dbhelper.ExecQuery(sql_fee_num, ref resultList_fee_num)
当resultList.Tables[0].Rows.Count 循环数比较小时,代码运行正常。当循环数超过一定值(数据库当前游标最大数:300),代码就会出错。提示:ORA-01000: 超出打开游标的最大数
这是因为Oracle数据库中打开的游标最大数为一定值,例如300,当代码中dbhelper.ExecQuery函数执行时, 循环中一个Command占用了一个数据库游标,执行的循环超过这个数时就会产生游标数目溢出错误。
//取各值:
for (int i = 0; i < resultList.Tables[0].Rows.Count; i++)
{
ls_label = resultList.Tables[0].Rows[i][0].ToString();
ls_ctype = resultList.Tables[0].Rows[i][1].ToString();
ls_doctor = resultList.Tables[0].Rows[i][2].ToString();
ls_doctor_id = resultList.Tables[0].Rows[i][3].ToString();
//
string sql_fee_num = @"这里是一段SQL";
//问题出现在这里 dbhelper.ExecQuery(sql_fee_num, ref resultList_fee_num)
if (dbhelper.ExecQuery(sql_fee_num, ref resultList_fee_num) == 0)
{
for (int j = 0; j < resultList_fee_num.Tables[0].Rows.Count; j++)
{
ls_fee = resultList_fee_num.Tables[0].Rows[j][0].ToString();
ls_reg = resultList_fee_num.Tables[0].Rows[j][1].ToString();
}
}
}
解决办法:
在ExecQuery(sql_str,ref ds)函数中每次执行完OracleCommand,都将command.dispose()下,释放掉这个资源就好了
try
{
//command 处理
}
catch (Exception)
{
//异常处理
throw;
}
finally
{
command.Dispose();
}