Oracle:更新语句无等待/无失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5545782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Oracle: no wait / no fail on update statement
提问by Synesso
I have many competing update statements in a multi-application environment. With the current design, deadlocks are frequent.
我在多应用程序环境中有许多相互竞争的更新语句。在当前的设计中,死锁经常发生。
All but one of the updates can be skipped if necessary and updated at the next interval.
如有必要,可以跳过除一个更新之外的所有更新,并在下一个时间间隔更新。
Can I apply NOWAIT to the update statement? Can I have my statement silently fail when it can't obtain a lock? Can I specify the length time it attempts to get a lock?
我可以将 NOWAIT 应用于更新语句吗?当我的语句无法获得锁时,我可以让我的语句无声地失败吗?我可以指定它尝试获得锁定的时间长度吗?
回答by DCookie
No you cannot specify NOWAIT on the update statement - the NOWAIT must be specified on the SELECT FOR UPDATE statement.
不,您不能在更新语句上指定 NOWAIT - 必须在 SELECT FOR UPDATE 语句上指定 NOWAIT。
Yes, you can silently fail a select for update - If you specify NOWAIT and handle the exception that is generated:
是的,您可以静默地使选择更新失败 - 如果您指定 NOWAIT 并处理生成的异常:
BEGIN
SELECT x FROM table FOR UPDATE NOWAIT;
EXCEPTION
WHEN OTHERS THEN
<handle exception>
END;
Yes, a length of time can be specified to wait. In place of the NOWAIT in the above example, specify WAIT n, where nis the number of seconds to wait for the lock. If you can't get the lock in that time, it will fail again with the ORA-00054, which you can handle as before.
是的,可以指定等待的时间长度。代替上面示例中的 NOWAIT,指定 WAIT n,其中n是等待锁定的秒数。如果在那段时间内无法获得锁定,它将再次失败并显示 ORA-00054,您可以像以前一样处理。
回答by Olivier Comte
Never silently catch "others". In this case you should catch the "resource_busy exception ORA-00054".
永远不要默默地抓住“别人”。在这种情况下,您应该捕获“resource_busy 异常 ORA-00054”。
declare
resource_busy exception;
pragma exception_init(resource_busy,-54);
begin
select x into z from table for update nowait;
exception
when resource_busy
then
--Do something
end;