SlideShare a Scribd company logo
1 of 21
Subject : Database Management System
Topic : Concurrency Control
CONTENTS
• What is Concurrency Control?
• Lock Based Protocol
• Two Phase Locking Protocol
• Deadlock
What is Concurrency Control?
The technique is used to protect data when multiple users
are accessing same data concurrently (same time) is
called concurrency control
Lock Based Protocol
• Lock is a mechanism to control concurrent access to data
item
• Data items can be locked in two modes:
1) Exclusive (X) Mode :- Data item can be both read as well as
written. X-lock is requested using lock-X instruction
2) Shared (S) Mode :- Data item can only be read. S-lock is
requested using lock-S instruction
• Lock requests are made to concurrency-control manager
• Transaction can proceed only after request is granted
Lock Based Protocol
• Lock-compatibility Matrix :
S X
S TRUE FALSE
X FALSE FALSE
• A transaction may be granted a lock on an item if the
requested lock is compatible with locks already held on the
item by other transaction
• Any number of transactions can hold shared locks on an
item, But if any transaction holds an exclusive on the item
no other transaction may hold any lock on the item
Lock Based Protocol
• If a lock cannot be granted, the requesting transaction is
made to wait till all incompatible locks held by other
transaction have been released. The lock is then granted.
• Example:
T1: lock-S(A); // Grant-S(A,T1)
read (A);
unlock(A);
lock-S(B); // Grant-S(B,T1)
read (B);
unlock(B);
display(A+B)
Pitfalls of Lock Based Protocol
• Locking as above is not sufficient to guarantee serializability -
if A and B get updated in-between the read of A and B, the
displayed sum would be wrong.
• A locking protocol is a set of rules followed by all transactions
while requesting and releasing locks. Locking protocols restrict
the set of possible schedules.
Pitfalls of Lock Based Protocol
• Neither T3 nor T4 can make progress — executing lock-S(B)
causes T4 to wait for T3 to release its lock on B, while executing
lock-X(A) causes T3 to wait for T4 to release its lock on A.
• Such a situation is called a deadlock.
– To handle a deadlock one of T3 or T4 must be rolled back
and its locks released.
Pitfalls of Lock Based Protocol
• Starvation is also possible if concurrency control manager is
badly designed. For example:
– A transaction may be waiting for an X-lock on an item, while
a sequence of other transactions request and are granted an
S-lock on the same item.
– The same transaction is repeatedly rolled back due to
deadlocks.
• Concurrency control manager can be designed to prevent
starvation.
Two Phase Locking Protocol
• This is a protocol which ensures conflict-serializable schedules.
• Phase 1: Growing Phase
– transaction may obtain locks
– transaction may not release locks
• Phase 2: Shrinking Phase
– transaction may release locks
– transaction may not obtain locks
• The protocol assures serializability. It can be proved that the
transactions can be serialized in the order of their lock points
(i.e. the point where a transaction acquired its final lock).
Two Phase Locking Protocol
• Two-phase locking does not ensure freedom from deadlocks
• Cascading roll-back is possible under two-phase locking. To
avoid this, follow a modified protocol called strict two-phase
locking. Here a transaction must hold all its exclusive locks till
it commits/aborts.
• Rigorous two-phase locking is even stricter: here all locks are
held till commit/abort. In this protocol transactions can be
serialized in the order in which they commit.
Deadlock
•A set of processes is deadlocked if each process in the set is
waiting for an event that only another process in the set can
cause.
Ti Tj
Four Conditions for Deadlock
A. Mutual exclusion condition
– each resource assigned to 1 process or is available
B. Hold and wait condition
– process holding resources can request additional
C. No preemption condition
– previously granted resources cannot forcibly taken away
D. Circular wait condition
– must be a circular chain of 2 or more processes
– each is waiting for resource held by next member of the chain
Strategies of Deadlock Handling
•Deadlock prevention.
Prevents deadlocks by restraining requests made to ensure
that at least one of the four deadlock conditions cannot occur.
•Deadlock avoidance.
Dynamically grants a resource to a process if the resulting
state is safe. A state is safe if there is at least one execution
sequence that allows all processes to run to completion.
•Deadlock detection and recovery.
That Allows deadlocks to form; then finds and breaks them.
Deadlock Avoidance
WAIT-DIE Rule :
If Ti requests a lock on a data item which is already locked
by Tj , then Ti is permitted to wait if ts (Ti ) < ts (Tj ). If
ts (Ti ) >ts (Tj ) , then Ti is aborted and restarted with the sa
me timestamp.
– if ts (Ti ) <ts (Tj ) then Ti waits else Ti dies
– non-preemptive: Ti never preempts Tj
– prefers younger transactions
Deadlock Avoidance
WOUND-WAIT Rule :
If Ti requests a lock on a data item which is already locked
by Tj , then Ti is permitted to wait iff ts(Ti)>ts(Tj). If
ts(Ti)<ts(Tj), then Tj is aborted and the lock is granted to Ti.
– if ts(Ti)<ts(Tj) then Tj is wounded else Ti waits
– preemptive: Ti preempts Tj if it is younger
– prefers older transactions
Deadlock Detection
• For deadlock detection, the system must provide
– An algorithm that examines the state of the system to detect whether a
deadlock has occurred
– And an algorithm to recover from the deadlock
• A detection-and-recovery scheme requires various kinds of
overhead
– Run-time costs of maintaining necessary information and executing the
detection algorithm
Database Concurrency Control Protocols

