隔離級別(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 |