oracle for update和for update nowait的区别

2025-12-05 00:03:28
推荐回答(3个)
回答1:

SQL> create table t(id int);

Table created.

SQL> insert into t values(1);

1 row created.

SQL> commit;

Commit complete.

SQL> select * from t;

ID
----------
1

SQL> update t set id=10 where id=1;

1 row updated.

此处不提交 另开两个session

session 1:
SQL> select * from t for update;

会一直处于等待状态 等待上面的update 提交或回滚

session 2:
SQL> select * from t for update nowait;
select * from t for update nowait
*
ERROR at line 1:
ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired

加nowait 后会直接返回错误不会一直等待

回答2:

指对数据加锁时是否等待;
如果数据已锁,则前者会等待直到可以锁定,而后者则直接返回并提示错误信息;
如果数据未锁,则两者效果一样。

回答3:

for update nowait直接返回拒绝,