More Related Content

What's hot

What's hot (20)

Concurrency Control.
Concurrency Control.Concurrency Control.
Concurrency Control.
 
15. Transactions in DBMS
15. Transactions in DBMS15. Transactions in DBMS
15. Transactions in DBMS
 
serializability in dbms
serializability in dbmsserializability in dbms
serializability in dbms
 
Concurrency control
Concurrency controlConcurrency control
Concurrency control
 
Validation based protocol
Validation based protocolValidation based protocol
Validation based protocol
 
Transaction management and concurrency control
Transaction management and concurrency controlTransaction management and concurrency control
Transaction management and concurrency control
 
Databases: Locking Methods
Databases: Locking MethodsDatabases: Locking Methods
Databases: Locking Methods
 
Concurrency Control Techniques
Concurrency Control TechniquesConcurrency Control Techniques
Concurrency Control Techniques
 
12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS
 
Distributed database
Distributed databaseDistributed database
Distributed database
 
Transaction Properties in database | ACID Properties
Transaction Properties in database | ACID PropertiesTransaction Properties in database | ACID Properties
Transaction Properties in database | ACID Properties
 
Concurrent transactions
Concurrent transactionsConcurrent transactions
Concurrent transactions
 
Distributed DBMS - Unit 6 - Query Processing
Distributed DBMS - Unit 6 - Query ProcessingDistributed DBMS - Unit 6 - Query Processing
Distributed DBMS - Unit 6 - Query Processing
 
Distributed concurrency control
Distributed concurrency controlDistributed concurrency control
Distributed concurrency control
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
File organization 1
File organization 1File organization 1
File organization 1
 
Concurrency control
Concurrency  controlConcurrency  control
Concurrency control
 
Concurrency Control in Distributed Database.
Concurrency Control in Distributed Database.Concurrency Control in Distributed Database.
Concurrency Control in Distributed Database.
 
Acid properties
Acid propertiesAcid properties
Acid properties
 
Query optimization
Query optimizationQuery optimization
Query optimization
 

Similar to Database Concurrency Control Protocols

Concurrency control
Concurrency controlConcurrency control
Concurrency controlJaya Jeswani
 
Unit 4 Concurrency control.pptx dbms lovely
Unit 4 Concurrency control.pptx dbms lovelyUnit 4 Concurrency control.pptx dbms lovely
Unit 4 Concurrency control.pptx dbms lovelyPritishMajumdar3
 
Unit 4 Concurrency control.pptx dbms lovely
Unit 4 Concurrency control.pptx dbms lovelyUnit 4 Concurrency control.pptx dbms lovely
Unit 4 Concurrency control.pptx dbms lovelyPritishMajumdar3
 
