lock总结

lock框架包括3个组件 resource structure,lock structure,enqueue.
resource structure,lock structure是数据结构
当进程或事务要访问资源时,需从内存中申请一个lock structure,其中记录锁模式(共享还是占有)、进程id等
如果资源已经被其他进程占有(或共享)且存在多个进程先后申请占有锁,则这些持有锁的进程依先后顺序进入队列(先进先出)
形成链表。resource structure有waiter指针指向这个链表。同样的 owner指针指向已占有资源的进程。
convert也是等待队列 不过优先级高于waiter。
如果是dml修改操作,需要先申请回滚段资源即同时申请一个tx锁 这个锁用于保护回滚段数据块。
而操作对象(表 视图)是需要先申请tm锁 用于保护对象数据结构,在其操作过程中不允许修改对象的数据结构。
V$LOCK
This view lists the locks currently held by the Oracle Database and outstanding requests for a lock or latch.
关键字段描述
1、TYPE VARCHAR2(2) Type of user or system lock
The locks on the user types are obtained by user applications. Any process that is blocking others is likely to be holding one of these locks. The user type locks are:
TM - DML enqueue
TX - Transaction enqueue
UL - User supplied
The locks on the system types are held for extremely short periods of time. The system type locks are listed in Table 6-1.
 
2、LMODE NUMBER Lock mode in which the session holds the lock:
•0 - none
•1 - null (NULL)
•2 - row-S (SS)
•3 - row-X (SX)
•4 - share (S)
•5 - S/Row-X (SSX)
•6 - exclusive (X)
 
3、REQUEST NUMBER Lock mode in which the process requests the lock:
•0 - none
•1 - null (NULL)
•2 - row-S (SS)
•3 - row-X (SX)
•4 - share (S)
•5 - S/Row-X (SSX)
•6 - exclusive (X)
脚本:
查看事务锁模式
select sid,type,decode(LMODE,0,'none',1,'null',
             2,'row-S (SS)',
      3,'row-X (SX)',
      4,'share (S)',
      5,'S/Row-X (SSX)',
      6,'exclusive (X)') LMODE,
             decode(REQUEST,0,'none',1,'null',
             2,'row-S (SS)',
      3,'row-X (SX)',
      4,'share (S)',
      5,'S/Row-X (SSX)',
      6,'exclusive (X)') REQUEST,
      case when lmode>0 and request = 0 then 'owner'
           when lmode=0 and request > 0 then 'waiter'
    when lmode>0 and request > 0 then 'converter' end ROLE
      from v$lock where sid='124';
用于定位跟踪文件
select spid,a.sid from v$session a,v$process b
where a.PADDR = b.addr and a.sid =(select sid from v$mystat where rownum=1);
队列的诊断事件
alter session set events '10704 trace name context forever';
跟踪enqueues 10706全局enqueues
查看等待事件
select sid,event from v$session_wait where sid='147';
会话一:
--锁表 不允许dml操作
SQL> lock table hrtab in exclusive mode;
Table(s) Locked.
--TM锁 lmode 排斥状态  type = TM
      SID TY LMODE         REQUEST       ROLE
---------- -- ------------- ------------- ---------
       125 TM exclusive (X) none          owner
--跟踪文件输出
ksqcmi: TM,ce6b,0 mode=6 timeout=21474836
ksqcmi: returns 0
会话二:
SQL> delete from hrtab;
--hang  挂住
--REQUEST  请求的锁模式sx TM共享 行级锁排斥  等待会话一释放表的tm锁
       SID TY LMODE         REQUEST       ROLE
---------- -- ------------- ------------- ---------
       147 TM none          row-X (SX)    waiter
跟踪文件输出
ksqcmi: TM,ce6b,0 mode=3 timeout=21474836
*** 2010-09-14 22:26:59.377
DUMP LOCAL BLOCKER/HOLDER: block level 2 res [0xce6b][0x0],[TM]
----------resource 0x0x2d159bc8----------------------
resname       : [0xce6b][0x0],[TM]
Local node    : 0
dir_node      : 0
master_node   : 0
hv idx        : 63
hv last r.inc : 0
current inc   : 2
hv status     : 0
hv master     : 0
open options  : dd cached
grant_bits    : KJUSERNL KJUSEREX
grant mode    : KJUSERNL  KJUSERCR  KJUSERCW  KJUSERPR  KJUSERPW  KJUSEREX
count         : 1         0         0         0         0         1
val_state     : KJUSERVS_NOVALUE
valblk        : 0x00000000000000000000000000000000 .
access_node   : 0
vbreq_state   : 0
state         : x0
resp          : 0x2d159bc8
On Scan_q?    : N
cache level   : 0
Total accesses: 386
Imm.  accesses: 283
Granted_locks : 1
Cvting_locks  : 1
value_block:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
GRANTED_Q :
lp 0x2f17f274 gl KJUSEREX rp 0x2d159bc8 [0xce6b][0x0],[TM]
  master 0 gl owner 0x2fee3848 possible pid 20103 xid 18000-0001-00000104 bast 0 rseq 5 mseq 0 history 0x14951495
  open opt KJUSERDEADLOCK 
