insert,update和delete造成阻塞的示例1.update产生锁示例:www.2cto.comsession1:SQL>droptabletpurge;SQL>createtablet(idintprimarykey);Tablecreated.SQL>selectsidfrom...
insert,update和delete造成阻塞的示例
1.update 产生锁示例: www.2cto.com
session 1:
SQL> drop table t purge;
SQL> create table t(id int primary key);
Table created.
SQL> select sid from v$mystat where rownum=1;
SID
----------
854
SQL> insert into t values(1);
1 row created.
SQL> commit;
Commit complete.
SQL> update t set id=2 where id=1; --更新未提交
1 row updated.
session 2:
SQL> select sid from v$mystat where rownum=1;
SID
----------
834
SQL> update t set id=3 where id=1; --此时更新同条记录时SQL产生等待
session 3:
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where sid in (834,854) order by 1,2;
SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
834 TM 91874 0 3 0 0
834 TX 655407 1648480 0 6 0
854 TM 91874 0 3 0 0
854 TX 655407 1648480 6 0 1
SQL> select sid,event from v$session_wait where sid in (834,854);
SID EVENT
---------- ----------------------------------------------------------------
834 enq: TX - row lock contention
854 SQL*Net message from client
说明:
这里sid=854是session1,sid=834是session2;
TM是一个表级锁(段锁),表示此表的记录在修改时,不允许对表进行DDL操作;
TX是一个行级锁(事务锁),表示不允许对表修改的记录进行DML操作;
当TY=TM and ID2=0时,ID1的值为此表(段)的object_id;
当TY=TX and ID2<>0时,ID1+ID2构成了这个事务在回滚段中的位置;
LMODE=3表示一个表级共享锁,LMODE=6行级的排他锁(最高模式的锁);
REQUEST=6 表示当前会话正待等待一个LMODE=6 的锁,表明这个会话正在被阻塞;
block=1是表示这个会话正在阻塞其它会话;
2.delete 产生锁示例:
session 1:
SQL> select * from t;
ID
----------
1
SQL> delete from t where id=1; --删除SQL未提交
1 row deleted.
session 2:
SQL> delete from t where id=1; --此时删除同条记录时SQL产生等待
session 3:
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where sid in (834,854) order by 1,2;
SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
834 TM 91874 0 3 0 0
834 TX 655364 1648669 0 6 0
854 TM 91874 0 3 0 0
854 TX 655364 1648669 6 0 1
SQL> select sid,event from v$session_wait where sid in (834,854);
SID EVENT
---------- ----------------------------------------------------------------
834 enq: TX - row lock contention
854 SQL*Net message from client
说明:
同update一样
3.insert 产生锁示例:
session 1:
SQL> drop table t purge;
SQL> create table t(id int primary key);
Table created.
SQL> insert into t values(1); --插入未提交
1 row created.
session 2:
SQL> insert into t values(1); --此时插入有同样主键记录时SQL产生等待
session 3:
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where sid in (834,854) order by 1,2;
SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
834 TM 91874 0 3 0 0
834 TX 262174 192335 6 0 0
834 TX 458776 193901 0 4 0
854 TM 91874 0 3 0 0
854 TX 458776 193901 6 0 1
SQL> select sid,event from v$session_wait where sid in (834,854);
SID EVENT
---------- ----------------------------------------------------------------
834 enq: TX - row lock contention
854 SQL*Net message from client
说明:
这里和update产生锁的情况不一样的是,会话2在持有一个LMODE=6的排他锁和等待一个LMODE=4的锁。
因插入的并不是同一条记录,会话2插入时的记录没被阻塞,而是对表的数据块头修改的阻塞,所以会话在请求一个LMODE=4的锁。