iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >oracle数据库事务transaction锁lock模式思考之一
  • 848
分享到

oracle数据库事务transaction锁lock模式思考之一

2024-04-02 19:04:59 848人浏览 泡泡鱼
摘要

前言         数据库事务是oracle非常基础又极为重要的概念。之前已经介绍过相关的一些概念,相关文章见下:      o

前言

        数据库事务oracle非常基础又极为重要的概念。之前已经介绍过相关的一些概念,相关文章见下:  
  

oracle产生事务transaction几种方式或方法  
oracle事务隔离级别transaction isolation level初识

      产生数据库事务时,必然会在数据库事务运行期间产生各种各样的。与锁相关的动态性能视图为v$lock,里面有个列lmode,即持锁模式或叫锁模式,其具体含义及取值

oracle数据库事务transaction锁lock模式思考之一

锁模式lmode可以有7种不同的取值,每个值到底是什么意思,具体含义见下

oracle数据库事务transaction锁lock模式思考之一

锁模式测试实践

创建测试表并插入记录

sql> create table t_lockmode(a int,b int);
Table created.
SQL> insert into t_lockmode select 1,1 from dual;
1 row created.
SQL> commit;
Commit complete.
  • row share

这种锁模式 允许 多个会话并发访问被锁定的表,但是不允许 其它会话以 exclusive排它模式锁定整个表
这种锁模式也是锁模式 share update的同义词
这种锁模式仍然存在是为了兼容 oracle旧版本
--未加锁前的测试会话的持锁信息
(可见数据库一直会持有各种锁,下述的锁是系统锁,而非用户锁)
SQL> select addr,sid,type,lmode,request,block from v$lock where sid=73;
ADDR			SID TY	    LMODE    REQUEST	  BLOCK
---------------- ---------- -- ---------- ---------- ----------
000000008D2498B8	 73 AE		4	   0	      0
000000008D249AE8	 73 TO		3	   0	      0
--测试会话加 row share锁模式
SQL> lock table t_lockmode in row share mode;
Table(s) Locked.
--加锁模式 row share后的持锁信息
SQL> /
ADDR			SID TY	    LMODE    REQUEST	  BLOCK
---------------- ---------- -- ---------- ---------- ----------
000000008D2498B8	 73 AE		4	   0	      0
000000008D249AE8	 73 TO		3	   0	      0
00007FE54C209CD8	 73 TM		2	   0	      0  --lmode=2
---其它会话可以row share锁模式并发访问表
SQL> select sid from v$mystat where rownum=1;
       SID
----------
	28
SQL> lock table t_lockmode in row share mode;
Table(s) Locked.
SQL> rollback;
Rollback complete.
--其它会话可以 row exclusive锁模式 并发访问表
SQL> lock table t_lockmode in row exclusive mode;
Table(s) Locked.
SQL> rollback;
Rollback complete.
---其它会话可以share锁模式并发访问表
SQL> lock table t_lockmode in share mode;
Table(s) Locked.
SQL> rollback;
Rollback complete.
----其它会话可以 share row exclusive锁模式并发访问表
SQL> lock table t_lockmode in share row exclusive mode;
Table(s) Locked.
SQL> rollback;
Rollback complete.
----其它会话不能以 exclusive锁模式并发访问表
--卡住
SQL> lock table t_lockmode in exclusive mode;
SQL> /
ADDR			SID TY	    LMODE    REQUEST	  BLOCK
---------------- ---------- -- ---------- ---------- ----------
000000008D2498B8	 73 AE		4	   0	      0
000000008D249AE8	 73 TO		3	   0	      0
00007FE54C2042E0	 73 TM		2	   0	      1
  • row exclusive

这种锁模式 同于row share,但是不允许其它会话以 share锁模式访问
这种锁模式 在执行DML操作(update,insert,delete)会自动获取这种锁模式
测试会话以row exclusive锁模式持有表
SQL> lock table t_lockmode in row exclusive mode;
Table(s) Locked.
SQL> select addr,sid,type,lmode,request,block from v$lock where sid=73;
ADDR			SID TY	    LMODE    REQUEST	  BLOCK
---------------- ---------- -- ---------- ---------- ----------
000000008D2498B8	 73 AE		4	   0	      0
000000008D249AE8	 73 TO		3	   0	      0
00007FE54C2042E0	 73 TM		3	   0	      0  --lmode=3
--其它会话可以row share锁模式 并发访问表
SQL> lock table t_lockmode in row share mode;
Table(s) Locked.
SQL> rollback;
Rollback complete.
--其它会话可以 row exclusive锁模式 并发访问表
SQL> lock table t_lockmode in row exclusive mode;
Table(s) Locked.
SQL> rollback;
Rollback complete.
--其它会话 不能以share锁模式 并发访问表
--卡住
SQL> lock table t_lockmode in share mode;
^Clock table t_lockmode in share mode
           *
ERROR at line 1:
ORA-01013: user requested cancel of current operation
--其它会话 不能以share row exclusive锁模式 并发访问表
SQL> lock table t_lockmode in share row exclusive mode;
^Clock table t_lockmode in share row exclusive mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
--其它会话 不能以exclusive锁模式 并发访问表
SQL> lock table t_lockmode in exclusive mode;
^Clock table t_lockmode in exclusive mode
           *
ERROR at line 1:
ORA-01013: user requested cancel of current operation
  • share

