也叫事务锁。每个事务只能有一个。
每个事务对应该一个TX锁,在该事务中修改或者select for update的每一行都会指向这个锁(数据块的结构中有相关的标志)。
如果一个事务想修改的某条记录已经被另一个事务锁住,那么这个事务会等待,如果再有事务会形成一个列表,等待的信息可以在v$lock里面查到。
准备实验数据:
SCOTT@ prod> create table emp2 as select * from emp ;
Table created.
SCOTT@ prod> create table dept2 as select * from dept ;
Table created.
SCOTT@ prod> alter table dept2 add primary key (deptno ) ;
Table altered.
SCOTT@ prod> alter table emp2 add primary key (empno ) ;
Table altered.
SCOTT@ prod> alter table emp2 add foreign key ( deptno ) references dept2(deptno) ;
Table altered.
SCOTT@ prod> create index idx111 on emp2(deptno ) ;
Index created.
SESSION1:
SCOTT@ prod> update dept2 set dname = initcap(dname) ;
4 rows updated.
SESSION2:
SYS@ prod> select username , v$lock.sid , trunc(id1/power(2,16)) rbs , bitand(id1 , to_number('ffff' , 'xxxx' )) + 0 slot ,
2 id2 seq , lmode , request
3 from v$lock , v$session
4 where v$lock.type = 'TX' and v$lock.sid = v$session.sid and v$session.username = 'SCOTT' ;
USERNAME SID RBS SLOT SEQ LMODE REQUEST
------------------------------ ---------- ---------- ---------- ---------- ---------- ----------
SCOTT 32 3 16 1735 6 0
SYS@ prod> select xidusn , xidslot , xidsqn from v$transaction ;
XIDUSN XIDSLOT XIDSQN
---------- ---------- ----------
3 16 1735
LMODE为6代表排它锁,REQUEST为0代表已经取得。
SESSION3:
SCOTT@ prod> update emp2 set ename = upper(ename) ;
14 rows updated.
SCOTT@ prod> update dept2 set deptno = deptno - 10 ;
被阻塞。
SESSION2:
SYS@ prod> select username , v$lock.sid , trunc(id1/power(2,16)) rbs , bitand(id1 , to_number('ffff' , 'xxxx' )) + 0 slot ,
2 id2 seq , lmode , request from v$lock , v$session
3 where v$lock.type = 'TX' and v$lock.sid = v$session.sid and v$session.username = 'SCOTT' ;
USERNAME SID RBS SLOT SEQ LMODE REQUEST
------------------------------ ---------- ---------- ---------- ---------- ---------- ----------
SCOTT 35 3 16 1735 0 6
SCOTT 35 2 14 1754 6 0
SCOTT 32 3 16 1735 6 0
SYS@ prod> select xidusn , xidslot , xidsqn from v$transaction ;
XIDUSN XIDSLOT XIDSQN
---------- ---------- ----------
2 14 1754
3 16 1735
可以看到SID为35的SESSION有两条,一条是它所请求的锁,一条是阻塞它的锁。
做自连接查看:
SYS@ prod> select ( select username from v$session where sid = a.sid ) blocker , a.sid ,
2 ' is blocking ' ,
3 ( select username from v$session where sid = b.sid ) blockee ,
4 b.sid
5 from v$lock a , v$lock b
6 where a.block = 1 and b.request > 0 and a.id1 = b.id1 and a.id2 = b.id2 ;
BLOCKER SID 'ISBLOCKING' BLOCKEE SID
------------------------------ ---------- ------------- ------------------------------ ----------
SCOTT 32 is blocking SCOTT 35
SESSION1:
提交
SCOTT@ prod> commit ;
Commit complete.
SESSION3:
阻塞被解除,更新成功。
4 rows updated.
SESSION2:
会话一的锁已经释放。
SYS@ prod> select username , v$lock.sid , trunc(id1/power(2,16)) rbs , bitand(id1 , to_number('ffff' , 'xxxx' )) + 0 slot ,
2 id2 seq , lmode , request from v$lock , v$session
3 where v$lock.type = 'TX' and v$lock.sid = v$session.sid and v$session.username = 'SCOTT' ;
USERNAME SID RBS SLOT SEQ LMODE REQUEST
------------------------------ ---------- ---------- ---------- ---------- ---------- ----------
SCOTT 35 2 14 1754 6 0
SYS@ prod> select xidusn , xidslot , xidsqn from v$transaction ;
XIDUSN XIDSLOT XIDSQN
---------- ---------- ----------
2 14 1754