1

我有一个在生产中运行的作业,它随机异常并出现错误 ORA 54。当我检查代码时,我可以看到NOWAIT导致问题的原因是 。现在我决定对其进行测试,并编写了一个匿名块,如下所示。

declare
    cursor c1 is
        select * from table where column_1=2 and column_2=2 and column_3=6
        for update of column_4 nowait;
    record_locked exception;
    pragma exception_init (record_locked, -54);
begin
    begin
        open c1;
    exception
        when record_locked then
            dbms_output.put_line('Faced a locked record. Waiting for 2 minutes..');
            dbms_lock.sleep(120);
            open c1;
    end;
exception
    when others then
        dbms_output.put_line('Exception Occured '||SQLCODE||SQLERRM);
end;

我打开了一个会话并运行了以下查询

select * from table
where column_1=2 and column_2=2 and column_3=6
for update of column_4 nowait;

我没有提交或回滚并保持会话打开。现在我在另一个会话中运行了上面的匿名块。等待 2 分钟后,它因 ORA 54 错误而失败。所以我相信我的假设是正确的。

现在的问题是,当我以相同的方式在测试环境中运行包含第一个匿名块的整个作业代码时,它等待锁定记录而没有异常结束。当我通过回滚释放锁时,它更新了记录并成功完成。

我想知道为什么?

4

1 回答 1

1

您从测试和生产中得到不同的结果,因为您的表格内容不同。您的测试表不包含任何与 where 子句匹配的行,因此您的会话不会相互阻塞。

尝试在测试表中添加至少一行与标准匹配的行,您应该得到与生产中相同的结果。

于 2013-04-08T09:57:48.127 回答