CONVERT_Q:
lp 0x2f18080c gl KJUSERNL rl KJUSERCW rp 0x2d159bc8 [0xce6b][0x0],[TM]
  master 0 gl owner 0x2fefd3c0 possible pid 22309 xid 1C000-0001-00000022 bast 0 rseq 5 mseq 0 history 0x4951449a
  convert opt KJUSERGETVALUE 
----------enqueue 0x0x2f17f274------------------------
lock version     : 3515
Owner node       : 0
grant_level      : KJUSEREX
req_level        : KJUSEREX
bast_level       : KJUSERNL
notify_func      : (nil)
resp             : 0x2d159bc8
procp            : 0x2f038e30
pid              : 20103  --占有资源的进程ID
proc version     : 7
oprocp           : (nil)
opid             : 0
group lock owner : 0x2fee3848
possible pid     : 20103
xid              : 18000-0001-00000104
dd_time          : 0.0 secs
dd_count         : 0
timeout          : 0.0 secs
On_timer_q?      : N
On_dd_q?         : N
lock_state       : GRANTED
Open Options     : KJUSERDEADLOCK
Convert options  : KJUSERGETVALUE
History          : 0x14951495
Msg_Seq          : 0x0
res_seq          : 5
valblk           : 0x00000000000000000000000000000000 .
DUMP LOCAL BLOCKER: initiate state dump for
  possible owner[24.20103]
Submitting asynchronized dump request [28]
查看会话二的等待事件
       SID EVENT
---------- ----------------------------------------------------------------
       147 enq: TM - contention
--等待表tm锁
会话一
释放锁
SQL> rollback;
Rollback complete.
跟踪日志输出
*** 2010-09-14 23:02:01.152
ksqcmi: TM,ce6b,0 mode=0 timeout=0
ksqcmi: release this enqueue
ksqcmi: returns 0
会话二:
日志:
*** 2010-09-14 23:02:01.165
ksqcmi: returns 0
ksqcmi: TM,ce6b,0 mode=0 timeout=0
ksqcmi: release this enqueue
ksqcmi: returns 0
ksqcmi: TM,ce6b,0 mode=6 timeout=21474836
ksqcmi: returns 0
ksqcmi: TM,ce6b,0 mode=0 timeout=0
ksqcmi: release this enqueue  --释放waiter队列
ksqcmi: returns 0
ksqcmi: TM,ce6b,0 mode=3 timeout=21474836
ksqcmi: returns 0
--新增letx锁 说明进程会先检测需处理记录的记录头,判断其itl(interested trancation list)索引
是否指向数据库itl中的表项。如果是,则这个表项可能记录进程id或事务id 或事务申请的回滚段地址。
这个事例中会话一没有涉及回滚段,会话二等待其释放tm锁。当会话一释放tm锁后,会话二首先申请
tx锁,然后在记录的数据块itl中申请空闲的表项,表项记录了回滚段的地址,修改记录头的索引指向这个表项,
修改记录前,拷贝原记录到回滚段中。

       SID TY LMODE         REQUEST       ROLE
---------- -- ------------- ------------- ---------
       147 TM row-X (SX)    none          owner
       147 TX exclusive (X) none          owner
会话二 回滚
日志
*** 2010-09-14 23:16:50.366
ksqcmi: TM,ce6b,0 mode=0 timeout=0
ksqcmi: release this enqueue --释放队列
ksqcmi: returns 0

实验二:
1、会话一
SQL> insert into hrtab values(5,'k');
1 row created.
日志
ksqcmi: TM,ce6b,0 mode=3(rx) timeout=21474836
ksqcmi: returns 0
持有锁
       SID TY LMODE         REQUEST       ROLE
---------- -- ------------- ------------- ---------
       131 TM row-X (SX)    none          owner
       131 TX exclusive (X) none          owner
2、会话二:
SQL> insert into hrtab values(7,'l');
1 row created.

       SID TY LMODE         REQUEST       ROLE
---------- -- ------------- ------------- ---------
       147 TM row-X (SX)    none          owner
       147 TX exclusive (X) none          owner
--tm 锁是共享的 所有不同的进程做insert相互没有影响

实验三:
会话一
SQL> update hrtab set val='p' where id='1';
2 rows updated.
       SID TY LMODE         REQUEST       ROLE
---------- -- ------------- ------------- ---------
       131 TM row-X (SX)    none          owner
       131 TX exclusive (X) none          owner
会话二:
SQL> update hrtab set val='p' where id='1';
挂起hang
       SID TY LMODE         REQUEST       ROLE
---------- -- ------------- ------------- ---------
       147 TX none          exclusive (X) waiter
       147 TM row-X (SX)    none          owner
--等待会话一释放tx锁 tx互斥导致会话二无法做一致性读
会话二等待事件
       SID EVENT
---------- ----------------------------------------------------------------
       147 enq: TX - row lock contention
其他:
lock table hrtab in share mode;
lock table hrtab in share row exclusive mode;
lock table hrtab in row shard mode;
lock table hrtab in row exclusive mode;

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/21993926/viewspace-673952/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/21993926/viewspace-673952/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值