Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1208636
  • 博文数量: 350
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 5668
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-23 17:53
文章分类

全部博文(350)

文章存档

2013年(350)

分类: Oracle

2013-04-24 16:34:34

今天遇到了一个之前从未在意过的问题引发的错误,赶紧记录一下。

两个过程,此处命名为proc1,proc2,用于更新相关表中的记录。
proc1对表记录做初始化,proc2按照业务逻辑修改表中的字段值,过程中均没有显式提交。

两个过程单独执行均不会报错,但同时执行就会出错。错误号:ORA-01002: fetch out of sequence

首先查看官方文档对ORA-01002错误号的解释:
Cause: This error means that a fetch has been attempted from a cursor which is no longer valid. Note that a PL/SQL cursor loop implicitly does fetches, and thus may also cause this error. There are a number of possible causes for this error, including: 1) Fetching from a cursor after the last row has been retrieved and the ORA-1403 error returned. 2) If the cursor has been opened with the FOR UPDATE clause, fetching after a COMMIT has been issued will return the error. 3) Rebinding any placeholders in the  statement, then issuing a fetch before reexecuting the statement.
Action: 1) Do not issue a fetch statement after the last row has been retrieved - there are no more rows to fetch. 2) Do not issue a COMMIT inside a fetch loop for a cursor that has been opened FOR UPDATE. 3) Reexecute the statement after rebinding, then attempt to fetch again.


由于proc1非常简单,就是几个update语句,proc2隐式声明了cursor循环更新,逻辑比较复杂,因此初步分析问题极可能是出在proc2的逻辑处理上,重点关注proc2。
由于pl/sql并没有提供单步调试的功能,为了清晰每步执行的状况,在相关重要位置通过dbms_output打印更新行ID等,并在适当位置加上exception捕获可能的错误。

重新单独执行proc2确实捕获到了几个错误,都是:ORA-01401: inserted value too large for column

根据打印输出的行号到原始表中查询了一下,确实是字段超长,重新看了看proc2的执行逻辑,由于其中对于更新操作有exception做处理,如果更新出错会继续跳到下一条,所以此处即使出错应该没关系,过程仍然会继续向下执行,这个错误应该不是造成ora-01002的主要原因。

手动rollback,然后重新同时执行proc1,proc2,这次出错,但这次只报了一次ORA-01401,接着就是ORA-01002,看起来仿佛又跟ora-01401有关系了,重点看看出错地方的代码,发现exception之后,执行了一个rollback,这个,不管是从业务逻辑,还是从程序逻辑上显然都是不合理的,去掉它之后再执行,成功!

再回过头来看一看,虽然proc2中ora-1401并非造成ora-01002的主因,但如果proc2执行过程中不报错,那么自然也不会执行rollback,造成事务回滚,从而触发ora-1002错误,这也解释了为什么之前都执行的好好的,没有做任何改动的情况下忽然出现了错误。如果proc2一直正确执行的话,这个错误肯定就不会暴露出来。问题的主要原因还是因为在proc2执行出错时,执行了rollback,因此将rollback注释掉,重新编译proc2,再次执行,一切正常,问题解决!


阅读(6728) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~