首页 > Linux操作系统 > Linux操作系统 > Oracle锁的总结
Oracle锁的机制是在并发操作的环境下为了保证数据的一致性和完整性而使用的一种机制.
有两种类型的锁:行级锁和表级锁.
(1)行级锁 DML操作(insert,update,delete)和select * from table for update nowait|wait N都会产生行级排它锁(ROW_X),注意用select * from table for update 这条形式来获得行锁的时候,不要忘了加上nowait,以避免相关行已被别的会话锁了后无限等待,要不然加上wait N等待N秒后自动退出,但要注意的是:如果是一个会话对表实现了锁定,那就算是加上wait N过了N秒也不会自动退出.
我是这样测试的:
session1:
SQL> select * from test_connect where id=1 for update;
ID NAME
---------- ------------------------------
1
session 2:
SQL> select * from test_connect where id=1 for update wait 1;
select * from test_connect where id=1 for update wait 1
ORA-30006: resource busy; acquire with WAIT timeout expired
过了1秒自动退出.
session1:
SQL> rollback;
Rollback complete
SQL> lock table test_connect in share mode;
Table(s) locked
session2:
SQL> select * from test_connect where id=1 for update wait 1;
这样session2就无限等待,所以为了避免无限等待,最好是加上nowait.
(2) 表级锁 表级锁分为两种:表级共享锁和表级排它锁.
多个会话可以同时对一个表加上表级共享表,这样各个会话就互相限制,除了可以进行查询外,其他对表的任何操作都不允许.
以下是测试:
session1:
SQL> lock table test_connect in share mode;
Table(s) locked
session 2:
SQL> lock table test_connect in share mode;
Table(s) locked
SQL> select * from test_connect where id=1 for update nowait;
select * from test_connect where id=1 for update nowait
ORA-00054: resource busy and acquire with NOWAIT specified
表级排它锁是级别最高的锁,只要一个会话对表加上了表级排它锁lock table test_connect in exclusive mode,其他会话除了能对此表作查询外,任何操作都不允许.
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/13387766/viewspace-489137/ ,如需转载,请注明出处,否则将追究法律责任。