Oracle 行级锁与表级锁

一、行级锁:
(下面这句将给该表的所有行都加上锁)
select * from person for update;

如果该行记录已经被锁定,就不用等待,系统会直接抛错 ora-00054
select * from person  where id = '1'  for update nowait

如果该行记录已经被锁定,更新的时候等待5秒,如果这5秒内,该行记录被解锁,那么返回查询结果,如果5秒内仍未解锁,那么系统会直接抛错 ora-00054
select * from person for update wait 5;

另外,如果使用 select * from person  where id = '1'  for update ,当该行记录已经被锁定时,那么系统将一直等待该行记录被释放后,再加锁。

二、表级锁:

行共享:允许用户进行任何操作,禁止排他锁
lock table person in row share mode;

行排他:允许用户进行任何操作,禁止共享锁
lock table person in row exclusive mode;

共享锁:其他用户只能看,不能修改
lock table person in share mode;

共享行排他:比共享锁有更多限制
lock table person in share row exclusive mode;

排他锁:其他用户只能看,不能修改,不能加其他锁
lock table person in exclusive mode;

对于通过lock table命令主动添加的锁定来说,如果要释放它们,只需要发出rollback命令即可。

相关推荐