Database Isolation

Isolation is the database-level property that controls how and when changes are made and if they become visible to each other. One of the goals of isolation is to allow multiple transactions occurring at the same time without impacting each other’s execution.


Capture.PNG
  • Lost update: a transaction write is ignored by another transaction. write->write

  • Dirty read: a transaction reads an object written by another transaction which again modifies the object afterwards. write->read

  • Non repeatable read: a transaction reads an object twice and gets different values. read->write

Mode of Lock

Capture.PNG

Dependency relation

  • In a history sequence H, consisting of tuples of the form
    (T, action, object).
    If T1 and T2 are transactions, O is an object, and there exists
    indexes i and j such that i < j, H[i] involves action a1 on O by
    T1, (i.e. H[i] = (T1,a1,O)) H[j] involves action a2 on O by
    T2 (i.e. H(j) = (T2, a2,O)) and there are no
    H(k) = (T’,WRITE,O) for i < k < j, the
    dependency of T1 on T2 can be written as:
    (T1, O, T2)
  • A history is said to be isolated if it is equal to a serial history.
    A serial history is history that is resulted as a consequence of
    running transactions sequentially one at a time. N transactions
    can result in a maximum of N! serial histories.
    Capture1.PNG

    Wormhole theorem: A history is isolated if and only if it has no
    wormholes.

Isolation Concept:

  • A transactions is a sequence of READ, WRITE, SLOCK, XLOCK actions on objects ending with COMMIT or ROLLBACK.
  • A transaction is well formed if each READ, WRITE and UNLOCK operation is covered earlier by a corresponding lock operation.
  • A history is legal if does not grant conflicting grants.
  • A transaction is two phase if its all lock operations precede its unlock operations
  • Locking theorem: If all transactions are well formed (READ, WRITE and UNLOCK operation is covered earlier by a corresponding lock operation) and two-phased, then any legal (does not grant conflicting grants) history will be isolated.
  • Locking theorem (Converse): If a transaction is not well formed or is not two-phase, then it is possible to write another transaction such that it is a wormhole.
  • Rollback theorem: An update transaction that does an UNLOCK and then does a ROLLBACK is not two phase.

Degree of Isolation



image.png

Granularity of Locking

  • Why

  1. Problem:
    C1 queries DB for all suspects with blue eyes red hair and C2 inserts new suspect with blue eyes & red hair, C1 may lock all appropriate suspects, but this won't prevent C2 from inserting new suspect, violating consistency
  2. Phantoms and predicate locks
phantom lock

sometimes called an anti-insert lock, is placed on a scan position to prevent the subsequent creation of phantom rows by other transactions. When a phantom lock is acquired, it prevents other transactions from inserting a row into a table immediately before the row that is anti-insert locked. A phantom lock is a long-term lock, that is held until the end of the transaction.

Predicate lock

Predicate locking is a method of locking based upon logical conditions as a solution to so-called phantom rows, such that if a transaction has issued SELECT ... WHERE age > 18, no other transaction would be able to add new rows where age greater than 18, delete rows where age greater than 18, or update an existing row so that it would conflict with the initial transaction.

  • It is NP complete
  • Hard to capture predicates
  1. Solution

Granular Lock:

Build hierarchy and locks can be taken at any level will automatically grant the locks on its descendants




Update lock mode

  • The situation here could cause deadlock, as after anyone of the T1, T2, T3 acquire share lock, no one could require the exclusive lock.


  • Solution 1


  • Solution 2


    Capture.PNG

Optimistic Locking

  • Allows you to lower the isolation level that you use in an application so that fewer locks are placed on the database
  • It is effective two phase locking with short-duration of locking.
  • It allows more applications to run concurrently against the database, and potentially increase the throughput of your applications.


Snapshot Isolation

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市射众,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件伞插,死亡現(xiàn)場離奇詭異垄潮,居然都是意外死亡奇瘦,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進(jìn)店門验靡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來倍宾,“玉大人,你說我怎么就攤上這事胜嗓「咧埃” “怎么了?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵辞州,是天一觀的道長怔锌。 經(jīng)常有香客問我,道長变过,這世上最難降的妖魔是什么埃元? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮媚狰,結(jié)果婚禮上岛杀,老公的妹妹穿的比我還像新娘。我一直安慰自己崭孤,他們只是感情好类嗤,可當(dāng)我...
    茶點故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著辨宠,像睡著了一般遗锣。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上嗤形,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天精偿,我揣著相機(jī)與錄音,去河邊找鬼。 笑死笔咽,一個胖子當(dāng)著我的面吹牛墓阀,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播拓轻,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼斯撮,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了扶叉?” 一聲冷哼從身側(cè)響起勿锅,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎枣氧,沒想到半個月后溢十,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡达吞,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年张弛,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片酪劫。...
    茶點故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡吞鸭,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出覆糟,到底是詐尸還是另有隱情刻剥,我是刑警寧澤,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布滩字,位于F島的核電站造虏,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏麦箍。R本人自食惡果不足惜漓藕,卻給世界環(huán)境...
    茶點故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望挟裂。 院中可真熱鬧享钞,春花似錦、人聲如沸话瞧。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽交排。三九已至,卻和暖如春饵筑,著一層夾襖步出監(jiān)牢的瞬間埃篓,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工根资, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留架专,地道東北人同窘。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像部脚,于是被迫代替她去往敵國和親想邦。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,047評論 2 355

推薦閱讀更多精彩內(nèi)容