for update 和 for update nowait的区别

2025-12-25 11:53:10
推荐回答(1个)
回答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 后会直接返回错误不会一直等待