溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

oracle數(shù)據(jù)庫事務(wù)transaction鎖lock模式思考之一

發(fā)布時間:2020-08-06 17:00:05 來源:ITPUB博客 閱讀:388 作者:wisdomone1 欄目:關(guān)系型數(shù)據(jù)庫

前言

        數(shù)據(jù)庫事務(wù)是oracle非?;A(chǔ)又極為重要的概念。之前已經(jīng)介紹過相關(guān)的一些概念,相關(guān)文章見下:  
  

oracle產(chǎn)生事務(wù)transaction幾種方式或方法  
oracle事務(wù)隔離級別transaction isolation level初識

      產(chǎn)生數(shù)據(jù)庫事務(wù)時,必然會在數(shù)據(jù)庫事務(wù)運行期間產(chǎn)生各種各樣的鎖。與鎖相關(guān)的動態(tài)性能視圖為v$lock,里面有個列l(wèi)mode,即持鎖模式或叫鎖模式,其具體含義及取值

oracle數(shù)據(jù)庫事務(wù)transaction鎖lock模式思考之一

鎖模式lmode可以有7種不同的取值,每個值到底是什么意思,具體含義見下

oracle數(shù)據(jù)庫事務(wù)transaction鎖lock模式思考之一

鎖模式測試實踐

創(chuàng)建測試表并插入記錄

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

這種鎖模式 允許 多個會話并發(fā)訪問被鎖定的表,但是不允許 其它會話以 exclusive排它模式鎖定整個表
這種鎖模式也是鎖模式 share update的同義詞
這種鎖模式仍然存在是為了兼容 oracle舊版本
--未加鎖前的測試會話的持鎖信息
(可見數(shù)據(jù)庫一直會持有各種鎖,下述的鎖是系統(tǒng)鎖,而非用戶鎖)
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鎖模式并發(fā)訪問表
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鎖模式 并發(fā)訪問表
SQL> lock table t_lockmode in row exclusive mode;
Table(s) Locked.
SQL> rollback;
Rollback complete.
---其它會話可以share鎖模式并發(fā)訪問表
SQL> lock table t_lockmode in share mode;
Table(s) Locked.
SQL> rollback;
Rollback complete.
----其它會話可以 share row exclusive鎖模式并發(fā)訪問表
SQL> lock table t_lockmode in share row exclusive mode;
Table(s) Locked.
SQL> rollback;
Rollback complete.
----其它會話不能以 exclusive鎖模式并發(fā)訪問表
--卡住
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鎖模式訪問
這種鎖模式 在執(zhí)行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鎖模式 并發(fā)訪問表
SQL> lock table t_lockmode in row share mode;
Table(s) Locked.
SQL> rollback;
Rollback complete.
--其它會話可以 row exclusive鎖模式 并發(fā)訪問表
SQL> lock table t_lockmode in row exclusive mode;
Table(s) Locked.
SQL> rollback;
Rollback complete.
--其它會話 不能以share鎖模式 并發(fā)訪問表
--卡住
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鎖模式 并發(fā)訪問表
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鎖模式 并發(fā)訪問表
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

這種鎖模式 允許 多個會話并發(fā)查詢,但是不允許 對于鎖定表的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鎖模式 并發(fā)訪問表
SQL>  lock table t_lockmode in row share mode;
Table(s) Locked.
SQL> rollback;
Rollback complete.
--其它會話不能以 row exclusive鎖模式 并發(fā)訪問表
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鎖模式 并發(fā)訪問表
SQL> lock table t_lockmode in share mode;
Table(s) Locked.
SQL> rollback;
Rollback complete.
---其它會話 不允許以 share row exclusive鎖模式 并發(fā)訪問表
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鎖模式 并發(fā)訪問表
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

這種鎖模式 用于查看整個表,允許 其它會話查看表的數(shù)據(jù),但是不允許其它會話 以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鎖模式 并發(fā)訪問表
SQL> lock table t_lockmode in row share mode;
Table(s) Locked.
SQL> rollback;
Rollback complete.
--其它會話 不允許 以row exclusive鎖模式 并發(fā)訪問表
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鎖模式 并發(fā)訪問表
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鎖模式 并發(fā)訪問表
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鎖模式 并發(fā)訪問表
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鎖模式 并發(fā)訪問表
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鎖模式 并發(fā)訪問表
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鎖模式 并發(fā)訪問表
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鎖模式 并發(fā)訪問表
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鎖模式 并發(fā)訪問表
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數(shù)據(jù)庫事務(wù)transaction鎖lock模式思考之一

小結(jié)

  • exclusive鎖模式最牛逼,它是唯我獨尊,獨對排它訪問,它一占用表鎖資源,其它會話只能等待

  • row share(share update)鎖模式相對而言最溫和,它基本和所有的鎖模式可以并存,只是不允許exclusive鎖模式

  • share row exclusive鎖模式雖然沒有exclusive鎖模式這么牛逼,它可以排第二種嚴(yán)厲鎖模式,它只能兼容row share(share update)鎖模式

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

培訓(xùn)課件

(收費20元)

oracle數(shù)據(jù)庫事務(wù)transaction鎖lock模式思考之一

oracle數(shù)據(jù)庫事務(wù)transaction鎖lock模式思考之一

聯(lián)系方式

oracle數(shù)據(jù)庫事務(wù)transaction鎖lock模式思考之一

oracle數(shù)據(jù)庫事務(wù)transaction鎖lock模式思考之一

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI