暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

开始聊聊“锁”-- 4.Oracle DML表锁的实验

InDataBase 2021-04-26
539

是时候表演真正的实验了。

这次实验主要是对表级锁的兼容性进行验证。


之前的老图,表级锁及兼容性一览表。


本次实验比较长长长,慢慢看。

整个实验中,使用了两个Session会话:Session A、Session B


开始:

2号锁

A:lock table scott.dept in row share mode;

A对表加了2号锁。


B:drop table scott.dept;

B执行删除表失败,2号表级锁限制DDL操作。


B:lock table scott.dept in exclusive mode nowait;

B加6号锁失败,2号表级锁与6号有冲突。


B: select loc from scott.dept where deptno=20 for udpate;

B加3号锁成功,2号表级锁与3号不冲突。


A:update scott.dept set loc='NEW YORK' where deptno=20;

A执行更新,加3号锁,因为与B在行级上发生了等待。


3号锁

A:rollback; 

A:lock table scott.dept in row exclusive mode;

A对表加了3号锁


B:rollback;

B:lock table scott.dept in exclusive mode;

B加6号锁失败,3号表级锁与6号有冲突。


B:lock table scott.dept in share row exclusive mode nowait;

B加5号锁失败,3号表级锁与5号有冲突。


B:rollback;

A:select loc from scott.dept where deptno=20 for udpate;

B:update scott.dept set loc='NEW YORK' where deptno=20;

A、B都为3号锁,在同一行,所以冲突了。


4号锁

A:lock table scott.dept in share mode;

A对表加了4号锁


B:lock table scott.dept in exclusive mode nowait;

B加6号锁失败,4号表级锁与6号有冲突。


B:lock table scott.dept in share row exclusive mode nowait;

B加5号锁失败,4号表级锁与5号有冲突。


B:lock table scott.dept in share mode;

B加4号锁失败,4号表级锁与4号没有冲突。


B:select loc from scott.dept where deptno=20;

select是0号,与4号表级锁不冲突。


B:select loc from scott.dept where deptno=20 for update;

B是3号表级锁+6号行级锁,与4号表级锁冲突了。


5号锁

A:lock table scott.dept in share row exclusive mode;

A对表加了5号锁


B:lock table scott.dept in exclusive mode nowait;

B加6号锁失败,5号表级锁与6号有冲突。


B:lock table scott.dept in share row exclusive mode nowait;

B加5号锁失败,5号表级锁与5号有冲突。


B:lock table scott.dept in share mode nowait;

B加4号锁失败,5号表级锁与4号有冲突。


6号锁

A:lock table scott.dept in exclusive mode;

A对表加了6号锁


B:lock table scott.dept in exclusive mode;

B加6号锁失败,6号表级锁与6号有冲突。


B:lock table scott.dept in row exclusive mode nowait;

B加3号锁失败,6号表级锁与3号有冲突。


B:lock table scott.dept in share mode;


B加4号锁失败,6号表级锁与4号有冲突。


B:lock table scott.dept in row share mode nowait;

B加2号锁失败,6号表级锁与2号有冲突。


吐血结束。

之所以做这个实验,目的有两个:

1、验证结论是否正确,因为现在网上的帖子多、说法多,到底什么正确,只有眼见为实。

2、敲敲代码可以让你静下心来,对这些表格类的结论加强记忆。


接下来还剩DDL锁,下次聊。


翻翻历史,温故知新

开始聊聊恶心的“锁”-- 3.Oracle DML表级锁概念

开始聊聊恶心的“锁”-- 2.Oracle DML行级锁概念

开始聊聊恶心的“锁”-- 1.一个故事了解Oracle有哪些锁

死锁与悲观锁、乐观锁不是一类东西

骨灰级科普-Oracle的序列可能不连续

后台翻页必用的rownum,你知道它的处理逻辑吗?

总在面试时被问COUNT(*)、COUNT(1)哪个快?

NVL系与Decode系,他们的差别你了解吗?

骨灰级科普-Oracle中的NULL,神马也不是

SQL也可以不那么简单



文章转载自InDataBase,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论