这种锁模式 允许 多个会话并发查询,但是不允许 对于锁定表的update操作
测试会话以share锁模式持有表
SQL> lock table t_lockmode in share mode;
Table(s) Locked.
SQL> select addr,sid,type,lmode,request,block from v$lock where sid=73;
ADDR			SID TY	    LMODE    REQUEST	  BLOCK
---------------- ---------- -- ---------- ---------- ----------
000000008D2498B8	 73 AE		4	   0	      0
000000008D249AE8	 73 TO		3	   0	      0
00007FE54C209CD8	 73 TM		4	   0	      0  --lmode=4
--其它会话可以row share锁模式 并发访问表
SQL>  lock table t_lockmode in row share mode;
Table(s) Locked.
SQL> rollback;
Rollback complete.
--其它会话不能以 row exclusive锁模式 并发访问表
SQL> lock table t_lockmode in row exclusive mode;
^Clock table t_lockmode in row exclusive mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
--其它会话可以 share锁模式 并发访问表
SQL> lock table t_lockmode in share mode;
Table(s) Locked.
SQL> rollback;
Rollback complete.
---其它会话 不允许以 share row exclusive锁模式 并发访问表
SQL> lock table t_lockmode in share row exclusive mode;
^Clock table t_lockmode in share row exclusive mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
---其它会话 不允许以 exclusive锁模式 并发访问表
SQL>  lock table t_lockmode in  exclusive mode;
^C lock table t_lockmode in  exclusive mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
  • share row exclusive

这种锁模式 用于查看整个表,允许 其它会话查看表的数据,但是不允许其它会话 以share锁模式获取表 ,也不允许 其它会话update被锁定表
这种锁模式 允许 对于锁定表的查询,但不允许 对于锁定表的其它任何操作
测试会话以 share row exclusive锁模式持有表
SQL> lock table t_lockmode in share row exclusive mode;
Table(s) Locked.
SQL> /
ADDR			SID TY	    LMODE    REQUEST	  BLOCK
---------------- ---------- -- ---------- ---------- ----------
000000008D2498B8	 73 AE		4	   0	      0
000000008D249AE8	 73 TO		3	   0	      0
00007FE54C209CD8	 73 TM		5	   0	      0
--其它会话 允许 以row share锁模式 并发访问表
SQL> lock table t_lockmode in row share mode;
Table(s) Locked.
SQL> rollback;
Rollback complete.
--其它会话 不允许 以row exclusive锁模式 并发访问表
SQL> lock table t_lockmode in row exclusive mode;
^Clock table t_lockmode in row exclusive mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
---其它会话 不允许 以share锁模式 并发访问表
SQL> lock table t_lockmode in  share mode;
^Clock table t_lockmode in  share mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
---其它会话 不允许 以share row exclusive锁模式 并发访问表
SQL> lock table t_lockmode in share row exclusive mode;
^Clock table t_lockmode in share row exclusive mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
---其它会话 不允许以 exclusive锁模式 并发访问表
SQL> lock table t_lockmode in exclusive mode;
^Clock table t_lockmode in exclusive mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
  • exclusive

这种锁模式 允许 对于锁定表的查询,但不允许 对于锁定表的其它任何操作
--测试会话以  exclusive锁模式持有表
SQL> lock table t_lockmode in exclusive mode;
Table(s) Locked.
SQL> /
ADDR			SID TY	    LMODE    REQUEST	  BLOCK
---------------- ---------- -- ---------- ---------- ----------
000000008D2498B8	 73 AE		4	   0	      0
000000008D249AE8	 73 TO		3	   0	      0
00007FE54C2042E0	 73 TM		6	   0	      0  --lmode=6
--其它会话 不允许以 row share锁模式 并发访问表
SQL> lock table t_lockmode in row share mode;
^Clock table t_lockmode in row share mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
---其它会话不允许 以row exclusive锁模式 并发访问表
SQL> lock table t_lockmode in row exclusive mode;
^Clock table t_lockmode in row exclusive mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
---其它会话不允许以 share锁模式 并发访问表
SQL> lock table t_lockmode in share mode;
^Clock table t_lockmode in share mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
---其它会话不允许 以share row exclusive锁模式 并发访问表
SQL> lock table t_lockmode in share row exclusive mode;
^Clock table t_lockmode in share row exclusive mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
--其它会话不允许 以exclusive锁模式 并发访问表
SQL> lock table t_lockmode in exclusive mode;
^Clock table t_lockmode in exclusive mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation

锁模式之间的的兼容性图

oracle数据库事务transaction锁lock模式思考之一

小结

  • exclusive锁模式最牛逼,它是唯我独尊,独对排它访问,它一占用表锁资源,其它会话只能等待

  • row share(share update)锁模式相对而言最温和,它基本和所有的锁模式可以并存,只是不允许exclusive锁模式

  • share row exclusive锁模式虽然没有exclusive锁模式这么牛逼,它可以排第二种严厉锁模式,它只能兼容row share(share update)锁模式

  • row exclusive及share锁模式排位在share row exclusive之后,它可以兼容3种锁模式,不兼容余下2种锁模式

培训课件

(收费20元)

oracle数据库事务transaction锁lock模式思考之一

oracle数据库事务transaction锁lock模式思考之一

联系方式

oracle数据库事务transaction锁lock模式思考之一

oracle数据库事务transaction锁lock模式思考之一

您可能感兴趣的文档:

--结束END--

本文标题: oracle数据库事务transaction锁lock模式思考之一

本文链接: https://www.lsjlt.com/news/45681.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作