You are on page 1of 34

CONTENTS

• What is Concurrency Control?


• Purpose of concurrency control
• Why we need concurrency control?
• Classification
• 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.
PURPOSE OF CONCURRENCY CONTROL

• To enforce Isolation (through mutual exclusion) among


conflicting transactions.

• To resolve read-write, write-read and write-write conflicts.


Why we need Concurrency
Control?
• Simultaneous execution of transactions over a shared
database can create several data integrity and consistency
problems:

• Lost Updates.
• Uncommitted Data.
• Inconsistent retrievals.
CLASSIFICATION

• Lock-Based Protocols

• Timestamp-Based
Protocols

• Validation-Based
Protocols
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.
Types of lock based protocol
• Simple Locking Protocol
• 2-phase locking Protocol

o Simple 2-phase locking


o Conservative 2-phase locking
o Strict 2-phase locking
o Rigorous 2-phase locking
Example
T1: A B T2: Display(A+B)
lock-X(A); lock-S(A);
read (A); read (A);
A=A-100; unlock (A);
write(A); lock-S(B);
unlock (A); read (B);
lock-X(B); unlock(B);
read (B); Display(A+B);
B = B+100;
write(B);
unlock(B);
T1 T2
lock-X(A);
read (A);
A=A-100;
write(A);
unlock (A); lock-S(A);
read (A);
unlock (A);
lock-S(B);
read (B);
unlock(B);
Display(A+B)

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

• Cascading roll-back is possible in this locking mechanism.


Strict 2-phase locking Protocol
• Most popular
• Guarantees strict schedule.
• Transaction T does not release any of the exclusive lock until
transaction T commits or abort.

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.

•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

You might also like