UGC-NET, GATE, NIELIT (O/A/B Level), KVS/NVS Computer Teacher, PGDCA, BCA, MCA, B-Tech व All India IT Exam विशेष नोट्स: इस अध्याय में हम Two-Phase Locking (2PL): Strict, Rigorous 2PL and Deadlock Handling (टू-फेज लॉकिंग (2PL): स्ट्रिक्ट, रिगोरस 2PL और डेडलॉक निवारण) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और राष्ट्रीय स्तर की परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Two-Phase Locking (2PL) is a concurrency control protocol in DBMS that guarantees conflict serializability by separating lock acquisition (Growing Phase) from lock release (Shrinking Phase). Strict and Rigorous 2PL prevent cascading rollbacks, while Deadlock Handling resolves circular wait conditions.
अध्याय सार: टू-फेज लॉकिंग (2PL) DBMS में एक समवर्ती नियंत्रण प्रोटोकॉल है जो लॉक अधिग्रहण (ग्रोइंग फेज) को लॉक रिलीज (श्रिंकिंग फेज) से अलग करके कॉन्फ्लिक्ट सीरियलाइजेबिलिटी की गारंटी देता है। स्ट्रिक्ट और रिगोरस 2PL कैस्केडिंग रोलबैक को रोकते हैं।
Two-Phase Locking (2PL): Strict, Rigorous 2PL and Deadlock Handling: Detailed Notes & Architecture (टू-फेज लॉकिंग (2PL): स्ट्रिक्ट, रिगोरस 2PL और डेडलॉक निवारण)
1. Basic 2PL: Growing and Shrinking Phases (बेसिक 2PL: ग्रोइंग और श्रिंकिंग फेज)
[EN] In Basic 2PL, every transaction operates in two phases. During the Growing Phase, a transaction may acquire Shared (S) or Exclusive (X) locks but cannot release any lock. Once it releases its first lock, it enters the Shrinking Phase, where it can only release locks and cannot acquire new ones. While Basic 2PL ensures conflict serializability, it can still suffer from cascading rollbacks and deadlocks.
[HI] बेसिक 2PL में, प्रत्येक ट्रांजेक्शन दो चरणों में काम करता है। ग्रोइंग फेज के दौरान, एक ट्रांजेक्शन शेयर्ड (S) या एक्सक्लूसिव (X) लॉक प्राप्त कर सकता है लेकिन कोई लॉक रिलीज नहीं कर सकता। एक बार जब यह अपना पहला लॉक रिलीज करता है, तो यह श्रिंकिंग फेज में प्रवेश करता है।
2. Strict 2PL and Rigorous 2PL (स्ट्रिक्ट 2PL और रिगोरस 2PL)
[EN] To prevent dirty reads and cascading rollbacks, Strict 2PL requires a transaction to hold all its Exclusive (X) locks until it commits or aborts. Rigorous 2PL goes a step further by holding both Shared (S) and Exclusive (X) locks until commit/abort. Most commercial RDBMS engines use Rigorous 2PL because it guarantees strict, cascadeless schedules.
[HI] डर्टी रीड्स और कैस्केडिंग रोलबैक को रोकने के लिए, स्ट्रिक्ट 2PL को ट्रांजेक्शन के कमिट या एबॉर्ट होने तक अपने सभी एक्सक्लूसिव (X) लॉक रखने की आवश्यकता होती है। रिगोरस 2PL कमिट/एबॉर्ट तक S और X दोनों लॉक रखता है।
3. Deadlock Prevention: Wait-Die and Wound-Wait (डेडलॉक रोकथाम: वेट-डाई और वाउंड-वेट)
[EN] 2PL can cause deadlocks when T1 waits for a lock held by T2 and vice versa. Timestamp-based deadlock prevention avoids this: in Wait-Die (non-preemptive), an older transaction waits for a younger one, while a younger transaction dies (aborts) if it needs a lock held by an older one. In Wound-Wait (preemptive), an older transaction wounds (aborts) a younger one, while a younger one waits.
[HI] 2PL डेडलॉक का कारण बन सकता है। टाइमस्टैम्प-आधारित डेडलॉक रोकथाम इसे टालती है: वेट-डाई (नॉन-प्रीएम्प्टिव) में, एक पुराना ट्रांजेक्शन युवा की प्रतीक्षा करता है, जबकि युवा मर जाता है। वाउंड-वेट (प्रीएम्प्टिव) में, पुराना ट्रांजेक्शन युवा को घायल (एबॉर्ट) कर देता है।
4. Deadlock Detection: Wait-For Graph (डेडलॉक डिटेक्शन: वेट-फॉर ग्राफ)
[EN] Instead of preventing deadlocks, a DBMS can allow them and detect them periodically using a Wait-For Graph (WFG). Nodes represent active transactions, and a directed edge Ti -> Tj means Ti is waiting for a lock held by Tj. A deadlock exists if and only if the Wait-For Graph contains a cycle, which is resolved by aborting a victim transaction.
[HI] डेडलॉक को रोकने के बजाय, DBMS उन्हें वेट-फॉर ग्राफ (WFG) का उपयोग करके समय-समय पर पता लगा सकता है। नोड्स सक्रिय ट्रांजेक्शन का प्रतिनिधित्व करते हैं, और यदि ग्राफ में कोई चक्र (cycle) है, तो डेडलॉक मौजूद है।
Syntax, Formula & Technical Example (Two-Phase Locking (2PL): Strict, Rigorous 2PL and Deadlock Handling)
-- Transaction T1 under Strict 2PL
BEGIN TRANSACTION;
SELECT balance FROM accounts WHERE id = 1 FOR UPDATE; -- Acquires X-lock
UPDATE accounts SET balance = balance - 500 WHERE id = 1;
-- X-lock is held until COMMIT/ROLLBACK to prevent dirty reads
COMMIT; -- Releases all X-locks
Two-Phase Locking (2PL): Strict, Rigorous 2PL and Deadlock Handling Quick Reference & Comparison Table
| 2PL Variant | Lock Release Rule | Key Exam Fact |
|---|---|---|
| Basic 2PL | Release any lock in Shrinking Phase | Ensures conflict serializability; allows cascading rollbacks |
| Strict 2PL | Hold X-locks until Commit/Abort | Prevents cascading rollbacks and dirty reads |
| Rigorous 2PL | Hold S & X locks until Commit/Abort | Used in most RDBMS; easiest to implement |
| Conservative 2PL | Acquire all locks before execution | Deadlock-free, but reduces concurrency |
Two-Phase Locking (2PL): Strict, Rigorous 2PL and Deadlock Handling Top 20 Bilingual MCQs with Answers
Q1. What does Basic Two-Phase Locking (2PL) guarantee?
(बेसिक टू-फेज लॉकिंग (2PL) क्या गारंटी देता है?)
(A) Conflict serializability
(B) Deadlock freedom
(C) Zero-latency execution
(D) No cascading rollbacks
Correct Answer: (A)
[EN] Every schedule produced by 2PL is conflict serializable.
[HI] 2PL द्वारा निर्मित प्रत्येक शेड्यूल कॉन्फ्लिक्ट सीरियलाइजेबल होता है।
Q2. In 2PL, what is the 'Lock Point' of a transaction?
(2PL में, किसी ट्रांजेक्शन का 'लॉक पॉइंट' क्या होता है?)
(A) The first SQL SELECT
(B) The moment the database starts
(C) The point where the transaction has acquired all its locks and begins releasing
(D) The point of deadlock
Correct Answer: (C)
[EN] The Lock Point marks the transition from the Growing Phase to the Shrinking Phase.
[HI] लॉक पॉइंट ग्रोइंग फेज से श्रिंकिंग फेज में संक्रमण को चिह्नित करता है।
Q3. How does Strict 2PL prevent cascading rollbacks?
(स्ट्रिक्ट 2PL कैस्केडिंग रोलबैक को कैसे रोकता है?)
(A) By running transactions sequentially
(B) By disabling logging
(C) By banning Shared locks
(D) By holding all Exclusive (X) locks until commit or abort
Correct Answer: (D)
[EN] Holding X-locks until commit ensures no other transaction reads uncommitted data.
[HI] कमिट तक X-लॉक रखने से यह सुनिश्चित होता है कि कोई अन्य ट्रांजेक्शन अनकमिटेड डेटा न पढ़े।
Q4. Which 2PL variant holds BOTH Shared (S) and Exclusive (X) locks until commit or abort?
(कौन सा 2PL वेरिएंट कमिट या एबॉर्ट तक शेयर्ड (S) और एक्सक्लूसिव (X) दोनों लॉक रखता है?)
(A) Rigorous 2PL
(B) Basic 2PL
(C) Optimistic 2PL
(D) Strict 2PL
Correct Answer: (A)
[EN] Rigorous 2PL is the most common protocol in commercial RDBMS.
[HI] रिगोरस 2PL वाणिज्यिक RDBMS में सबसे आम प्रोटोकॉल है।
Q5. Which 2PL variant is completely free from deadlocks?
(कौन सा 2PL वेरिएंट डेडलॉक से पूरी तरह मुक्त है?)
(A) Rigorous 2PL
(B) Strict 2PL
(C) Basic 2PL
(D) Conservative (Static) 2PL
Correct Answer: (D)
[EN] Conservative 2PL requires acquiring all needed locks before starting, preventing circular wait.
[HI] कंजर्वेटिव 2PL को शुरू करने से पहले सभी आवश्यक लॉक प्राप्त करने की आवश्यकता होती है।
Q6. In the Wait-Die deadlock prevention scheme, what happens if an older transaction Ti requests a lock held by a younger transaction Tj?
(वेट-डाई डेडलॉक रोकथाम योजना में, यदि एक पुराना ट्रांजेक्शन Ti युवा ट्रांजेक्शन Tj द्वारा रखे गए लॉक का अनुरोध करता है तो क्या होता है?)
(A) Ti waits
(B) Both commit
(C) Ti dies (aborts)
(D) Tj dies (aborts)
Correct Answer: (A)
[EN] Wait-Die is non-preemptive: older waits for younger; younger dies if blocked by older.
[HI] वेट-डाई नॉन-प्रीएम्प्टिव है: पुराना युवा की प्रतीक्षा करता है; युवा मर जाता है।
Q7. In the Wound-Wait scheme, what happens if an older transaction Ti requests a lock held by a younger transaction Tj?
(वाउंड-वेट योजना में, यदि एक पुराना ट्रांजेक्शन Ti युवा ट्रांजेक्शन Tj द्वारा रखे गए लॉक का अनुरोध करता है तो क्या होता है?)
(A) Tj upgrades to X-lock
(B) Ti waits for Tj
(C) Ti wounds (aborts) Tj
(D) Ti aborts itself
Correct Answer: (C)
[EN] Wound-Wait is preemptive: older preempts (wounds) younger.
[HI] वाउंड-वेट प्रीएम्प्टिव है: पुराना युवा को एबॉर्ट कर देता है।
Q8. How does a DBMS detect deadlocks using a Wait-For Graph?
(DBMS वेट-फॉर ग्राफ का उपयोग करके डेडलॉक का पता कैसे लगाता है?)
(A) By checking disk space
(B) By checking for a cycle in the directed graph
(C) By sorting timestamps
(D) By counting total locks
Correct Answer: (B)
[EN] A cycle in the Wait-For Graph is a necessary and sufficient condition for a deadlock.
[HI] वेट-फॉर ग्राफ में एक चक्र डेडलॉक के लिए आवश्यक और पर्याप्त शर्त है।
Q9. What is 'Starvation' (Livelock) in locking protocols?
(लॉकिंग प्रोटोकॉल में 'स्टार्वेशन' (लाइवलॉक) क्या है?)
(A) The database runs out of RAM
(B) Two transactions commit simultaneously
(C) A transaction is repeatedly aborted or bypassed and never makes progress
(D) A lock is held forever by a crashed server
Correct Answer: (C)
[EN] Assigning original timestamps on restart prevents starvation in Wait-Die/Wound-Wait.
[HI] पुनरारंभ पर मूल टाइमस्टैम्प सौंपने से स्टार्वेशन को रोका जा सकता है।
Q10. Can a transaction acquire a new lock during the Shrinking Phase of 2PL?
(क्या कोई ट्रांजेक्शन 2PL के श्रिंकिंग फेज के दौरान नया लॉक प्राप्त कर सकता है?)
(A) Yes, only Shared locks
(B) Yes, if it has high priority
(C) No
(D) Yes, only Exclusive locks
Correct Answer: (C)
[EN] Once any lock is released, no new locks may be acquired.
[HI] एक बार कोई लॉक रिलीज हो जाने के बाद, कोई नया लॉक प्राप्त नहीं किया जा सकता।
Q11. What is 'Lock Conversion' (Upgrading) in 2PL?
(2PL में 'लॉक कन्वर्जन' (अपग्रेडिंग) क्या है?)
(A) Moving locks to disk
(B) Deleting a lock table
(C) Upgrading a Shared (S) lock to an Exclusive (X) lock during the Growing Phase
(D) Converting X-lock to S-lock in Growing Phase
Correct Answer: (C)
[EN] Lock upgrades are only permitted during the Growing Phase.
[HI] लॉक अपग्रेड केवल ग्रोइंग फेज के दौरान अनुमत हैं।
Q12. Which SQL clause explicitly acquires an Exclusive row lock in PostgreSQL/MySQL?
(PostgreSQL/MySQL में कौन सा SQL क्लॉज स्पष्ट रूप से एक्सक्लूसिव रो लॉक प्राप्त करता है?)
(A) SELECT ... DISTINCT
(B) SELECT ... FOR UPDATE
(C) SELECT ... READ ONLY
(D) GROUP BY
Correct Answer: (B)
[EN] FOR UPDATE locks selected rows against concurrent updates.
[HI] FOR UPDATE चयनित पंक्तियों को समवर्ती अपडेट के विरुद्ध लॉक करता है।
Q13. Does Strict 2PL prevent deadlocks?
(क्या स्ट्रिक्ट 2PL डेडलॉक को रोकता है?)
(A) Only for read-only queries
(B) Only in NoSQL
(C) Yes, always
(D) No, deadlocks can still occur
Correct Answer: (D)
[EN] Strict 2PL prevents cascading rollbacks, not deadlocks.
[HI] स्ट्रिक्ट 2PL कैस्केडिंग रोलबैक को रोकता है, डेडलॉक को नहीं।
Q14. Which of the following is true about Conflict Serializability?
(कॉन्फ्लिक्ट सीरियलाइजेबिलिटी के बारे में निम्नलिखित में से कौन सा सत्य है?)
(A) 2PL allows non-serializable schedules
(B) 2PL produces conflict-serializable schedules, but not all conflict-serializable schedules can be produced by 2PL
(C) 2PL and conflict serializability are identical sets
(D) 2PL is slower than serial execution
Correct Answer: (B)
[EN] 2PL is a sufficient (not necessary) condition for conflict serializability.
[HI] 2PL कॉन्फ्लिक्ट सीरियलाइजेबिलिटी के लिए एक पर्याप्त (आवश्यक नहीं) शर्त है।
Q15. When a deadlock is detected, how does the DBMS resolve it?
(जब डेडलॉक का पता चलता है, तो DBMS इसे कैसे हल करता है?)
(A) By choosing a victim transaction and rolling it back
(B) By restarting the OS
(C) By granting X-locks to all
(D) By ignoring the cycle
Correct Answer: (A)
[EN] The victim is typically the transaction with the least work done so far.
[HI] विक्टिम आमतौर पर वह ट्रांजेक्शन होता है जिसने अब तक सबसे कम काम किया है।
Q16. In a Wait-For Graph, what does a directed edge T1 -> T2 signify?
(वेट-फॉर ग्राफ में, एक निर्देशित किनारा T1 -> T2 क्या दर्शाता है?)
(A) T2 is waiting for T1
(B) T1 has committed before T2
(C) T1 is waiting for a lock currently held by T2
(D) T1 and T2 are in different databases
Correct Answer: (C)
[EN] Edges point from the waiting transaction to the lock-holding transaction.
[HI] किनारे प्रतीक्षारत ट्रांजेक्शन से लॉक-होल्डिंग ट्रांजेक्शन की ओर इशारा करते हैं।
Q17. Within Two-Phase Locking (2PL): Strict, Rigorous 2PL and Deadlock Handling, which 2pl variant directly maps to: "Release any lock in Shrinking Phase"?
(Two-Phase Locking (2PL): Strict, Rigorous 2PL and Deadlock Handling में कौन सा 2PL Variant सीधे "Release any lock in Shrinking Phase" से संबंधित है?)
(A) Strict 2PL
(B) Conservative 2PL
(C) Basic 2PL
(D) Rigorous 2PL
Correct Answer: (C)
[EN] In Two-Phase Locking (2PL): Strict, Rigorous 2PL and Deadlock Handling, 'Basic 2PL' handles 'Release any lock in Shrinking Phase' (Ensures conflict serializability; allows cascading rollbacks).
[HI] Two-Phase Locking (2PL): Strict, Rigorous 2PL and Deadlock Handling में 'Basic 2PL' का कार्य/विवरण 'Release any lock in Shrinking Phase' (Ensures conflict serializability; allows cascading rollbacks) है।
Q18. Which option accurately describes 'Strict 2PL' in Two-Phase Locking (2PL): Strict, Rigorous 2PL and Deadlock Handling?
(Two-Phase Locking (2PL): Strict, Rigorous 2PL and Deadlock Handling के संदर्भ में 'Strict 2PL' की सही विशेषता कौन सी है?)
(A) Hold X-locks until Commit/Abort
(B) Hold S & X locks until Commit/Abort
(C) Acquire all locks before execution
(D) Release any lock in Shrinking Phase
Correct Answer: (A)
[EN] 'Strict 2PL' provides 'Hold X-locks until Commit/Abort' — key point: Prevents cascading rollbacks and dirty reads.
[HI] 'Strict 2PL' का मुख्य विवरण 'Hold X-locks until Commit/Abort' है (Prevents cascading rollbacks and dirty reads)।
Q19. In Two-Phase Locking (2PL): Strict, Rigorous 2PL and Deadlock Handling, identify the correct parameter for the specification: "Hold S & X locks until Commit/Abort".
(Two-Phase Locking (2PL): Strict, Rigorous 2PL and Deadlock Handling में "Hold S & X locks until Commit/Abort" विवरण किस विकल्प के लिए सही है?)
(A) Rigorous 2PL
(B) Conservative 2PL
(C) Strict 2PL
(D) Basic 2PL
Correct Answer: (A)
[EN] 'Rigorous 2PL' corresponds to 'Hold S & X locks until Commit/Abort' (Used in most RDBMS; easiest to implement).
[HI] 'Rigorous 2PL' का संबंध 'Hold S & X locks until Commit/Abort' (Used in most RDBMS; easiest to implement) से है।
Q20. Considering Two-Phase Locking (2PL): Strict, Rigorous 2PL and Deadlock Handling, what key behavior defines 'Conservative 2PL'?
(Two-Phase Locking (2PL): Strict, Rigorous 2PL and Deadlock Handling में 'Conservative 2PL' की मुख्य भूमिका या मान क्या है?)
(A) Hold S & X locks until Commit/Abort
(B) Acquire all locks before execution
(C) Release any lock in Shrinking Phase
(D) Hold X-locks until Commit/Abort
Correct Answer: (B)
[EN] 'Conservative 2PL' represents 'Acquire all locks before execution' (Deadlock-free, but reduces concurrency).
[HI] 'Conservative 2PL' का अर्थ/मान 'Acquire all locks before execution' है (Deadlock-free, but reduces concurrency)।
Two-Phase Locking (2PL): Strict, Rigorous 2PL and Deadlock Handling — Exam FAQs
Q1: Why do most databases use Rigorous 2PL instead of Basic 2PL?
अधिकांश डेटाबेस बेसिक 2PL के बजाय रिगोरस 2PL का उपयोग क्यों करते हैं?
[EN] Basic 2PL allows dirty reads and cascading rollbacks, whereas Rigorous 2PL guarantees cascadeless, recoverable schedules.
[HI] बेसिक 2PL डर्टी रीड्स और कैस्केडिंग रोलबैक की अनुमति देता है, जबकि रिगोरस 2PL कैस्केडलेस शेड्यूल की गारंटी देता है।
Q2: What is the difference between Deadlock Prevention and Deadlock Detection?
डेडलॉक रोकथाम और डेडलॉक डिटेक्शन में क्या अंतर है?
[EN] Prevention (Wait-Die/Wound-Wait) stops cycles before they form; Detection allows locks freely and periodically checks a Wait-For Graph.
[HI] रोकथाम चक्र बनने से पहले ही रोक देती है; डिटेक्शन स्वतंत्र रूप से लॉक की अनुमति देता है और समय-समय पर वेट-फॉर ग्राफ की जांच करता है।
Q3: Can 2PL cause starvation?
क्या 2PL स्टार्वेशन का कारण बन सकता है?
[EN] Yes, if a transaction is repeatedly chosen as a deadlock victim or blocked by continuous shared locks.
[HI] हाँ, यदि किसी ट्रांजेक्शन को बार-बार डेडलॉक विक्टिम के रूप में चुना जाता है।
Post a Comment
Post a Comment