Concurrency Control & Deadlock Handling
Concurrency Control & Deadlock HandlingConcurrency Control & Deadlock Handling
Concurrency Control & Deadlock HandlingMeghaj Mallick
 
concurrencycontrol from power pint pdf a
concurrencycontrol  from power pint pdf aconcurrencycontrol  from power pint pdf a
concurrencycontrol from power pint pdf aMdAyanParwez
 
deadlock handling
deadlock handlingdeadlock handling
deadlock handlingSuraj Kumar
 
Concurrency Control.pptx
Concurrency Control.pptxConcurrency Control.pptx
Concurrency Control.pptxVijaySourtha
 
Concurrency Control, Recovery, Case Studies
Concurrency Control, Recovery, Case StudiesConcurrency Control, Recovery, Case Studies
Concurrency Control, Recovery, Case StudiesPrabu U
 
Concurrency control PPT
Concurrency control PPTConcurrency control PPT
Concurrency control PPTShushrutGupta
 
DBMS (Deadlock, deadlock prevention, 2phase locking)
DBMS (Deadlock, deadlock prevention, 2phase locking)DBMS (Deadlock, deadlock prevention, 2phase locking)
DBMS (Deadlock, deadlock prevention, 2phase locking)Gaurav Solanki
 
Concurrency control!
Concurrency control!Concurrency control!
Concurrency control!Ashish K
 
Lock based protocols
Lock based protocolsLock based protocols
Lock based protocolsChethanMp7
 

Similar to Database Concurrency Control Protocols (20)

concurrency control
concurrency controlconcurrency control
concurrency control
 
Concurrency control
Concurrency controlConcurrency control
Concurrency control
 
Unit 4 Concurrency control.pptx dbms lovely
Unit 4 Concurrency control.pptx dbms lovelyUnit 4 Concurrency control.pptx dbms lovely
Unit 4 Concurrency control.pptx dbms lovely
 
Unit 4 Concurrency control.pptx dbms lovely
Unit 4 Concurrency control.pptx dbms lovelyUnit 4 Concurrency control.pptx dbms lovely
Unit 4 Concurrency control.pptx dbms lovely
 
Concurrency Control & Deadlock Handling
Concurrency Control & Deadlock HandlingConcurrency Control & Deadlock Handling
Concurrency Control & Deadlock Handling
 
concurrencycontrol from power pint pdf a
concurrencycontrol  from power pint pdf aconcurrencycontrol  from power pint pdf a
concurrencycontrol from power pint pdf a
 
UNIT II.pptx
UNIT II.pptxUNIT II.pptx
UNIT II.pptx
 
Concurrency control
Concurrency control Concurrency control
Concurrency control
 
deadlock handling
deadlock handlingdeadlock handling
deadlock handling
 
Cs501 concurrency
Cs501 concurrencyCs501 concurrency
Cs501 concurrency
 
Concurrency Control.pptx
Concurrency Control.pptxConcurrency Control.pptx
Concurrency Control.pptx
 
Concurrency Control, Recovery, Case Studies
Concurrency Control, Recovery, Case StudiesConcurrency Control, Recovery, Case Studies
Concurrency Control, Recovery, Case Studies
 
concurrency-control
concurrency-controlconcurrency-control
concurrency-control
 
Concurrency control PPT
Concurrency control PPTConcurrency control PPT
Concurrency control PPT
 
2 con control
2 con control2 con control
2 con control
 
deadlock
deadlockdeadlock
deadlock
 
DBMS (Deadlock, deadlock prevention, 2phase locking)
DBMS (Deadlock, deadlock prevention, 2phase locking)DBMS (Deadlock, deadlock prevention, 2phase locking)
DBMS (Deadlock, deadlock prevention, 2phase locking)
 
Concurrency control!
Concurrency control!Concurrency control!
Concurrency control!
 
Dbms
DbmsDbms
Dbms
 
Lock based protocols
Lock based protocolsLock based protocols
Lock based protocols
 

More from Janki Shah

Collections in .net technology (2160711)
Collections in .net technology (2160711)Collections in .net technology (2160711)
Collections in .net technology (2160711)Janki Shah
 
