事務隔離級別(isolation level)

概述

  • 隔離屬性是原子性、一致性、隔離性和持久性 (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

相關文章
相關標籤/搜索