Database Management System 21: Concurrency Control
Database Management System 21: Concurrency Control
Database Management System 21: Concurrency Control
Chittaranjan Pradhan
Chittaranjan Pradhan
School of Computer Engineering,
KIIT University
21.1
Concurrency Control
Need of Concurrency Control
Chittaranjan Pradhan
Need of Concurrency
Lost Update Problem Control
Lock-Based Protocols
This problem occurs when two transactions that access the Basic Rules for Locking
Working of Locking
same database items have their operations interleaved in a
Locking Protocol
way that makes the value of some database item incorrect
T1 T2
Read(A);
A:=A-100;
Read(A);
Temp=0.2*A;
A:=A-Temp;
Write(A);
Write(A);
Read(B);
B:=B+100;
Write(B);
21.2
Concurrency Control
Need of Concurrency Control...
Chittaranjan Pradhan
Need of Concurrency
Temporary Update(or Dirty Read) Problem Control
Lock-Based Protocols
This problem occurs when one transaction updates a database Basic Rules for Locking
Working of Locking
item and then the transaction fails due to some reason. The
Locking Protocol
updated item is accessed by another transaction before it is
changed back to its original value
T1 T2
Read(A);
A:=A-100;
Write(A);
Read(A);
Temp=0.2*A;
A:=A-Temp;
Write(A);
Read(B);
B:=B+100;
21.3
Concurrency Control
Need of Concurrency Control...
Chittaranjan Pradhan
T1 T2
sum=0;
Read(A);
A:=A-100;
Write(A);
Read(A);
sum:=sum+A;
Read(B);
sum:=sum+B;
Read(B);
B:=B+100;
Write(B);
21.4
Concurrency Control
Lock-Based Protocols
Chittaranjan Pradhan
Lock-Based Protocols
Need of Concurrency
Locking is a procedure used to control concurrent access to Control
Working of Locking
Need of Concurrency
Control
• All transactions that need to access a data item must first Lock-Based Protocols
acquire a read lock or write lock on the data item Basic Rules for Locking
Working of Locking
depending on whether it is a read only operation or not Locking Protocol
21.7
Concurrency Control
Working of Locking...
Chittaranjan Pradhan
21.8
Concurrency Control
Working of Locking...
Chittaranjan Pradhan
T1 T2
Read(A);
Need of Concurrency
Write(A); Control
Read(A); Lock-Based Protocols
Schedule3 Write(A); Basic Rules for Locking
Working of Locking
Read(B);
Write(B); Locking Protocol
Read(B);
Write(B);
T1 T2 Concurrency-Control Manager
Lock-X(A)
Grant-X(A, T1 )
Read(A);
Write(A);
Unlock(A)
Lock-X(A)
Grant-X(A, T2 )
Read(A);
Write(A);
Unlock(A)
Lock-X(B)
Grant-X(B, T1 )
Read(B);
Write(B);
Unlock(B)
Lock-X(B)
Grant-X(B, T2 )
Read(B);
Write(B);
Unlock(B)
21.9
Concurrency Control
Working of Locking...
Chittaranjan Pradhan
Need of Concurrency
Control
Lock-Based Protocols
T1 T2 Basic Rules for Locking
Working of Locking
Read(A);
Locking Protocol
Read(A);
Write(A);
Schedule4 Read(B);
Write(A);
Read(B);
Write(B);
Write(B);
T1 T2 Concurrency-Control Manager
Lock-X(A)
Grant-X(A, T1 )
Read(A);
Lock-X(A)
21.10
Concurrency Control
Working of Locking...
Chittaranjan Pradhan
T1 T2
Need of Concurrency
Read(A); Control
Write(A);
Lock-Based Protocols
Read(A); Basic Rules for Locking
Schedule5 Read(B); Working of Locking
T1 T2 Concurrency-Control Manager
Lock-X(A)
Grant-X(A, T1 )
Read(A);
Write(A);
Unlock(A)
Lock-X(A)
Grant-X(A, T2 )
Read(A);
Lock-X(B)
Grant-X(B, T1 )
Read(B);
Write(A);
Unlock(A)
Lock-X(B)
21.11
Concurrency Control
Working of Locking...
Chittaranjan Pradhan
Need of Concurrency
Control
T1 T2
Read(A); Read(A); Lock-Based Protocols
A:=A-100; Read(B); Basic Rules for Locking
T1 T2
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);
21.12
Concurrency Control
Working of Locking...
Chittaranjan Pradhan
21.13
Concurrency Control
Working of Locking...
Chittaranjan Pradhan
Lock-Based Protocols
unlocking process. That means the unlocking is delayed to the Basic Rules for Locking
Locking Protocol
21.14
Concurrency Control
Working of Locking...
Chittaranjan Pradhan
T1 T2 Concurrency-Control Manager
Lock-X(A)
Grant-X(A, T1 ) Need of Concurrency
Control
Read(A);
A:=A-100; Lock-Based Protocols
Basic Rules for Locking
Write(A); Working of Locking
Lock-S(A)
Locking Protocol
T3 T2 Concurrency-Control Manager
Lock-X(B) Need of Concurrency
Grant-X(B, T3 ) Control
Read(B); Lock-Based Protocols
B:=B-100; Basic Rules for Locking
Write(B); Working of Locking
Lock-S(A)
Locking Protocol
Grant-S(A, T2 )
Read(A);
Lock-S(B);
Lock-X(A)
Locking Protocol
Need of Concurrency
Control
• At this point, T1 may release the lock, but still T2 has to
Lock-Based Protocols
wait for T3 to finish. There may be a new transaction T4 Basic Rules for Locking
Working of Locking
that requests a shared-mode lock on the same data item,
Locking Protocol
and is granted the lock before T3 releases it
• In such a situation, T2 never gets the exclusive-mode lock
on the data item. Thus, T2 cannot progress at all and is
said to be starved. This problem is called as the starvation
problem
We can avoid starvation of transactions by granting locks in the
following manner; when a transaction Ti requests a lock on a
data item Q in a particular mode M, the concurrency-control
manager grants the lock provided that:
• There is no other transaction holding a lock on Q in a
mode that conflicts with M
• There is no other transaction that is waiting for a lock on Q
and that made its lock request before Ti
21.18