Gauss Elimination & Gauss Jordan Methods in Numerical & Statistical Methods
Gauss Elimination & Gauss Jordan Methods in Numerical & Statistical MethodsGauss Elimination & Gauss Jordan Methods in Numerical & Statistical Methods
Gauss Elimination & Gauss Jordan Methods in Numerical & Statistical MethodsJanki Shah
 
File Management in Operating System
File Management in Operating SystemFile Management in Operating System
File Management in Operating SystemJanki Shah
 
Addressing in Computer Networks
Addressing in Computer NetworksAddressing in Computer Networks
Addressing in Computer NetworksJanki Shah
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure Janki Shah
 
Number system in Digital Electronics
Number system in Digital ElectronicsNumber system in Digital Electronics
Number system in Digital ElectronicsJanki Shah
 
Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Janki Shah
 
Compiler in System Programming/Code Optimization techniques in System Program...
Compiler in System Programming/Code Optimization techniques in System Program...Compiler in System Programming/Code Optimization techniques in System Program...
Compiler in System Programming/Code Optimization techniques in System Program...Janki Shah
 
Sorting in Linear Time in Analysis & Design of Algorithm
Sorting in Linear Time in Analysis & Design of AlgorithmSorting in Linear Time in Analysis & Design of Algorithm
Sorting in Linear Time in Analysis & Design of AlgorithmJanki Shah
 

More from Janki Shah (9)

Collections in .net technology (2160711)
Collections in .net technology (2160711)Collections in .net technology (2160711)
Collections in .net technology (2160711)
 
Gauss Elimination & Gauss Jordan Methods in Numerical & Statistical Methods
Gauss Elimination & Gauss Jordan Methods in Numerical & Statistical MethodsGauss Elimination & Gauss Jordan Methods in Numerical & Statistical Methods
Gauss Elimination & Gauss Jordan Methods in Numerical & Statistical Methods
 
File Management in Operating System
File Management in Operating SystemFile Management in Operating System
File Management in Operating System
 
Addressing in Computer Networks
Addressing in Computer NetworksAddressing in Computer Networks
Addressing in Computer Networks
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Number system in Digital Electronics
Number system in Digital ElectronicsNumber system in Digital Electronics
Number system in Digital Electronics
 
Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++
 
Compiler in System Programming/Code Optimization techniques in System Program...
Compiler in System Programming/Code Optimization techniques in System Program...Compiler in System Programming/Code Optimization techniques in System Program...
Compiler in System Programming/Code Optimization techniques in System Program...
 
Sorting in Linear Time in Analysis & Design of Algorithm
Sorting in Linear Time in Analysis & Design of AlgorithmSorting in Linear Time in Analysis & Design of Algorithm
Sorting in Linear Time in Analysis & Design of Algorithm
 

Recently uploaded

Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture designssuser87fa0c1
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage examplePragyanshuParadkar1
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 

Recently uploaded (20)

Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture design
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage example
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 

