ORACLE foundation of the oracle lock (oracle lock mode) Detailed

ORACLE locks in the following modes:
0: none
. 1: null null
2: Row-S shared row (RS): shared table lock, Sub Share 
. 3: Row-row X-exclusive (RX): used to modify the line, Sub exclusive 
. 4: share shared lock (S): stop other DML operations, share
. 5: S / row-X-shared line exclusive (SRX): stop other transaction operations, share / Sub exclusive 
. 6: exclusive exclusive (X): independent access use, exclusive

The type 1.oracle provided can be queried v $ lock_type according to the type, are the two most we usually contact

 code show as below Copy the code

select * from v$lock_type where type in ('TM','TX')


See description, can probably learn other kinds of locks to synchronize access to information .TM is the object used, TX is and transaction-related.

3. To know there are two concepts:

. (1) to lock the lock data, i.e., row level locking, only one of: exclusive lock exclusive (the ROW)
(2) locking lock on the table, i.e. the locking of the lock metadata metadata (Table), a total of five. species:

The RS 2: Row Share
. 3 the RX: Row exclusive
. 4 S: Share
. 5 the SRX: Share Row exclusive
. 6 X-: exclusive4 The concepts oracle of online documentation.

From this table we can find at least two things. The first is that each database operation corresponds to what kind of lock (refer to the middle of the row), and the second is between each lock, whether if you encounter after conflict, the so-called conflict is whether the cause of the current database operations ram live, where Y *, said that if the two operations are locked on the same line, there will be conflict, and then after the completion of a pre-operation will wait for the completion of the operation, otherwise there would have been rammed; if not on the same line, so it will not conflict, post-operation does not wait for an example to illustrate: suppose now operates as a: records for id = 1 is update, while B operation. to: record id on = 2 is deleted, according to the table description, when operating in the a-level locks is TM RX, TX level only one of X, B will be in a TM is the RX level locks, TX level is only one X, and according to the table illustrates, when the RX RX encountered, if two operating extraordinary record, so it will not conflict, so the two AB will operate in accordance with their respective first add a TM lock, coupled with a TX lock, then the smooth implementation of their respective operations, will not tamper live. If the operation of transposition record id B 1, then two operations recorded for the same record, the TM lock will show the conflict, so the operation will wait for A B operation is completed submission (ie TX A lock is released after), B then generates a corresponding TX lock and a lock TM again to complete the operation, otherwise it will always stay ram, TX lock release wait a.

The methods of dynamic performance views:

select * from v$lock_type where type in ('TM','TX');

 code show as below Copy the code
select * from v$lock;
select * from v$transaction;

Highlight what v $ lock view:

To set a scene: in the session A record in a table for update after the update is not submitted, to delete the same record in the tables are in session B

 

 code show as below Copy the code
Session A:
SQL> create table ttt as select * from dba_objects where rownum<=10;

Table has been created.

 code show as below Copy the code

SQL> update ttt set object_name='TEST' where object_id=20;

1 line has been updated.

 code show as below Copy the code

SQL>

Session B:
SQL> delete from ttt where object_id=20;

At this time, because A did not commit, so B would have been rammed and then open a live session C, related to the query v $ lock view

 

 code show as below Copy the code

SQL> select * from v$lock where type in ('TM','TX');

ADDR     KADDR           SID TY        ID1        ID2      LMODE    REQUEST      CTIME      BLOCK
-------- -------- ---------- -- ---------- ---------- ---------- ---------- ---------- ----------
CA4244F4 CA424508        147 TX     393242        563          0          6        270          0
C8E7F704 C8E7F71C        140 TM      55156          0          3          0        301          0
C8E7F7C8 C8E7F7E0        147 TM      55156          0          3          0        270          0
C8ED3C38 C8ED3D54        140 TX     393242        563          6          0        301          1

You can clearly see two sid sid is generated session 140 to lock, resulting in a TX (TM) and a lock, the lock mode TM (LMODE) = 3, (3 to RX:. Matches row exclusive control table and when the operation is update, resulting in RX lock); for sid of session 147 also produces a TM and a TX lock, TM lock mode (LMODE) = 3 (3 to RX: control matching row exclusive and tables when the operation is a delete, generate lock RX), while the TX lock mode (LMODE) = 0, are representatives waiting for a lock. v $ lock_type definition above, we can see, the TM type lock, ID1 represents the object_id, query dba_objects can easily draw the TTT is locked object obj. bLOCK from the last one (the block does not represent a block, but on behalf of obstruction) = 1 can also be seen, sid = 140 in after a session TX lock after generated, this record modification operations are also found, so BLOCK + 1, represented by block other operations while this record operation.

In this case, the query select * from v $ transaction; view, may be associated with the information obtained

We describe v $ lock_type from the TX lock, can be aware of TX and related matters. So before you can view information about the v $ lock TX lock, you can see the value of ADDR v $ transaction value is the same. Even ID1 can be calculated from the value, which segment is locked: the removal of the TX and ID1 to modulo 2 to the power 16, get information:

 code show as below Copy the code

SQL> select 393242 / 65536, mod(393242, 65536) from dual;

393242/65536 MOD(393242,65536)
------------ -----------------
  6.00039673                26

可以神奇的发现和v$transaction中的XIDUSN和XIDSLOT对应上了!

6.最后补充一个操作,创建索引的时候会生成的锁操作:


首先将ttt表插入很多数据

 代码如下 复制代码
SQL> insert into ttt select * from dba_objects;
SQL> commit;

提交完成。

 代码如下 复制代码

SQL> select count(*) from ttt;

  COUNT(*)
----------
   1739045

接着再在改表上创建一个索引

 代码如下 复制代码
SQL> create index idx_ttt on ttt(object_id);

创建索引的同时,查询v$lock表

可以发现在创建索引的会生成2个TM锁,锁类别分别为4和3,我们查询这2个TM分别锁定的是什么对象:

根据查询结果发现lmode=4的object_id为55160的对象对应的是TTT这个表,LMODE=4对应的是TM的S锁

总结

数字越大锁级别越高, 影响的操作越多。

1级锁有:Select,有时会在v$locked_object出现。
2级锁有:Select for update,Lock For Update,Lock Row Share 
select for update当对话使用for update子串打开一个游标时,所有返回集中的数据行都将处于行级(Row-X)独占式锁定,其他对象只能查询这些数据行,不能进行update、delete或select for update操作。
3级锁有:Insert, Update, Delete, Lock Row Exclusive
没有commit之前插入同样的一条记录会没有反应, 因为后一个3的锁会一直等待上一个3的锁, 我们必须释放掉上一个才能继续工作。
4级锁有:Create Index, Lock Share
locked_mode为2,3,4不影响DML(insert,delete,update,select)操作, 但DDL(alter,drop等)操作会提示ora-00054错误。
00054, 00000, "resource busy and acquire with NOWAIT specified"
// *Cause: Resource interested is busy.
// *Action: Retry if necessary.
5级锁有:Lock Share Row Exclusive 
具体来讲有主外键约束时update / delete ... ; 可能会产生4,5的锁。
6级锁有:Alter table, Drop table, Drop Index, Truncate table, Lock Exclusive

 

Guess you like

Origin www.cnblogs.com/yaoyangding/p/12013938.html