TX (transaction) lock

A TX lock is acquired when a transaction initiates its first change,and it is held until the transaction performs acommit or rollback.

Oracle process lock like this:

1.  find the address of the row you want to lock

2  goto the row

3 lock the row (waiting for the transaction that has it locked to end if it is already locked, unless you are using the NOWAIT option).


SQL> update dept set deptno = deptno + 10;

4 rows updated.

SQL> select username,
  2     v$lock.sid,
  3     trunc(id1/power(2,16)) rbs,
  4     bitand(id1,to_number('ffff','xxxx'))+0 slot,
  5     id2 seq,
  6     lmode,
  7     request
  8  from v$lock,v$session
  9  where v$lock.type = 'TX'
 10  and v$lock.sid = v$session.sid
 11  and v$session.username =USER;

USERNAME                              SID        RBS       SLOT        SEQ
------------------------------ ---------- ---------- ---------- ----------
     LMODE    REQUEST
---------- ----------
OPS                                   159          6         15       2564
         6          0


SQL> select XIDUSN,XIDSLOT,XIDSQN
  2  from v$transaction;

    XIDUSN    XIDSLOT     XIDSQN
---------- ---------- ----------
         6         15       2564


SQL> update dept set deptno = deptno + 10;

4 rows updated.


新开一个session执行

SQL> update emp set ename = upper(ename);

14 rows updated.

SQL> update dept set deptno = deptno - 10;

会话block在这里了。


SQL> select username,
  2     v$lock.sid,
  3     trunc(id1/power(2,16)) rbs,
  4     bitand(id1,to_number('ffff','xxxx'))+0 slot,
  5     id2 seq,
  6     lmode,
  7     request
  8  from v$lock,v$session
  9  where v$lock.type = 'TX'
 10  and v$lock.sid = v$session.sid
 11  and v$session.username =USER;


USERNAME          SID        RBS       SLOT        SEQ      LMODE    REQUEST
---------- ---------- ---------- ---------- ---------- ---------- ----------
OPS               159          8          3       2568          0          6
OPS               159         10          0       2572          6          0
OPS               153          8          3       2568          6          0


用v$lock表的self-join

SQL> select
  2     (select username from v$session where sid = a.sid) blocker,
  3     a.sid,
  4     'is blocking ',
  5     (select username from v$session where sid = b.sid) blockee,
  6     b.sid
  7  from v$lock a,v$lock b
  8  where a.block = 1
  9  and b.request > 0
 10  and a.id1 = b.id1
 11  and a.id2 = b.id2;

BLOCKER                               SID 'ISBLOCKING'
------------------------------ ---------- ------------
BLOCKEE                               SID
------------------------------ ----------
OPS                                   153 is blocking
OPS                                   159


SQL> col blocker for a10
SQL> col blockee for a10
SQL> /

BLOCKER           SID 'ISBLOCKING' BLOCKEE           SID
---------- ---------- ------------ ---------- ----------
OPS               153 is blocking  OPS               159


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值