隔离级别(isolation level)定义了事务与事务之间的隔离程度。隔离级别与并发性是互为矛盾的:隔离程度越高,数据库的并发性越差;隔离程度越低,数据库的并发性越好。
ANSI/ISO SQL92标准定义了一些数据库操作的隔离级别:
未提交读(read uncommitted)
提交读(read committed)
重复读(repeatable read)
序列化(serializable)
通过一些现象,可以反映出隔离级别的效果。这些现象有:
1. 更新丢失(lost update):当系统允许两个事务同时更新同一数据时,发生更新丢失。
2. 脏读(dirty read):当一个事务读取另一个事务尚未提交的修改时,产生脏读。
3. 非重复读(nonrepeatable read):同一查询在同一事务中多次进行,由于其他提交事务所做的修改或删除,每次返回不同的结果集,此时发生非重复读。(A transaction rereads data it has previously read and finds that another committed transaction has modified or deleted the data. )
4. 幻像(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 | Possible | Possible | Possible |
Read committed | Not possible | Possible | Possible |
Repeatable read | Not possible | Not possible | Possible |
Serializable | Not possible | Not possible | Not possible |