insert操作引起的row lock测验(Row Locking on Insert)


记录insert 引发的row lock等待的测验

一、测验

1.创建主从表

create table t1 (
  id1 int primary key
);
create table t2 (
  id1 int references t1 (id1)
);

insert into t1 values (1);
insert into t2 values (1);
commit

2.从session 1删除子表
session 1:

SQL> delete t2;

3.session 2 插入1笔至父表
session 2:

SQL> insert into t1 values (2);

4.可以看出没有阻塞

select l1.sid, ' IS BLOCKING ', l2.sid
from   v$lock l1, v$lock l2
where  l1.block =1 and l2.request > 0
and    l1.id1=l2.id1
and    l1.id2=l2.id2
no rows selected

5.返回session 1,同样插入1笔至主表t1

SQL> rollback;
Rollback complete.
SQL> insert into t1 values (2);

6.此时会话 1 卡住,等待会话 2 提交/回滚:

SQL> select l1.sid, ' IS BLOCKING ', l2.sid
  2  from   v$lock l1, v$lock l2
  3  where  l1.block =1 and l2.request > 0
  4  and    l1.id1=l2.id1
  5  and    l1.id2=l2.id2;
         SID 'ISBLOCKING'         SID
---------- ------------- ----------
       615  IS BLOCKING         593

二、结论

当不同的session在拥有主键约束的表上insert相同的键值,也会引发row lock竞争

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值