首页 > 代码库 > 锁
锁
select type,name from v$lock_type;
SQL> create table t(id int primary key);
表已创建。
SQL> insert into t values(1);
已创建 1 行。
SQL> commit;
提交完成。
SQL> update t set id=2 where id =1;
已更新 1 行。
SQL> select distinct sid from v$mystat;
SID
----------
71
SQL>
C:\Users\Administrator>sqlplus / as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on 星期六 12月 28 09:52:04 2013
Copyright (c) 1982, 2010, Oracle. All rights reserved.
连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> update t set id=2 where id=1;
连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> select distinct sid from v$mystat;
SID
----------
140
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where type in(‘TM‘,‘TX‘) order by 1,2;
SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
13 TM 76350 0 3 0 0
13 TX 327703 7274 0 6 0
71 TM 76350 0 3 0 0
71 TX 327703 7274 6 0 1
SQL> select object_name from dba_objects where object_id =76350;
OBJECT_NAME
--------------------------------------------------------------------------------
T
ID1 + ID2就是回滚段上的那个号
SQL> select sid,event from v$session_wait where sid in(13,71);
SID EVENT
---------- ----------------------------------------------------------------
13 enq: TX - row lock contention
71 SQL*Net message from client
SQL> select * from t;
ID
----------
2
SQL> insert into t values(3);
已创建 1 行。
SQL>
SQL> insert into t values(1);
已创建 1 行。
SQL> insert into t values(3);
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where type in(‘TM‘,‘TX‘) order by 1,2;
SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
13 TM 76350 0 3 0 0
13 TX 196620 7299 0 4 0
13 TX 655364 7058 6 0 0
71 TM 76350 0 3 0 0
71 TX 196620 7299 6 0 1
SQL> select * from t;
ID
----------
2
3
1
SQL> select * from t where id=1 for update;
ID
----------
1
SQL> update t set id=4 where id=1;
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where type in(‘TM‘,‘TX‘) order by 1,2;
SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
71 TM 76350 0 3 0 0
71 TX 196622 7299 6 0 0
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where type in(‘TM‘,‘TX‘) order by 1,2;
SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
13 TM 76350 0 3 0 0
13 TX 196622 7299 0 6 0
71 TM 76350 0 3 0 0
71 TX 196622 7299 6 0 1
SQL> create table p(id int primary key);
表已创建。
SQL> create table c(id references p(id));
表已创建。
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where type in(‘TM‘,‘TX‘) order by 1,2;
SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
71 TM 76354 0 3 0 0
71 TM 76356 0 3 0 0
71 TX 65540 7128 6 0 0
SQL> select object_name from dba_objects where object_id in (76354,76356);
OBJECT_NAME
--------------------------------------------------------------------------------
P
C
许多Oracle锁又被称为"enqueue locks"。从英文一般的构词规则来看,enqueue应该是动词(翻译成"排队"或"加入队列"),但在这里,经常被用做形容词(可以翻译成"队列式的")、甚至是名词(可以翻译成"队列")。计算机软件中的"锁"往往是用来解决稀缺资源的争用问题,而此种问题更完善的解决还会用到排队机制(如同人类社会解决稀缺资源争用问题一样,比如众多顾客到银行办理各种业务),所以"锁"(lock)与"队列"(enqueue)这两个词在这一领域的某些场合可以互换。下面还是看一个具体的例子: 在SESS#1(SID为10)在DEPT表上获得Row Exclusive锁,而SESS#2(SID为12)在DEPT表上申请Share锁,被SESS#1阻塞。 来源: <http://hi.baidu.com/irphzqgscabnoqd/item/67a0939d02dd98df1b49dfd4> |