oracle v$lock系列之三
結論
1,oracle11g測試環境2,產生事務時會同時在表上加2個鎖,分別為事務鎖tx鎖以及表鎖tm鎖
3,表鎖tm鎖又從鎖定模式分為7個不同值
本文僅測試了其中5個不同值
分別為:
2 - row-S (SS)
3 - row-X (SX)
4 - share (S)
5 - S/Row-X (SSX)
6 - exclusive (X)
4,上述的鎖定模式數字越大,持鎖規模越大,越影響並行操作,即:
6 - exclusive (X) > 5 - S/Row-X (SSX) > 4 - share (S) > 3 - row-X (SX) > 2 - row-S (SS)
5,關於這些持鎖模式相互相容性,請見下述的具體測試
6,關於如下2種持鎖模式在另一則文章進行測試
0 - none
1 - null (NULL)
測試
SQL> create table t_lock(a int);
Table created.
SQL> insert into t_lock values(1);
1 row created.
commit;
SQL> update t_lock set a=999;
1 row updated.
數字1的十六進位制(在資料塊以16進位制儲存)
SQL> select dump(1,16) from dual;
DUMP(1,16)
----------------------------------
Typ=2 Len=2: c1,2
可見新事務會產生行鎖(TX鎖)及表鎖(TM鎖)
SQL> select sid,type,id1,id2,lmode,request,ctime,block from v$lock where sid in (select sid from v$mystat where rownum=1) and type in ('TM','TX');
SID TYPE ID1 ID2 LMODE REQUEST CTIME BLOCK
---------- ---- ---------- ---------- ---------- ---------- ---------- ----------
37 TM 74639 0 3 0 261 0
37 TX 10289154 4472 6 0 261 0
SQL> select DBMS_ROWID.ROWID_RELATIVE_FNO(ROWID),DBMS_ROWID.ROWID_BLOCK_NUMBER(ROWID) from t_lock;
DBMS_ROWID.ROWID_RELATIVE_FNO(ROWID) DBMS_ROWID.ROWID_BLOCK_NUMBER(ROWID)
------------------------------------ ------------------------------------
4 98894
測試下tm表鎖不同鎖模式的適用場景
先摘錄下ORACLE V$LOCK關於LMODE的含義
LMODE NUMBER Lock mode in which the session holds the lock:
0 - none
1 - null (NULL)
2 - row-S (SS)
3 - row-X (SX)
4 - share (S)
5 - S/Row-X (SSX)
6 - exclusive (X)
先看 2 - row-S (SS)
會話1
SQL> lock table t_testlock in row share mode;
Table(s) Locked.
SQL> select sid,type,id1,id2,decode(lmode,0,'none',1,'null',2,'row-s',3,'row-x',4,'s',5,'ssx',6,'x') lmode,request,ctime,block from v$lock where sid in (select sid from v$mystat where rownum=1) and type in ('TM');
SID TYPE ID1 ID2 LMODE REQUEST CTIME BLOCK
---------- ---- ---------- ---------- ---------- ---------- ---------- ----------
245 TM 74659 0 row-s 0 4 0
會話2
SQL> insert into t_testlock values(2);
1 row created.
SQL> delete from t_testlock;
2 rows deleted.
SQL> update t_testlock set a=33;
1 row updated.
SQL> truncate table t_testlock;
truncate table t_testlock
*
ERROR at line 1:
ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired
SQL> drop table t_testlock purge;
drop table t_testlock purge
*
ERROR at line 1:
ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired
SQL> lock table t_testlock in row share mode;
Table(s) Locked.
SQL> lock table t_testlock in row exclusive mode;
Table(s) Locked.
SQL> lock table t_testlock in share mode;
Table(s) Locked.
SQL> lock table t_testlock in share row exclusive mode;
Table(s) Locked.
SQL> lock table t_testlock in share update mode;
Table(s) Locked.
SQL> lock table t_testlock in exclusive mode;
lock table t_testlock in exclusive mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
可見row-s行級共享鎖不會阻塞其它會話的任何DML操作,但會阻塞ddl操作
即會阻塞表級排它鎖操作,但不會阻塞行級排它鎖
---------------------------------------------------------------
會話1
SQL> create table t_testlock(a int);
Table created.
SQL> create table t_testlock(a int);
Table created.
SQL> insert into t_testlock values(999);
1 row created.
可見insert獲取行級排它鎖row-x
SQL> select sid,type,id1,id2,decode(lmode,0,'none',1,'null',2,'row-s',3,'row-x',4,'s',5,'ssx',6,'x') lmode,request,ctime,block from v$lock where sid in (select sid from v$mystat where rownum=1) and type in ('TM');
SID TYPE ID1 ID2 LMODE REQUEST CTIME BLOCK
---------- ---- ---------- ---------- ---------- ---------- ---------- ----------
245 TM 74659 0 row-x 0 394 0
或者採用如下方式
會話1
SQL> lock table t_testlock in row exclusive mode;
Table(s) Locked.
SQL> select sid,type,id1,id2,decode(lmode,0,'none',1,'null',2,'row-s',3,'row-x',4,'s',5,'ssx',6,'x') lmode,request,ctime,block from v$lock where sid in (select sid from v$mystat where rownum=1) and type in ('TM');
SID TYPE ID1 ID2 LMODE REQUEST CTIME BLOCK
---------- ---- ---------- ---------- ---------- ---------- ---------- ----------
245 TM 74659 0 row-x 0 2 0
會話2
SQL> insert into t_testlock values(2);
1 row created.
SQL> drop table t_testlock purge;
drop table t_testlock purge
*
ERROR at line 1:
ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired
SQL> lock table t_testlock in row share mode;
Table(s) Locked.
SQL> lock table t_testlock in row exclusive mode;
Table(s) Locked.
SQL> lock table t_testlock in share mode;
lock table t_testlock in share mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> lock table t_testlock in share row exclusive mode;
lock table t_testlock in share row exclusive mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> lock table t_testlock in exclusive mode;
lock table t_testlock in exclusive mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
可見row-x行級排它鎖不會阻塞其它會話的DML操作,但會阻塞ddl操作
同時會阻塞行級或表級排它鎖以及表級共享鎖
---------------------------------------------------------------
會話1
SQL> lock table t_testlock in share mode;
Table(s) Locked.
SQL> select sid,type,id1,id2,decode(lmode,0,'none',1,'null',2,'row-s',3,'row-x',4,'s',5,'ssx',6,'x') lmode,request,ctime,block from v$lock where sid in (select sid from v$mystat where rownum=1) and type in ('TM');
SID TYPE ID1 ID2 LMODE REQUEST CTIME BLOCK
---------- ---- ---------- ---------- ---------- ---------- ---------- ----------
245 TM 74659 0 s 0 5 0
會話2
SQL> insert into t_testlock values(1);
insert into t_testlock values(1)
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> delete from t_testlock;
delete from t_testlock
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> update t_testlock set a=3;
update t_testlock set a=3
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> truncate table t_testlock;
truncate table t_testlock
*
ERROR at line 1:
ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired
SQL> drop table t_testlock purge;
SQL> drop table t_testlock purge;
drop table t_testlock purge
*
ERROR at line 1:
ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired
SQL>
SQL> select * from t_testlock for update;
select * from t_testlock for update
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> select * from t_testlock;
A
----------
33
SQL> lock table t_testlock in row share mode;
Table(s) Locked.
SQL> lock table t_testlock in row exclusive mode;
lock table t_testlock in row exclusive mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> lock table t_testlock in share mode;
Table(s) Locked.
SQL> lock table t_testlock in share row exclusive mode;
lock table t_testlock in share row exclusive mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> lock table t_testlock in exclusive mode;
lock table t_testlock in exclusive mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
可見s鎖即表級共享鎖,鎖定級別最高,會阻塞其它會話的DML操作以及DDL操作,但不阻塞其它會話的SELECT查詢操作
會陰塞其它會話的行級及表級排它鎖
---------------------------------------------------------------
會話1
SQL> lock table t_testlock in share row exclusive mode;
Table(s) Locked.
SQL> select sid,type,id1,id2,decode(lmode,0,'none',1,'null',2,'row-s',3,'row-x',4,'s',5,'ssx',6,'x') lmode,request,ctime,block from v$lock where sid in (select sid from v$mystat where rownum=1) and type in ('TM');
SID TYPE ID1 ID2 LMODE REQUEST CTIME BLOCK
---------- ---- ---------- ---------- ---------- ---------- ---------- ----------
245 TM 74659 0 ssx 0 5 0
會話2
SQL> insert into t_testlock values(1);
insert into t_testlock values(1)
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> update t_testlock set a=3;
update t_testlock set a=3
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> delete from t_testlock;
delete from t_testlock
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> select * from t_testlock for update;
select * from t_testlock for update
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> truncate table t_testlock;
truncate table t_testlock
*
ERROR at line 1:
ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired
SQL> drop table t_testlock;
drop table t_testlock
*
ERROR at line 1:
ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired
SQL> select * from t_testlock;
A
----------
33
SQL>
SQL> lock table t_testlock in row share mode;
Table(s) Locked.
SQL> lock table t_testlock in row exclusive mode;
lock table t_testlock in row exclusive mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> lock table t_testlock in share mode;
lock table t_testlock in share mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> lock table t_testlock in share row exclusive mode;
lock table t_testlock in share row exclusive mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> lock table t_testlock in exclusive mode;
lock table t_testlock in exclusive mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
可見ssx鎖即共享行排它表級,鎖定級別要比s鎖即共享表鎖更高,會阻塞其它會話的DML操作以及DDL操作,但不阻塞其它會話的SELECT查詢操作
而且其它會話不能再持有ssx鎖,且僅不會阻塞行級共享鎖
而
---------------------------------------------------------------
會話1
SQL> lock table t_testlock in exclusive mode;
Table(s) Locked.
SQL> select sid,type,id1,id2,decode(lmode,0,'none',1,'null',2,'row-s',3,'row-x',4,'s',5,'ssx',6,'x') lmode,request,ctime,block from v$lock where sid in (select sid from v$mystat where rownum=1) and type in ('TM');
SID TYPE ID1 ID2 LMODE REQUEST CTIME BLOCK
---------- ---- ---------- ---------- ---------- ---------- ---------- ----------
245 TM 74659 0 x 0 3 0
會話2
SQL> insert into t_testlock values(1);
insert into t_testlock values(1)
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> delete from t_testlock;
delete from t_testlock
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> update t_testlock set a=3;
update t_testlock set a=3
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> truncate table t_testlock;
truncate table t_testlock
*
ERROR at line 1:
ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired
SQL> drop table t_testlock;
drop table t_testlock
*
ERROR at line 1:
ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired
SQL> select * from t_testlock for update;
select * from t_testlock for update
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> lock table t_testlock in row share mode;
lock table t_testlock in row share mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> lock table t_testlock in row exclusive mode;
lock table t_testlock in row exclusive mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> lock table t_testlock in share mode;
lock table t_testlock in share mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> lock table t_testlock in share row exclusive mode;
lock table t_testlock in share row exclusive mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL> lock table t_testlock in exclusive mode;
lock table t_testlock in exclusive mode
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
SQL>
SQL> select * from t_testlock;
A
----------
33
x鎖即表級排它鎖,是鎖定級別最高的鎖模式,它會阻塞其它會話的任何DML操作以及任何型別的加鎖操作
可見x鎖最牛比,鎖定級別最高,會阻塞其它會話的任何鎖模式
個人簡介:
8年oracle從業經驗,具備豐富的oracle技能,目前在國內北京某專業oracle服務公司從事高階技術顧問。
服務過的客戶:
中國電信
中國移動
中國聯通
中國電通
國家電網
四川達州商業銀行
湖南老百姓大藥房
山西省公安廳
中國郵政
北京302醫院
河北廊坊新奧集團公司
專案經驗:
中國電信3G專案AAA系統資料庫部署及最佳化
中國聯通CRM資料庫效能最佳化
中國移動10086電商平臺資料庫部署及最佳化
湖南老百姓大藥房ERR資料庫sql最佳化專案
四川達州商業銀行TCBS核心業務系統資料庫模型設計和RAC部署及最佳化
四川達州商業銀行TCBS核心業務系統後端批處理儲存過程功能模組編寫及最佳化
北京高鐵訊號監控系統RAC資料庫部署及最佳化
河南宇通客車資料庫效能最佳化
中國電信電商平臺核心採購模組表模型設計及最佳化
中國郵政儲蓄系統資料庫效能最佳化及sql最佳化
北京302醫院資料庫遷移實施
河北廊坊新奧data guard部署及最佳化
山西公安廳身份證審計資料庫系統故障評估
聯絡方式:
手機:18201115468
qq : 305076427
qq微博: wisdomone1
新浪微博:wisdomone9
qq群:275813900
itpub部落格名稱:wisdomone1 http://blog.itpub.net/9240380/
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9240380/viewspace-1816522/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- oracle lock鎖_v$lock_轉Oracle
- oracle lock系列一Oracle
- oracle v$lock詳解Oracle
- 【MySQL】MetaData Lock 之三MySql
- oracle鎖表問題處理 v$lock v$locked_objectOracleObject
- Oracle RAC Cache Fusion 系列十:Oracle RAC Enqueues And Lock Part 1OracleENQ
- oracle lock轉換及oracle deadlock死鎖系列一Oracle
- 通過dump library cache分析與學習oracle易碎解析鎖v$lock之系列十Oracle
- Oracle LOCK內部機制及最佳實踐系列(二)模擬RI鎖定導致阻塞的場景,並分析v$lockOracle
- Oracle動態效能檢視學習之v$lock & v$locked_objectOracleObject
- (轉)學習Oracle動態效能表-(3)V$LOCK,V$LOCKED_OBJECTOracleObject
- Oracle SQL效能最佳化系列講座之三(轉)OracleSQL
- 通過10046分析v$lock持鎖模式lmode之系列四模式
- webpack系列之三resolveWeb
- go微服務系列之三Go微服務
- 【DOC】VIEW: "V$LOCK" Reference NoteView
- [Oracle Script] LockOracle
- About Oracle LockOracle
- oracle enqueue lockOracleENQ
- Oracle Latch & LockOracle
- 【Oracle】Oracle常用EVENT之三Oracle
- ORACLE記憶體管理 之三 PGA v$sql_workarea_histogram v$pga_target_adviceOracle記憶體SQLHistogram
- 基於v$lock.block及request及dba_waiters或dba_blockers學習lock鎖系列七BloCAI
- v$session之小測試(一)_與v$lockSession
- iOS動畫系列之三:Core AnimationiOS動畫
- MySQL-lock(鎖)-v2.0MySql
- ORACLE LOCK,LATCH,PINOracle
- ORACLE LOCK MODE 1.2.3.4.5.6Oracle
- Oracle Lock Information QueriesOracleORM
- Dead lock - oracleOracle
- ORACLE lock 轉貼Oracle
- ORACLE查LOCK表Oracle
- v$lock之alter table drop column與alter table set unused column區別系列五
- 【轉】Oracle:檢查被鎖資料在V$LOCK中的情況Oracle
- RxJS 系列之三 - Operators 詳解JS
- Gradle for Android系列之三 tasksGradleAndroid
- 《Windows Communication Foundation之旅》系列之三Windows
- ORACLE V$lock檢視TYPE,ID1,ID2取值的含義Oracle