Ch15 Concurrency Control
Ch15 Concurrency Control
Concurrency Control
Concurrency Control in Database Management System is a procedure of
managing simultaneous operations without conflicting with each other. It ensures
that Database transactions are performed concurrently and accurately to
produce correct results without violating data integrity of the respective
Database.
• Reasons for using Concurrency control method in DBMS:
To apply Isolation through mutual exclusion between conflicting transactions
To resolve read-write and write-write conflict issues
To preserve database consistency through constantly preserving execution
obstructions
The system needs to control the interaction among the concurrent transactions.
This control is achieved using concurrent-control schemes.
Concurrency control helps to ensure serializability
Why we need concurrency control
T1 T2
S(A) X(A)
R(A) R(A)
U(A) W(A)
U(A)
Lock-Based Protocols (Cont.)
• Lock-compatibility matrix
1. T1 T2
X(A)
R(A)
W(A)
U(A)
S(A)
U(A)
commit
Abort
Pitfalls of Lock-Based Protocols(Deadlock)
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 Protocols(Deadlock)
• Consider the partial schedule
T1 T2
Grant X(A)
Deadlock
Deadlock refers to a
X(B) Grant Lock specific situation where
two or more processes
Wait X(B)
are waiting for each
other to release a
resource or more than
X(A) (Wait) two processes are
waiting for the resource
in a circular chain.
Pitfalls of Lock-Based Protocols (Cont.)
• The potential for deadlock exists in most locking protocols.
Deadlocks are a necessary evil.
• 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.
concepts
Starvation
Starvation is the situation when a transaction needs to
wait for an indefinite period to acquire a lock.
Following are the reasons for Starvation:
•When waiting scheme for locked items is not
properly managed
•In the case of resource leak, the same transaction is
selected as a victim repeatedly
Two Phase Locking Protocol(2PL)
T1 T2
1 lock-S(A)
2 lock-S(A) Transaction T1:
3 lock-X(B) •The growing Phase is from steps 1-3.
•The shrinking Phase is from steps 5-7.
4 ……. ……
•Lock Point at 3
5 Unlock(A)
Transaction T2:
6 Lock-X(C) •The growing Phase is from steps 2-6.
7 Unlock(B) •The shrinking Phase is from steps 8-9.
8 Unlock(A) •Lock Point at 6
9 Unlock(C)
10 ……. ……
Two Phase Locking Protocol(Cascading Rollbacks in 2-
PL
To analyze the
schedule. because of
Dirty Read in T2 and
T3 in lines 8 and 12
respectively, when
T1 failed we have to
roll back others also.
Hence, Cascading
Rollbacks are
possible in 2-PL.
The Two-Phase Locking Protocol (Cont.)
• 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. Recoverable and Cascadeless, but not free from
Deadlock.
• 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.
Recoverable and Cascadeless, but not free from Deadlock.
Conservative two-Phase Locking
also called as Static Two – Phase Locking Protocol. This protocol is almost free from
deadlocks as all required items are listed in advanced. It requires locking of all data
items to access before the transaction starts. It prevents deadlocks but not
starvation and cascading rollbacks. However, it is difficult to use in practice because
of the need to predeclare the read-set and the write-set which is not possible in
many situations. In practice, the most popular variation of 2-PL is Strict 2-PL.
This protocol requires the transaction to lock all the items it access before the
transaction begins execution by predeclaring its read-set and write-set. If any of
the predeclared items needed cannot be locked, the transaction does not lock any
of the items, instead, it waits until all the items are available for locking.
Strict 2-PL Conservative 2-PL Rigorous 2-PL
In Strict 2-PL, A transaction can A transaction has to acquire locks A transaction can acquire locks on
acquire locks on data items on all the data items it requires data items whenever it requires
whenever it requires (only in before the transaction begins it (only in growing phase) during its
growing phase) during its execution. execution. execution.
It has growing phase. It does not have a growing phase. It has a growing phase.
It has partial shrinking phase. It has a shrinking phase. It does not have a shrinking phase.
It ensures that the schedule It ensures that the schedule It ensures that the schedule
generated would be Serializable, generated would be Serializable generated would be Serializable,
Recoverable and Cascadeless. and Deadlock-Free. Recoverable and Cascadeless.
It does not ensures Deadlock-Free It does not ensures Recoverable It does not ensures Deadlock-Free
schedule. and Cascadeless schedule. schedule.
It ensures that the schedule It does not ensure Strict It ensures that the schedule
generated would be Strict. Schedule. generated would be Strict.
Thank You