共計 3637 個字符,預(yù)計需要花費(fèi) 10 分鐘才能閱讀完成。
本篇內(nèi)容介紹了“數(shù)據(jù)庫事務(wù)隔離的級別”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓丸趣 TV 小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
1 概述與背景
這是數(shù)據(jù)庫事務(wù)原理和工程實(shí)踐系列文章的第一篇,本文主要在 Jim Gray 的論文 A Critique of ANSI SQL Isolation Levels 基礎(chǔ)上分析關(guān)系數(shù)據(jù)庫的事務(wù)隔離級別標(biāo)準(zhǔn)和不同隔離級別情況下的行為。第 2 節(jié)主要討論 ANSI 標(biāo)準(zhǔn)的下的隔離級別,第 3 節(jié)主要討論基于悲觀鎖實(shí)現(xiàn)的事務(wù)隔離級別,第 4 節(jié)主要討論基于多版本技術(shù)的事務(wù)隔離,最后總結(jié)排序本文討論到的各個隔離級別。
ACID 是關(guān)系數(shù)據(jù)庫的一組重要特性,其中 Isolation(隔離性)描述了數(shù)據(jù)庫允許多個并發(fā)事務(wù)同時對其數(shù)據(jù)進(jìn)行讀寫和修改的能力,隔離性可以防止多個事務(wù)并發(fā)時由于交錯執(zhí)行而導(dǎo)致數(shù)據(jù)的不一致。在最極端的情況下,數(shù)據(jù)庫完全串行化執(zhí)行每一個事務(wù),所有事務(wù)之間遵守全序關(guān)系,在這種情況下,不存在并發(fā)事務(wù)間的隔離問題,但是在實(shí)際工程實(shí)踐中,處于對數(shù)據(jù)庫性能吞吐量的考慮,允許多個事務(wù)之間按照一定的規(guī)則,打破串行話的全序關(guān)系,ANSI SQL Isolation Levels 即規(guī)定了這種“規(guī)則”,通過將隔離性劃分為 4 個級別,來換取多層級的事務(wù)間并發(fā)能力(即數(shù)據(jù)庫的吞吐能力)。
注,本文內(nèi)容融入了作者個人的理解,并沒有嚴(yán)格遵守 A Critique of ANSI SQL Isolation Levels 原文的內(nèi)容;其中 cursor stability 隔離級別將在后續(xù)文章中討論,快照隔離級別與 ANSI 標(biāo)準(zhǔn)異象的比較也有所不同。
2 ANSI 事務(wù)隔離級別
ANSI SQL-92 標(biāo)準(zhǔn) (http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt) 將數(shù)據(jù)庫并發(fā)事務(wù)間的隔離性行為劃分為 3 種 異象(phenomena),從低到高的自然語言定義依次為:
P1 臟讀 (Dirty read): SQL-transaction T1 modifies a row. SQL- transaction T2 then reads that row before T1 performs a COMMIT. If T1 then performs a ROLLBACK, T2 will have read a row that was never committed and that may thus be considered to have never existed.
P2 不可重復(fù)讀 (Non-repeatable read): SQL-transaction T1 reads a row. SQL- transaction T2 then modifies or deletes that row and performs a COMMIT. If T1 then attempts to reread the row, it may receive the modified value or discover that the row has been deleted.
P3 幻讀 (Phantom): SQL-transaction T1 reads the set of rows N that satisfy some search condition . SQL-transaction T2 then executes SQL-statements that generate one or more rows that satisfy the search condition used by SQL-transaction T1. If SQL-transaction T1 then repeats the initial read with the same search condition , it obtains a different collection of rows.
通過依次禁止這三種異象,ANSI 確定了 4 種標(biāo)準(zhǔn)隔離級別,如下表所示:
級別 P1(臟讀)P2(不可重復(fù)讀)P3(幻讀)Read Uncommitted 允許允許允許 Read Committed 禁止允許允許 Repeatable Read 禁止
禁止
允許 (Anomaly) Serializable 禁止
禁止
禁止
Note: The exclusion of these penomena or SQL-transactions executing at isolation level SERIALIZABLE is a consequence of the requirement that such transactions be serializable.
如標(biāo)準(zhǔn)文檔所述,禁止了 P1/P2/P3 異象的事務(wù)即滿足 Serializable 級別,但矛盾的是,標(biāo)準(zhǔn)文檔中對 Serializable 又做了如下說明:
The execution of concurrent SQL-transactions at isolation level SERIALIZABLE is guaranteed to be serializable. A serializable execution is defined to be an execution of the operations of concurrently executing SQL-transactions that produces the same effect as some serial execution of those same SQL-transactions
它要求多個并發(fā)事務(wù)執(zhí)行的效果與某種串行化執(zhí)行的效果一致,但是僅僅禁止 P1/P2/P3 異象,并不一定能夠保證“serial execution”的效果,因此論文中將 ANSI Serializable 稱為 Anomaly Serializable。
P1/P2/P3 的形式化描述
根據(jù)標(biāo)準(zhǔn)文檔的定義,可以將這三種異象使用形式化語言描述如下,稱為 A1/A2/A3(其中 w1[x]表示事務(wù) 1 寫入記錄 x,r1 表示事務(wù) 1 讀取記錄 x,c1 表示事務(wù) 1 提交,a1 表示事務(wù) 1 回滾,r1[P]表示事務(wù) 1 按照謂詞 P 的條件讀取若干條記錄,w1[y in P]表示事務(wù) 1 寫入記錄 y 滿足謂詞 P 的條件):
A1 臟讀:w1[x] … r2[x] … (a1 and c2 in any order)
A2 不可重復(fù)讀:r1[x] … w2[x] … c2 … r1[x] … c1
A3 幻讀:r1[P] … w2[y in P] … c2 … r1[P] … c1
上述 A1/A2/A3 形式化描述,根據(jù)標(biāo)準(zhǔn)定義的 P1/P2/P3 異象的自然語言描述轉(zhuǎn)化而來,但是 ANSI 標(biāo)準(zhǔn)定義的異象只針對了單個記錄或謂詞描述,對于多條記錄需滿足業(yè)務(wù)一致性的場景并未能覆蓋(比如兩個賬戶間轉(zhuǎn)賬要求余額總和不變),舉例如下:
H1:r1[x=50]w1[x=10] r2[x=10]r2[y=50] c2 r1[y=50]w1[y=90] c1
事務(wù) 1 執(zhí)行賬戶 x 向賬戶 y 轉(zhuǎn)賬 40,事務(wù) 2 讀取到了進(jìn)行到了一半的事務(wù) 1(Read Uncommitted),破壞了余額總和的一致性
因?yàn)槭聞?wù) 1 并未回滾,H1 的行為并不符合 A1 的形式化定義
H2:r1[x=50]
Read LockWrite Lock
Locking
Read Uncommited
none required
Well-formed Writes,
Long duration Write locks
Locking
Read CommitedWell-formed Reads,
Short duration read lock
Well-formed Writes,
Long duration Write locks
Locking
Repeatable ReadWell-formed Reads,
Long duration data-item Read locks,
Short duration Read Predicate locks
Well-formed Writes
Long duration Write locks
Locking
SerializableWell-formed Reads,
Long duration Read locks
Well-formed Writes
Long duration Write locks
將 locking 標(biāo)記的四種隔離級別與 ANSI 隔離級別對比:
Well-formed Reads, Short duration read lockRead Uncommitted Read Committed (Repeatable Read Snapshot) Serializable
本文首先介紹了 ANSI 基于“異象”的隔離級別標(biāo)準(zhǔn),并分析了其狹義和廣義的描述;然后介紹了基于鎖的隔離級別標(biāo)準(zhǔn),與 ANSI 隔離級別進(jìn)行了比較;最后分析快照隔離級別,在 ANSI 隔離級別標(biāo)準(zhǔn)基礎(chǔ)上,提出了兩種新的“異象”,得出快照隔離在幾種標(biāo)準(zhǔn)隔離級別特性中的位置。
“數(shù)據(jù)庫事務(wù)隔離的級別”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注丸趣 TV 網(wǎng)站,丸趣 TV 小編將為大家輸出更多高質(zhì)量的實(shí)用文章!