1. 1
Concurrency Control Techniques
Concurrency Control Techniques: Overview of Locking, 2PL, Timestamp ordering,
multiversioning, validation
There are number of concurrency control techniques that are used to ensure the isolation property
of concurrently executing transactions. Most of these techniques ensure serializability of
schedules using protocols (sets of rules) that guarantee serializability. One important set of
protocols employs the technique of locking data items to prevent multiple transactions from
accessing the items concurrently. Locking protocols are used in most commercial DBMSs.
Another set of concurrency control protocols use timestamps. A timestamp is a unique identifier
for each transaction, generated by the system.
The multiversion concurrency control protocols use multiple versions of a data item.
A protocol based on the concept of validation or certification of a transaction after it executes
its operations; is sometimes called optimistic protocol.
1. Locking Techniques for Concurrency Control
Transaction processing system usually allows multiple transactions to run concurrently. By
allowing multiple transactions to run concurrently will improve the performance of the system in
terms of increased throughout or improved response time, but this allows causes several
complications with consistency of the data. Ensuring consistency in spite of concurrent execution
of transaction require extra work, which is performed by the concurrency controller system of
DBMS.
What is Lock?
A lock is a variable associated with a data item that describes the status of the item with respect
to possible operations that can be applied to it. Generally, there is one lock for each data item in
the database. Locks are used as a means of synchronizing the access by concurrent transactions
to the database item.
Types of Locks
Generally 2 types of locks are used in concurrency control.
i) Binary locks, which are simple but restrictive and so are not used in practice.
ii) Shared/Exclusive locks, which provide more general locking capabilities and are used
in practical database locking schemes.
i) Binary Locks
A binary lock can have two states or values: locked and unlocked. A distinct lock is associated
with each database item A. If the value of the lock on A is 1, item A cannot be accessed by a
2. 2
database operation that requests the item. If the value of the lock on A is 0 then item can be
accessed when requested.
We refer to the current value of the lock associated with item A as LOCK (A).
There are two operations, lock_item(A) and unlock(A) that are used with binary locking. A
transaction requests access to an item A by first issuing a lock_item(A) operation.
If LOCK (A) = 1, the transaction is forced to wait.
If LOCK (A) = 0 it is set to 1 (the transaction locks the item) and the transaction is
allowed to access item A.
When the transaction is through using the item, it issues an unlock(A) operation, which sets
LOCK (A) to 0 (unlocks the item) so that A may be accessed by other transactions. Hence binary
lock enforces mutual exclusion on the data item.
Rules of Binary Locks
If the simple binary locking scheme described here is used, every transaction must obey the
following rules:
1) A transaction must issue the operation lock_item(A) before any read_item (A) or write,
item operations are performed in T.
2) A transaction T must issue the operation unlock(A) after all read_item (A) and
write_item (A) operations are completed in T.
3) A transaction T will not issue a lock _item (A) operation if it already holds the lock on
Item A.
4) A transaction T will not issue an unlock(A) operation unless it already holds the lock on
item A.
5) The lock manager module of the DBMS can enforce these rules. Between the lock_item
(A) and unlock(A) operations in transaction T, is said to hold the lock on item A. At most
one transaction can hold the lock on a particular item. Thus no two transactions can
access the same item concurrently.
Disadvantages of Binary Locks
As binary locking scheme is too restrictive for database items, because at most one transaction
can hold a lock on a given item. So, binary locking system cannot be used for practical purpose.
ii) Share/Exclusive (for Read/Write) Locks
Locking operations: There are three locking operations called read_lock(A), write_lock(A)
and unlock(A). A lock associated with an item A, LOCK (A), now has three possible states:
"read-locked", "write-locked," or "unlocked." A read-locked item is also called share-locked item
because other transactions are allowed to read the item, whereas a write-locked item is caused
exclusive-locked, because a single transaction exclusively holds the lock on the item.
3. 3
Compatibility of Locks Suppose that there are A and B two different locking modes Read or
Shared(S) & Write or Exclusive(X). If a transaction T1 requests a lock of mode on item Q on
which transaction T2 currently hold a lock of mode B. If transaction can be granted lock, in spite
of the presence of the mode B lock, then we say mode A is compatible with mode B. Such a
function is shown in one matrix as shown below:
The graphs shows that if two transactions only read the same data object they do not conf1ict,
but if one transaction writes a data object and another either read or write the same data object,
then they conflict with each other. A transaction requests a shared lock on data item Q by
executing the read_lock(Q) instruction. Similarly, an exclusive lock is requested through the
write_lock(Q) instruction. A data item Q can be unlocked via the unlock(Q) instruction.
How Should Lock be Used?
To access a data item, transaction T1 must first lock that item. If the data item is already locked
by another transaction in an incompatible mode, the concurrency control manager will not grant
the lock until all incompatible locks held by other transactions have been released. Thus, T1 is
made to wait until all incompatible locks held by other transactions have been released
When we use the shared/exclusive locking scheme, the system must enforce the following rules:
a. A transaction T must issue the operation read_lock(X) or write_lock(X) before
any read_item(X) operation is performed in T.
b. A transaction T must issue the operation write_lock(X) before any write_item(X)
operation is performed in T.
c. A transaction T must issue the operation unlock(X) after all read_item(X) and
write_item(X) operations are completed in T.
d. A transaction T will not issue a read_lock(X) operation if it already holds a read
(shared) lock or a write (exclusive) lock on item X. This rule may be relaxed.
e. A transaction T will not issue a write_lock(X) operation if it already holds a read
(shared) lock or write (exclusive) lock on item X. This rule may be relaxed.
f. A transaction T will not issue an unlock(X) operation unless it already holds a
read (shared) lock or a write (exclusive) lock on item X.
4. 4
For example, a transaction T1’ after applying the read/write or shared/exclusive lock on T1.
T1 T1’(equivalent transaction after applying read/write lock)
Example: Let x & y are two accounts that are accessed by transactions T1 & T2. T1 transfers
50/- from account x to y and is defined below.
Transaction T1 T2: simply add x & y and display the sum
Suppose that the values of accounts x and y are Rs.200 and Rs.100, respectively. If these two
transactions are executed serially, either in the order T1 and T2 or the order T2 and T1 then
transaction T2 will display the value Rs.300. If however, these transactions are executed
concurrently, as shown in Schedule 1, in this case, transaction T2 displays Rs.250, which is
incorrect. The reason for this mistake is that the transaction TI unlocked data item x too early, as
a result of which T2 shows an inconsistent state.
read(x);
read(y);
y=y+x;
write(y);
read_lock(x); Shared lock on x
read(x);
unlock(x); unlock x
write_lock(y); exclusive lock on y
read(y);
y = y + x;
write(y);
unlock(y); unlock y
read_lock(y);
read(y);
unlock(y);
read_lock(x);
read(x);
unlock(x);
sum=x+y;
display(sum);
write_lock(x);
read(x);
x = x – 50;
write(x);
unlock(x);
write_lock(y);
read(y);
y = y + 50;
write(y);
unlock(y);
5. 5
T1 T2
write_lock(x);
read(x);
x = x – 50;
write(x);
unlock(x);
read_lock(y);
read(y);
unlock(y);
read_lock(x);
read(x);
unlock(x);
sum=x+y;
display(sum);
write_lock(y);
read(y);
y = y + 50;
write(y);
unlock(y);
Figure: Schedule 1
Hence, it can be found that using read/write locking mechanism doesn’t guarantee serializibilty.
Guaranteeing serializibilty can be done by the locking scheme called 2-phase locking scheme or
2PL mechanism.
6. 6
Two Phase Locking (2PL) Protocol
The use of locks has helped us to create neat and clean concurrent schedule. The Two Phase
Locking Protocol defines the rules of how to acquire the locks on a data item and how to release
the locks. The Two Phase Locking Protocol assumes that a transaction can only be in one of two
phases.
i) Growing Phase: During this phase, the transaction can only acquire locks but no existing lock
can be released.
ii) Shrinking Phase: During this phase, existing locks can be released but no new locks can be
acquired.
The 2PC Locking protocol always results in a serializable schedule.
Example :: here the transaction T1 do not follow the 2PC Protocol, because the write_lock(y)
operation in T1 follows the unlock(x) operation. On enforcing the 2PC Locking scheme, the
transaction T1 can be written as T1’ as follows:
T1 T1’
Problems in Two-Phase Locking
Deadlock - It happens whenever a transaction waits for a lock to be unlock (to access the
data).
Cascading roll back -When the data requested by 1 transaction is held by some other
transactions again and again and the requested data is not given.
write_lock(x);
read(x);
x = x – 50;
write(x);
unlock(x);
write_lock(y);
read(y);
y = y + 50;
write(y);
unlock(y);
write_lock(x);
read(x);
x = x – 50;
write(x);
write_lock(y);
unlock(x);
read(y);
y = y + 50;
write(y);
unlock(y);
7. 7
Concurrency Control Based on Timestamp Ordering
The Timestamp Ordering(TO) Algorithm
The timestamp-ordering protocol ensures serializability among transaction in their conflicting
read and write operations. This is the responsibility of the protocol system that the conflicting
pair of tasks should be executed according to the timestamp values of the transactions.
Timestamps
A timestamp is a unique identifier created by the DBMS to identify a transaction. Typically,
timestamp values are assigned in the order in which the transactions are submitted to the system,
so a timestamp can be thought of as the transaction start time. The timestamp of transaction T
can be referred as TS(T). Concurrency control techniques based on timestamp ordering do not
use locks; hence, deadlocks cannot occur.
In this, a unique fixed timestamp is associated with each transaction to keep the order of the
transaction. It is denoted by TS (Ti).
Example: If a transaction T1 has been assigned timestamp TS (T1) and a new transaction TS
(T2) enters the system, then TS (T1) < TS (T2).
Two methods for implementing TS
Use the value of the system as the timestamp (System Clock).
Use a logical counter that is incremented after a new timestamp has been assigned.
Implementation Method
The timestamp of a data item can be of the following two types:
WTS(Q) or W-timestamp (Q): This means the latest time when the data item Q has
been written into.
RTS(Q) or R-timestamp (Q): This means the latest time when the data item Q has been
read from.
These two timestamps are updated each time a successful read/write operation is performed on
the data item Q.
Timestamp ordering protocol works as follows:
1. If a transaction Ti issues read operation:
If TS(Ti) < WTS(Q), then the read operation is rejected and the transaction Ti is restarted
with a new timestamp value; otherwise, i.e if TS(Ti) >= WTS(Q) , the read operation is executed
and RTS(Q) is set to max(RTS(Q), TS(Ti)).
2. If a transaction Ti issues write operation:
If TS (Ti) < RTS(Q) or TS (Ti) < WTS(Q), then the write operation is rejected or
rollback and the transaction Ti is restarted with a new timestamp value; otherwise, if TS(Ti) >=
RTS(Q) or TS(Ti) >= WTS(Q) , the write operation is executed and WTS(Q) is set to TS(Ti).
8. 8
Example:
Step T1 T2 T3 a b c
(200) (150) (175) RT(a)=0, RT(b)=0, RT(c)=0,
WT(a)=0 WT(b)=0 WT(c)=0
1 Read(b) RT(b)=200
2 Read(a) RT(a)=150
3 Read(c) RT(c)=175
4 Write(b) WT(b)=200
5 Write(a) WT(a)=200
6 Write(c)
7 Write(a)
At step1, the operationread(b) executed,as
TS(T1) < WTS(b) i.e 200 < 0 does not follows , and
RTS(b) is set to max(RTS(b), TS(T1)) i.e RTS(b)=200
Similar rules for step 2, 3
For step 4 the operationwrite(b)executed,as
TS (T1) < RTS(b) or TS (T1) < WTS(b) i.e. 200 < 200 or 200 < 0 does not follows , and
WTS(b) is set to TS(T1) i.e WTS(b)=200
Step 5 the operationwrite(a)isalsoexecuted.
At step6, howeverthe operationwrite(c) isrejectedasitdoesnotfollowsthe rule
At step7, T3 triesto write a & the read value of a i.e RTS(a) =150. But TS(T3) > RT(a) , i.e 175 > 150, T3
neednotto be aborted.