概述
- 隔离属性是原子性、一致性、隔离性和持久性 (ACID) 这四个属性之一,逻辑工做单元必须具有这四个属性才能称为事务。
- 隔离级别定义了事务与事务之间的隔离程度。
- 隔离级别与并发性是互为矛盾的:隔离程度越高,数据库的并发性越差;隔离程度越低,数据库的并发性越好。
隔离级别
ANSI/ISO SQL92
标准定义了一些数据库操做的隔离级别:
- 未提交读(read uncommitted): 当事务A更新某条数据时,不允许其余事务来更新该数据,但能够读取。
- 提交读(read committed): 当事务A更新某条数据时,不允许其余事务进行任何操做包括读取,但事务A读取时,其余事务能够进行读取、更新
- 重复读(repeatable read): 当事务A更新数据时,不允许其余事务进行任何操做,但当事务A进行读取时,其余事务只能读取,不能更新。
- 序列化(serializable): 最严格的隔离级别,事务必须依次进行。
读取现象
经过一些现象,能够反映出隔离级别的效果。这些现象有: html
- 更新丢失(lost update):当系统容许两个事务同时更新同一数据时,发生更新丢失。
- 脏读(dirty read):当一个事务读取另外一个事务还没有提交的修改时,产生脏读。
- 不重复读(nonrepeatable read):同一查询在同一事务中屡次进行,因为其余提交事务所作的修改或删除,每次返回不一样的结果集,此时发生非重复读。(A transaction rereads data it has previously read and finds that another committed transaction has modified or deleted the data. )
- 幻读(phantom read):同一查询在同一事务中屡次进行,因为其余提交事务所作的插入操做,每次返回不一样的结果集,此时发生幻像读。(A transaction reexecutes a query returning a set of rows that satisfies a search condition and finds that another committed transaction has inserted additional rows that satisfy the condition. )
隔离级别与读取现象
隔离级别 |
脏读 Dirty Read |
不可重复读取 NonRepeatable Read |
幻读 Phantom Read |
未受权读取/未提交读 read uncommitted |
可能发生 |
可能发生 |
可能发生 |
受权读取/提交读 read committed |
- |
可能发生 |
可能发生 |
重复读 read repeatable |
- |
- |
可能发生 |
序列化 serializable |
- |
- |
- |
常见数据库的默认级别
数据库 |
默认隔离级别 |
Oracle |
read committed |
SqlServer |
read committed |
MySQL(InnoDB) |
Read-Repeatable |
来源: http://epub.itpub.net/3/4.htm
http://technet.microsoft.com/zh-cn/library/ms171885.aspx
http://msdn.microsoft.com/en-us/library/ms175909.aspx
http://docs.oracle.com/cd/B10500_01/server.920/a96524/c21cnsis.htm
http://dev.mysql.com/doc/refman/5.0/en/innodb-transaction-model.html mysql