Database Concurrency Control
When multiple transactions are trying to access the same sharable resource, there could arise many
problems if the access control is not done properly. There are some important mechanisms to which
access control can be maintained.
There are protocols where Locks and Timestamps can be used to provide an environment in which
concurrent transactions can preserve their Consistency and Isolation properties.
1. LOCK BASED PROTOCOL
A lock is nothing but a mechanism that tells the DBMS whether a particular data item is being used
by any transaction for read/write purpose. Since there are two types of operations, i.e. read and write,
whose basic nature are different, the locks for read and write operation may behave differently.
Read operation performed by different transactions on the same data item poses less of a challenge.
The value of the data item, if constant, can be read by any number of transactions at any given time.
Write operation is something different. When a transaction writes some value into a data item, the
content of that data item remains in an inconsistent state, starting from the moment when the writing
operation begins up to the moment the writing operation is over. If we allow any other transaction to
read/write the value of the data item during the write operation, those transaction will read an
inconsistent value or overwrite the value being written by the first transaction. In both the cases
anomalies will creep into the database.
The simple rule for locking can be derived from here. If a transaction is reading the content of a
sharable data item, then any number of other processes can be allowed to read the content of the same
data item. But if any transaction is writing into a sharable data item, then no other transaction will be
allowed to read or write that same data item.
Depending upon the rules we have found, we can classify the locks into two types:
a) Shared Lock: A transaction may acquire shared lock on a data item in order to read its
content. The lock is shared in the sense that any other transaction can acquire the shared lock
on that same data item for reading purpose.
b) Exclusive Lock: A transaction may acquire exclusive lock on a data item in order to both
read/write into it. The lock is excusive in the sense that no other transaction can acquire any
kind of lock (either shared or exclusive) on that same data item. The relationship between
Shared and Exclusive Lock can be represented by the following illustration below which is
known as Lock Matrix.
How Should Lock be Used?
In a transaction, a data item which we want to read/write should first be locked before the read/write
is done. After the operation is over, the transaction should then unlock the data item so that other
transaction can lock that same data item for their respective usage. For example, a transaction to
deposit KShs. 1,000/- from account A to account B. The transaction should now be written as the
following:
Lock-X (A); (Exclusive Lock, we want to both read A’s value and modify it)
Read A;
1
, A = A – 1000;
Write A;
Unlock (A); (Unlocking A after the modification is done)
Lock-X (B); (Exclusive Lock, we want to both read B’s value and modify it)
Read B;
B = B + 1000;
Write B;
Unlock (B); (Unlocking B after the modification is done)
And the transaction that deposits 10% amount of account A to account C should now be written as:
Lock-S (A); (Shared Lock, we only want to read A’s value)
Read A;
Temp = A * 0.1;
Unlock (A); (Unlocking A)
Lock-X (C); (Exclusive Lock, we want to both read C’s value and modify it)
Read C;
C = C + Temp;
Write C;
Unlock (C); (Unlocking C after the modification is done)
Let us see how these locking mechanisms help us to create error free schedules.
T1 T2
Read A;
A = A – 1000;
....................................................................Read A;
....................................................................Temp = A * 0.1;
....................................................................Read C;
....................................................................C = C + Temp;
....................................................................Write C;
Write A;
Read B;
B = B + 1000;
Write B;
We detected the error based on common sense only, that the Context Switching is being performed
before the new value has been updated in A. T2 reads the old value of A, and thus deposits a wrong
amount in C. Had we used the locking mechanism, this error could never have occurred. Let us
rewrite the schedule using the locks.
T1 T2
Lock-X (A)
Read A;
A = A – 1000;
..........................................................................Lock-S (A)
..........................................................................Read A;
..........................................................................Temp = A * 0.1;
2
Voordelen van het kopen van samenvattingen bij Stuvia op een rij:
Verzekerd van kwaliteit door reviews
Stuvia-klanten hebben meer dan 700.000 samenvattingen beoordeeld. Zo weet je zeker dat je de beste documenten koopt!
Snel en makkelijk kopen
Je betaalt supersnel en eenmalig met iDeal, creditcard of Stuvia-tegoed voor de samenvatting. Zonder lidmaatschap.
Focus op de essentie
Samenvattingen worden geschreven voor en door anderen. Daarom zijn de samenvattingen altijd betrouwbaar en actueel. Zo kom je snel tot de kern!
Veelgestelde vragen
Wat krijg ik als ik dit document koop?
Je krijgt een PDF, die direct beschikbaar is na je aankoop. Het gekochte document is altijd, overal en oneindig toegankelijk via je profiel.
Tevredenheidsgarantie: hoe werkt dat?
Onze tevredenheidsgarantie zorgt ervoor dat je altijd een studiedocument vindt dat goed bij je past. Je vult een formulier in en onze klantenservice regelt de rest.
Van wie koop ik deze samenvatting?
Stuvia is een marktplaats, je koop dit document dus niet van ons, maar van verkoper peterndindi. Stuvia faciliteert de betaling aan de verkoper.
Zit ik meteen vast aan een abonnement?
Nee, je koopt alleen deze samenvatting voor €9,91. Je zit daarna nergens aan vast.