Database Concurrency Control Protocols

  • 1. Subject : Database Management System Topic : Concurrency Control
  • 2. CONTENTS • What is Concurrency Control? • Lock Based Protocol • Two Phase Locking Protocol • Deadlock
  • 3. What is Concurrency Control? The technique is used to protect data when multiple users are accessing same data concurrently (same time) is called concurrency control
  • 4.
  • 5. Lock Based Protocol • Lock is a mechanism to control concurrent access to data item • Data items can be locked in two modes: 1) Exclusive (X) Mode :- Data item can be both read as well as written. X-lock is requested using lock-X instruction 2) Shared (S) Mode :- Data item can only be read. S-lock is requested using lock-S instruction • Lock requests are made to concurrency-control manager • Transaction can proceed only after request is granted
  • 6. Lock Based Protocol • Lock-compatibility Matrix : S X S TRUE FALSE X FALSE FALSE • A transaction may be granted a lock on an item if the requested lock is compatible with locks already held on the item by other transaction • Any number of transactions can hold shared locks on an item, But if any transaction holds an exclusive on the item no other transaction may hold any lock on the item
  • 7. Lock Based Protocol • If a lock cannot be granted, the requesting transaction is made to wait till all incompatible locks held by other transaction have been released. The lock is then granted. • Example: T1: lock-S(A); // Grant-S(A,T1) read (A); unlock(A); lock-S(B); // Grant-S(B,T1) read (B); unlock(B); display(A+B)
  • 8. Pitfalls of Lock Based Protocol • Locking as above is not sufficient to guarantee serializability - if A and B get updated in-between the read of A and B, the displayed sum would be wrong. • A locking protocol is a set of rules followed by all transactions while requesting and releasing locks. Locking protocols restrict the set of possible schedules.
  • 9. Pitfalls of Lock Based Protocol • Neither T3 nor T4 can make progress — executing lock-S(B) causes T4 to wait for T3 to release its lock on B, while executing lock-X(A) causes T3 to wait for T4 to release its lock on A. • Such a situation is called a deadlock. – To handle a deadlock one of T3 or T4 must be rolled back and its locks released.
  • 10. Pitfalls of Lock Based Protocol • Starvation is also possible if concurrency control manager is badly designed. For example: – A transaction may be waiting for an X-lock on an item, while a sequence of other transactions request and are granted an S-lock on the same item. – The same transaction is repeatedly rolled back due to deadlocks. • Concurrency control manager can be designed to prevent starvation.
  • 11.
  • 12. Two Phase Locking Protocol • This is a protocol which ensures conflict-serializable schedules. • Phase 1: Growing Phase – transaction may obtain locks – transaction may not release locks • Phase 2: Shrinking Phase – transaction may release locks – transaction may not obtain locks • The protocol assures serializability. It can be proved that the transactions can be serialized in the order of their lock points (i.e. the point where a transaction acquired its final lock).
  • 13. Two Phase Locking Protocol • Two-phase locking does not ensure freedom from deadlocks • Cascading roll-back is possible under two-phase locking. To avoid this, follow a modified protocol called strict two-phase locking. Here a transaction must hold all its exclusive locks till it commits/aborts. • Rigorous two-phase locking is even stricter: here all locks are held till commit/abort. In this protocol transactions can be serialized in the order in which they commit.
  • 14.
  • 15. Deadlock •A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause. Ti Tj
  • 16. Four Conditions for Deadlock A. Mutual exclusion condition – each resource assigned to 1 process or is available B. Hold and wait condition – process holding resources can request additional C. No preemption condition – previously granted resources cannot forcibly taken away D. Circular wait condition – must be a circular chain of 2 or more processes – each is waiting for resource held by next member of the chain
  • 17. Strategies of Deadlock Handling •Deadlock prevention. Prevents deadlocks by restraining requests made to ensure that at least one of the four deadlock conditions cannot occur. •Deadlock avoidance. Dynamically grants a resource to a process if the resulting state is safe. A state is safe if there is at least one execution sequence that allows all processes to run to completion. •Deadlock detection and recovery. That Allows deadlocks to form; then finds and breaks them.
  • 18. Deadlock Avoidance WAIT-DIE Rule : If Ti requests a lock on a data item which is already locked by Tj , then Ti is permitted to wait if ts (Ti ) < ts (Tj ). If ts (Ti ) >ts (Tj ) , then Ti is aborted and restarted with the sa me timestamp. – if ts (Ti ) <ts (Tj ) then Ti waits else Ti dies – non-preemptive: Ti never preempts Tj – prefers younger transactions
  • 19. Deadlock Avoidance WOUND-WAIT Rule : If Ti requests a lock on a data item which is already locked by Tj , then Ti is permitted to wait iff ts(Ti)>ts(Tj). If ts(Ti)<ts(Tj), then Tj is aborted and the lock is granted to Ti. – if ts(Ti)<ts(Tj) then Tj is wounded else Ti waits – preemptive: Ti preempts Tj if it is younger – prefers older transactions
  • 20. Deadlock Detection • For deadlock detection, the system must provide – An algorithm that examines the state of the system to detect whether a deadlock has occurred – And an algorithm to recover from the deadlock • A detection-and-recovery scheme requires various kinds of overhead – Run-time costs of maintaining necessary information and executing the detection algorithm