4.4.19 · HinglishDatabases

Locking — shared, exclusive, intent locks

2,150 words10 min readRead in English

4.4.19 · Coding › Databases


Locks ki zaroorat KYUN hai?

KYA lock karte hain: rows, pages, tables (granularity). KAISE correctness enforce karte hain: ek transaction ko read/write se pehle sahi lock acquire karna hoga, aur use hold karna hoga (usually commit tak, Two-Phase Locking ke under).


Do base lock modes

Compatibility — first principles se derive ki

Socho: kya do requests bina kisi anomaly ke saath exist kar sakti hain?

Existing →
Requested ↓
S X
S ✅ compatible ❌ conflict
X ❌ conflict ❌ conflict

KYUN har cell:

  • S–S ✅: do readers ek hi unchanged value dekhte hain → safe.
  • S–X ❌: writer wo data change kar dega jis par reader depend kar raha hai → unsafe.
  • X–S ❌: reader ek uncommitted/changing value dekhega → unsafe.
  • X–X ❌: do writers = lost update → unsafe.

Intent locks — multi-granularity problem

KAISE row locking kaam karta hai (rule): row par S lene ke liye → pehle table par IS lo. Row par X lene ke liye → pehle table par IX lo.

Full compatibility matrix (multi-granularity)

IS IX S SIX X
IS
IX
S
SIX
X
Figure — Locking — shared, exclusive, intent locks

Worked examples


Common mistakes


Recall Feynman: ek 12-saal ke bacche ko samjhao

Ek coloring book socho jo sab share karte hain. Agar kuch bachche sirf ek page dekhna chahte hain, toh sab saath crowd kar sakte hain — yeh hai shared lock. Lekin agar ek bacchi page color karna chahti hai, toh woh use le jaati hai taaki koi aur na dekhe na color kare — yeh hai exclusive lock. Ab book bahut badi hai, isliye page pakadne se pehle, tum cover par ek flag lagaate ho — "Hey, Main andar ek page use kar raha hoon!" — yeh hai intent lock. Yeh flag ek teacher ko, jo poori book chahta ho, turant pata deta hai ki yeh busy hai, bina har page palte.


Flashcards

Shared (S) lock kya permit karta hai?
Concurrent reads — multiple transactions ek hi item par S hold kar sakte hain; kisi bhi X ko block karta hai.
Exclusive (X) lock kya permit karta hai?
Ek single writer; us item par koi doosra S ya X nahi.
S aur X mein kaun se lock-mode pairs compatible hain?
Sirf S–S. S–X, X–S, X–X sab conflict karte hain.
Intent Shared (IS) lock kya hai?
Ek coarse-level flag jo announce karta hai ki transaction kisi finer-grained child par S lock hold karta/karega.
Intent Exclusive (IX) lock kya hai?
Ek coarse-level flag jo announce karta hai ki transaction kisi child par X lock hold karta/karega.
IX aur IX compatible kyun hain?
Yeh sirf neeche writes announce karte hain; do transactions alag-alag rows likh sakte hain, isliye coarse node par koi real conflict nahi.
IX, S ke saath incompatible kyun hai?
S poore node ko reading ke liye lock karta hai; IX matlab koi child likh raha ho sakta hai, jo reader ka data change kar sakta hai.
Intent locks kaunsi problem solve karte hain?
Yeh sab child rows () scan karne se bachate hain conflict detect karne ke liye; coarse-level check hai.
SIX ka matlab kya hai?
Poore node par Shared lock PLUS kuch children ke liye Intent-Exclusive: sab kuch padho, kuch rows likho.
Row par X lock lene se pehle pehle kya acquire karna hoga?
Uske parent (table) par IX (intent exclusive) lock, multi-granularity protocol ke anusaar.
Teen anomalies batao jo locking prevent karta hai.
Lost update, dirty read, non-repeatable read.

Connections

  • Two-Phase Locking — locks kab acquire/release hote hain serializability guarantee karne ke liye.
  • ACID Properties — Isolation mostly locking ke through implement hoti hai.
  • Isolation Levels — Read Committed / Repeatable Read / Serializable lock duration tune karte hain.
  • Deadlocks — conflicting order mein held locks cycles cause karte hain; detection & victims.
  • MVCC — read-locks ka alternative: readers snapshots dekhte hain S locks lene ki jagah.
  • Lock Granularity — row vs page vs table tradeoffs jo intent locks ko motivate karte hain.

Concept Map

causes

includes

includes

includes

prevents

split into

split into

compatible with

conflicts with

conflicts with

held until commit under

needs

flags table level for

avoids scanning all rows O n

Concurrent access

Anomalies

Lost update

Dirty read

Non-repeatable read

Lock mechanism

Shared lock S read

Exclusive lock X write

Two-Phase Locking

Multi-granularity locking

Intent locks

Row-level locks