现在的位置: 首页 > 综合 > 正文

锁 死锁 阻塞 Latch 等待 详解

2012年10月15日 ⁄ 综合 ⁄ 共 4434字 ⁄ 字号 评论关闭

 

锁这东西是纯概念性的东西,但是作用又非常大。 以前整理过两篇文章,今天又看了点书,觉得还不是很清楚。 就重新整理了下。 在想要不要把以前的文章删掉,这样在blog里是比较简介的,但后来又一想。 这些连接在其他的地方可能被引用了。 就决定还是保留着了,也算见证自己成长的一个过程。

 

ORACLE 锁机制

http://blog.csdn.net/tianlesoftware/archive/2009/10/20/4696896.aspx

 

oracle 锁问题的解决

http://blog.csdn.net/tianlesoftware/archive/2009/10/28/4733630.aspx

 

 

关于Oracle 锁的内容,可以参考

Data Concurrency and ConsistencyOracle 并发与一致)

http://download.oracle.com/docs/cd/E11882_01/server.112/e10713/consist.htm#CNCPT1339

 

 

一.锁(Lock

 

1.1 锁的概念

数据库是一个多用户使用的共享资源。当多个用户并发地存取数据时,在数据库中就会产生多个事务同时存取同一数据的情况。若对并发操作不加控制就可能会读取和存储不正确的数据,破坏数据库的一致性。

 

加锁是实现数据库并发控制的一个非常重要的技术。当事务在对某个数据对象进行操作前,先向系统发出请求,对其加锁。加锁后事务就对该数据对象有了一定的控制,在该事务释放锁之前,其他的事务不能对此数据对象进行更新操作。

 

Oracle 数据库中,它并不是对某个表加上锁或者某几行加上锁,锁是以数据块的一个属性存在的 也就是说,每个数据块本身就存储着自己数据块中数据的信息,这个地方叫ITLInterested Transaction List),凡是在这个数据块上有活动的事务,它的信息就会记录在这里面供后续的操作查询,一保证事务的一致性。

 

1.2 锁的分类

 

1.2.1. 按用户与系统划分,可以分为自动锁与显示锁

a) 自动锁(Automatic Locks):当进行一项数据库操作时,缺省情况下,系统自动为此数据库操作获得所有有必要的锁。自动锁分DML锁,DDL锁,system locks

b) 显示锁(Manual Data Locks):某些情况下,需要用户显示的锁定数据库操作要用到的数据,才能使数据库操作执行得更好,显示锁是用户为数据库对象设定的。

         Oracle Database performs locking automatically to ensure data concurrency, data integrity, and statement-level read consistency. However, you can manually override the Oracle Database default locking mechanisms. Overriding the default locking is useful in situations such as the following:

Applications require transaction-level read consistency or repeatable reads.

In this case, queries must produce consistent data for the duration of the transaction, not reflecting changes by other transactions. You can achieve transaction-level read consistency by using explicit locking, read-only transactions, serializable transactions, or by overriding default locking.

 

Applications require that a transaction have exclusive access to a resource so that the transaction does not have to wait for other transactions to complete.

 

You can override Oracle Database automatic locking at the session or transaction level. At the session level, a session can set the required transaction isolation level with the ALTER SESSION statement. At the transaction level, transactions that include the following SQL statements override Oracle Database default locking:

1The SET TRANSACTION ISOLATION LEVEL statement

2The LOCK TABLE statement (which locks either a table or, when used with views, the base tables)

3The SELECT ... FOR UPDATE statement

 

Locks acquired by the preceding statements are released after the transaction ends or a rollback to savepoint releases them.

If Oracle Database default locking is overridden at any level, then the database administrator or application developer should ensure that the overriding locking procedures operate correctly. The locking procedures must satisfy the following criteria: data integrity is guaranteed, data concurrency is acceptable, and deadlocks are not possible or are appropriately handled.

 

 

1.2.2. 按锁级别划分,可分为: 排它锁(Exclusive Locks,即X锁)和共享锁(Share Locks,即S锁)

a) 共享锁( S ) 共享锁使一个事务对特定数据库资源进行共享访问——另一事务也可对此资源进行访问或获得相同共享锁。共享锁为事务提供高并发性,但如拙劣的事务设计+共享锁容易造成死锁或数据更新丢失。

b) 排它锁( X) 事务设置排它锁后,该事务单独获得此资源,另一事务不能在此事务提交之前获得相同对象的共享锁或排它锁。

 

1.2.3 按操作划分,可分为DML锁(data locks,数据锁)、DDL锁(data dictionary lock)和 System Locks

 

Lock

Description

DML Locks

Protect data. For example, table locks lock entire tables, while row locks lock selected rows. See "DML Locks".

DDL Locks

Protect the structure of schema objects—for example, the dictionary definitions of tables and views. See "DDL Locks".

System Locks

Protect internal database structures such as data files. Latches, mutexes, and internal locks are entirely automatic. See "System Locks".

 

1.2.3.1  DML

 

这部分内容,可以参考Oracle 联机文档

Automatic Locks in DML Operations

http://download.oracle.com/docs/cd/E11882_01/server.112/e10592/ap_locks001.htm#SQLRF55502

        

DML锁用于控制并发事务中的数据操纵,保证数据的一致性和完整性。DML 锁主要用于保护并发情况下的数据完整性。 它又分为:

 1TM锁(表级锁)

2TX锁(事务锁或行级锁)

 

Oracle执行DML语句时,系统自动在所要操作的表上申请TM类型的锁。当TM锁获得后,系统再自动申请TX类型的锁,并将实际锁定的数据行的锁标志位进行置位。这样在事务加锁前检查TX锁相容性时就不用再逐行检查锁标志,而只需检查TM锁模式的相容性即可,大大提高了系统的效率。

 

         在数据行上只有X锁(排他锁)。在Oracle数据库中,当一个事务首次发起一个DML语句时就获得一个TX锁,该锁保持到事务被提交或回滚。当两个或多个会话在表的同一条记录上执行 DML语句时,第一个会话在该条记录上加锁,其他的会话处于等待状态。当第一个会话提交后,TX锁被释放,其他会话才可以加锁。

 

Oracle数据库发生TX锁等待时,如果不及时处理常常会引起Oracle数据库挂起,或导致死锁的发生,产生ORA-600的错误。这些现象都会对实际应用产生极大的危害,如长时间未响应,大量事务失败等。

 

 

TM(表锁)

当事务获得行锁后,此事务也将自动获得该行的表锁(共享锁),以防止其它事务进行DDL语句影响记录行的更新。事务也可以在进行过程中获得共享锁或排它锁,只有当事务显示使用LOCK TABLE 句显示的定义一个排它锁时,事务才会获得表上的排它锁,也可使用LOCK TABLE显示的定义一个表级的共享锁(LOCK TABLE具体用法请参考相关文档)

TM锁包括了SSSXSX 等多种模式,在数据库中用06来表示。不同的SQL操作产生不同类型的TM锁。

 

TM锁类型表

锁模式

锁描述

解释

SQL操作

0

none

 

 

1

NULL

Select

2

SS(Row-S)

行级共享锁,其他对象只能查询这些数据行

Select for updateLock for updateLock row share

3

SX(Row-X)

行级排它锁,在提交前不允许做DML操作

InsertUpdate DeleteLock row share

4

S(Share)

共享锁: 阻止其他DML操作

Create indexLock share

5

SSX(S/Row-X)

共享行级排它锁:阻止其他事务操作

Lock share row exclusive

6

X(Exclusive)

排它锁:独立访问使用

Alter tableDrop ableDrop index

抱歉!评论已关闭.