结论
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/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/9240380/viewspace-1816522/