Share Table Lock(S)
Session1 session_id=35 :
dexter@STARTREK>lock table tun2_tab in SHARE MODE ;
Table(s)Locked.
Session2 session_id=160:
dexter@STARTREK>lock table tun2_tab in ROW SHARE mode ;
Table(s)Locked.
dexter@STARTREK>commit ;
Commitcomplete.
dexter@STARTREK>lock table tun2_tab in ROW EXCLUSIVE mode ;
waiting ...
锁情况:
sys@STARTREK>/
SID TY ID1 LMODE REQUEST BLOCK
------------ ---------- ---------- ---------- ----------
35 TM 76917 4 0 1
160 TM 76917 0 3 0
S 锁比较特殊,它不允许与RX|SX 也就是3级锁并发,但是允许多个S锁并发的在多个事务中持有。
例如两个sessoin同时执行下面的命令
locktable tun2_tab in SHARE MODE ;
可以看到下面的lock信息:
sys@STARTREK>/
SID TY ID1 LMODE REQUEST BLOCK
------------ ---------- ---------- ---------- ----------
35 TM 76917 4 0 0
129 TM 76917 4 0 0
但是S锁无法与SRX|SSX和X锁并发。
Share Row ExclusiveTable Lock (SRX|SSX)
Also called a share-subexclusive table lock (SSX)
Session1 session_id=35 :
dexter@STARTREK>lock table tun2_tab in SHARE ROW EXCLUSIVE MODE ;
Table(s)Locked.
Session2 session_id=129:
dexter@STARTREK>lock table tun2_tab in ROW SHARE mode ;
Table(s)Locked.
dexter@STARTREK>commit ;
Commitcomplete.
dexter@STARTREK>lock table tun2_tab in ROW EXCLUSIVE mode ;
waiting ...
锁情况:
sys@STARTREK>@lock
SID TY ID1 LMODE REQUEST BLOCK
------------ ---------- ---------- ---------- ----------
35 TM 76917 5 0 1
129 TM 76917 0 3 0
SRX|SSX锁无法与RX|SX以上的锁并发持有。
Exclusive Table Lock(X)
Session1 session_id=35 :
dexter@STARTREK>lock table tun2_tab in EXCLUSIVE MODE ;
Table(s)Locked.
Session2 session_id=129:
dexter@STARTREK>lock table tun2_tab in ROW SHARE mode ;
waiting ...
X锁无法与任何锁并发。
上面的内容笔者已经使用PLSQL模拟出来,详情见附录1
下面列举几种常用操作的加锁情况,是否会因为table lock发生阻塞,只要代入表中即可。
|
|
RS|SS |
RX|SX |
S |
SRX|SSX |
X |
|
RS|SS |
√ |
√ |
√ |
√ |
× |
|
RX|SX |
√ |
√ |
× |
× |
× |
|
S |
√ |
× |
√ |
× |
× |
|
SRX|SSX |
√ |
× |
× |
× |
× |
|
X |
× |
× |
× |
× |
× |
|
insert /*+ append_values */ into values |
X |
|
insert /*+ append */ into select |
X |
|
insert into values |
RX |
|
insert into select |
RX |
|
sql*ldr Conventional |
RX |
|
sql*ldr append |
X |
|
sql*ldr append+parallel |
S |
|
update |
RX |
|
delete |
RX |
|
select for update |
RX |
4.导致死锁的SQL示例。
下面给出一个最简单的示例。
_dexter@FAKE>select * from a ;
X
----------
1
2
_dexter@FAKE>select * from b ;
X
----------
1
2
s1 t1:
_dexter@FAKE>update b set x=3 where x= 1 ;
1 rowupdated.
s2 t2:
_dexter@FAKE>update a set x=3 where x=1 ;
1 rowupdated.
s1 t3:
_dexter@FAKE>update a set x=5 where x= 1 ;
s2 t4:
_dexter@FAKE>update b set x=5 where x=1 ;
s1 t5:
_dexter@FAKE>update a set x=5 where x= 1 ;
update aset x=5 where x= 1
*
ERROR atline 1:
ORA-00060:deadlock detected while waiting for resource
raise error00600 deadlock
_dexter@FAKE>select * from b ;
X
----------
3
2
s2 t6:
stillwaiting
直到s1 结束事务
|
Session1 |
Session2 |
Description |
|
|
T1 |
update b set x=3 where x= 1 ; |
Session1 更新B表记录 |
|
|
T2 |
_dexter@FAKE> update a set x=3 where x=1 ; |
Session2更新A表记录 |
|
|
T3 |
update a set x=5 where x= 1 ; waiting… |
Session1在事务中尝试修改Sessino2事务中修改的A表的数据 |
|
|
T4 |
update b set x=5 where x=1 ; waiting… |
Session2在事务中尝试修改Sessino1事务中修改的B表的数据 |
|
|
T5 |
ERROR at line 1: ORA-00060: deadlock detected while waiting for resource raise error 00600 deadlock |
上面的就是死锁的情况。Session1会Raise一个00060死锁的error。Sessino2 still waiting 。 |
从这里可以看到,由于逻辑错误锁引发的死锁。两个事务都在等待对方释放锁资源。
第一个争抢资源导致死锁的语句会被取消(只是取消这一个语句,而不是结束整个事务)
更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12
本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-08/134077.htm
