Professional Documents
Culture Documents
What Is Concurrency Control? - Purpose of Concurrency Control - Why We Need Concurrency Control? - Classification - Deadlock
What Is Concurrency Control? - Purpose of Concurrency Control - Why We Need Concurrency Control? - Classification - Deadlock
• Lost Updates.
• Uncommitted Data.
• Inconsistent retrievals.
CLASSIFICATION
• Lock-Based Protocols
• Timestamp-Based
Protocols
• Validation-Based
Protocols
Lock Based Protocol
• Lock-compatibility Matrix :
S X
S TRUE FALSE
X FALSE FALSE
lock-X(B);
read (B);
B = B+100;
write(B);
unlock(B);
Problems with simple locking mechanism
• Incorrect Results / Inconsistent database state
• Deadlock
• 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 (2PL)
• A Transaction locks an object before using it.
• When an object is locked by another transaction, the requesting transaction
m u s t wait.
• When a transaction releases a lock, it may not request another lock.
Lock point
Obtain lock
Release lock
No. of locks
Phase Phase
1 2
BEGIN END
T1 T2 T3
T1 T3 T2
LP
LP
LP
Problems with simple 2-Phase Locking
Protocol
• Two-phase locking does not ensure freedom from deadlocks
• Unnecessary or early lock. T1 T2
• Cascading roll-back is possible under two-phase locking. Lock- X(B)
read(B)
T1 T2 write(B)
Lock-X(A) Lock-S(A)
read(A) read(A)
Write(A) Lock-X(B)
Lock-X(B) Lock-X(A) .
. Lock-S(A) . .
. . .
. .
T1 T2 T3
Continue… Lock- X(A)
read(A)
write(A)
Unlock(A)
. Lock-X(A)
. read(A)
. write(A)
Unlock(A)
. Lock-X(A)
. read(A)
. write(A)
Unlock(A)
.
.
.
Lock upgrade/downgrade
• Lock-S(x) Lock-X(x)
• Lock-X(x) Lock-S(x)
Conservative 2-phase locking
• All the data item lock before starting of any transaction.
• No growing phase.
• T1 A,B,C
• No deadlock
Problems with conservative 2-Phase Locking
Protocol
• Practical implementation is very difficult
Start End
Commit
Unlock X(A)
• No cascading rollback.
Problems with Strict 2-Phase Locking
Protocol
• Deadlock is possible.
Rigorous 2-phase locking Protocol
• Transaction T does not release any of the lock until transaction T commits or
abort.
• No cascading rollback.
Problems with Rigorous 2-Phase Locking
Protocol
• Deadlock is possible.
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.