Module 5 Part2 Concurrency-Control-Techniques
Module 5 Part2 Concurrency-Control-Techniques
Slide 18- 1
Database Concurrency Control
1 Purpose of Concurrency Control
To enforce Isolation (through mutual exclusion) among
conflicting transactions.
To preserve database consistency through consistency
preserving execution of transactions.
To resolve read-write and write-write conflicts.
Example:
In concurrent execution environment if T1 conflicts with T2
over a data item A, then the existing concurrency control
decides if T1 or T2 should get the A and if the other
transaction is rolled-back or waits.
Slide 18- 2
Database Concurrency Control
Two-Phase Locking Techniques
Locking is an operation which secures
(a) permission to Read
(b) permission to Write a data item for a transaction.
Example:
Lock (X). Data item X is locked in behalf of the requesting
transaction.
Unlocking is an operation which removes these permissions
from the data item.
Example:
Unlock (X): Data item X is made available to all other
transactions.
Lock and Unlock are Atomic operations.
Slide 18- 3
Database Concurrency Control
Two-Phase Locking Techniques: Essential components
Two locks modes:
(a) shared (read) (b) exclusive (write).
Shared mode: shared lock (X)
More than one transaction can apply share lock on X for
reading its value but no write lock can be applied on X by any
other transaction.
Exclusive mode: Write lock (X)
Only one write lock on X can exist at any time and no shared
lock can be applied by any other transaction on X.
Conflict matrix Read Write
Read
Y N
Write
N N
Slide 18- 4
Database Concurrency Control
Two-Phase Locking Techniques: Essential
components
Lock Manager:
Managing locks on data items.
Lock table:
Lock manager uses it to store the identify of
transaction locking a data item, the data item, lock
mode and pointer to the next data item locked. One
simple way to implement a lock table is through
linked list.
Transaction ID Data item id lock mode Ptr to next data item
T1 X1 Read Next
Slide 18- 5
Database Concurrency Control
Two-Phase Locking Techniques: Essential
components
Database requires that all transactions should be
well-formed. A transaction is well-formed if:
It must lock the data item before it reads or writes to
it.
It must not lock an already locked data items and it
must not try to unlock a free data item.
Slide 18- 6
Database Concurrency Control
Two-Phase Locking Techniques: Essential components
The following code performs the lock operation:
Slide 18- 7
Database Concurrency Control
Two-Phase Locking Techniques: Essential
components
The following code performs the unlock operation:
Slide 18- 8
Database Concurrency Control
Two-Phase Locking Techniques: Essential components
The following code performs the read operation:
B: if LOCK (X) = “unlocked” then
begin LOCK (X) “read-locked”;
no_of_reads (X) 1;
end
else if LOCK (X) “read-locked” then
no_of_reads (X) no_of_reads (X) +1
else begin wait (until LOCK (X) = “unlocked” and
the lock manager wakes up the transaction);
go to B
end;
Slide 18- 9
Database Concurrency Control
Two-Phase Locking Techniques: Essential components
The following code performs the write lock operation:
B: if LOCK (X) = “unlocked” then
begin LOCK (X) “read-locked”;
no_of_reads (X) 1;
end
else if LOCK (X) “read-locked” then
no_of_reads (X) no_of_reads (X) +1
else begin wait (until LOCK (X) = “unlocked” and
the lock manager wakes up the transaction);
go to B
end;
Slide 18- 10
Database Concurrency Control
Two-Phase Locking Techniques: Essential components
The following code performs the unlock operation:
if LOCK (X) = “write-locked” then
begin LOCK (X) “unlocked”;
wakes up one of the transactions, if any
end
else if LOCK (X) “read-locked” then
begin
no_of_reads (X) no_of_reads (X) -1
if no_of_reads (X) = 0 then
begin
LOCK (X) = “unlocked”;
wake up one of the transactions, if any
end
end;
Slide 18- 11
Database Concurrency Control
Two-Phase Locking Techniques: Essential components
Lock conversion
Lock upgrade: existing read lock to write lock
Slide 18- 12
Database Concurrency Control
Two-Phase Locking Techniques: The algorithm
Two Phases:
(a) Locking (Growing)
one at a time.
Unlocking (Shrinking) Phase:
A transaction unlocks its locked data items one at a time.
Requirement:
For a transaction these two phases must be mutually exclusively,
that is, during locking phase unlocking phase must not start and
during unlocking phase locking phase must not begin.
Slide 18- 13
Database Concurrency Control
Two-Phase Locking Techniques: The algorithm
T1 T2 Result
read_lock (Y); read_lock (X); Initial values: X=20; Y=30
read_item (Y); read_item (X); Result of serial execution
unlock (Y); unlock (X); T1 followed by T2
write_lock (X); Write_lock (Y); X=50, Y=80.
read_item (X); read_item (Y); Result of serial execution
X:=X+Y; Y:=X+Y; T2 followed by T1
write_item (X); write_item (Y); X=70, Y=50
unlock (X); unlock (Y);
Slide 18- 14
Database Concurrency Control
Two-Phase Locking Techniques: The algorithm
T1 T2 Result
read_lock (Y); X=50; Y=50
read_item (Y); Nonserializable because it.
unlock (Y); violated two-phase policy.
read_lock (X);
read_item (X);
Time unlock (X);
write_lock (Y);
read_item (Y);
Y:=X+Y;
write_item (Y);
unlock (Y);
write_lock (X);
read_item (X);
X:=X+Y;
write_item (X);
unlock (X);
Slide 18- 15
Database Concurrency Control
Two-Phase Locking Techniques: The algorithm
T’1 T’2
read_lock (Y); read_lock (X); T1 and T2 follow two-phase
read_item (Y); read_item (X); policy but they are subject to
write_lock (X); Write_lock (Y); deadlock, which must be
unlock (Y); unlock (X); dealt with.
read_item (X); read_item (Y);
X:=X+Y; Y:=X+Y;
write_item (X); write_item (Y);
unlock (X); unlock (Y);
Slide 18- 16
Database Concurrency Control
Two-Phase Locking Techniques: The algorithm
Two-phase policy generates two locking algorithms
(a) Basic
(b) Conservative
Conservative:
Prevents deadlock by locking all desired data items before
Slide 18- 17
Database Concurrency Control
Dealing with Deadlock and Starvation
Deadlock
T’1 T’2
read_lock (Y); T1 and T2 did follow two-phase
read_item (Y); policy but they are deadlock
read_lock (X);
read_item (Y);
write_lock (X);
(waits for X) write_lock (Y);
(waits for Y)
Slide 18- 18
Database Concurrency Control
Dealing with Deadlock and Starvation
Deadlock prevention
Slide 18- 19
Database Concurrency Control
Dealing with Deadlock and Starvation
Deadlock detection and resolution
Slide 18- 20
Database Concurrency Control
Dealing with Deadlock and Starvation
Deadlock avoidance
Slide 18- 21
Database Concurrency Control
Dealing with Deadlock and Starvation
Starvation
Slide 18- 22
Database Concurrency Control
Timestamp based concurrency control algorithm
Timestamp
Slide 18- 23
Database Concurrency Control
Timestamp based concurrency control algorithm
Basic Timestamp Ordering
Slide 18- 24
Database Concurrency Control
Timestamp based concurrency control algorithm
Strict Timestamp Ordering
Slide 18- 25
Database Concurrency Control
Timestamp based concurrency control algorithm
Thomas’s Write Rule
Slide 18- 26
Database Concurrency Control
Multiversion concurrency control techniques
This approach maintains a number of versions of a
data item and allocates the right version to a read
operation of a transaction. Thus unlike other
mechanisms a read operation in this mechanism is
never rejected.
Side effect:
Significantly more storage (RAM and disk) is
required to maintain multiple versions. To check
unlimited growth of versions, a garbage collection is
run when some criteria is satisfied.
Slide 18- 27
Database Concurrency Control
Multiversion technique based on timestamp
ordering
This approach maintains a number of versions of a
data item and allocates the right version to a read
operation of a transaction.
Thus unlike other mechanisms a read operation in
this mechanism is never rejected.
Side effects: Significantly more storage (RAM and
disk) is required to maintain multiple versions. To
check unlimited growth of versions, a garbage
collection is run when some criteria is satisfied.
Slide 18- 28
Database Concurrency Control
Multiversion technique based on timestamp ordering
Assume X1, X2, …, Xn are the version of a data item X
created by a write operation of transactions. With each Xi a
read_TS (read timestamp) and a write_TS (write timestamp)
are associated.
read_TS(Xi): The read timestamp of Xi is the largest of all
the timestamps of transactions that have successfully read
version Xi.
write_TS(Xi): The write timestamp of Xi that wrote the
value of version Xi.
A new version of Xi is created only by a write operation.
Slide 18- 29
Database Concurrency Control
Multiversion technique based on timestamp ordering
To ensure serializability, the following two rules are used.
If transaction T issues write_item (X) and version i of X has
the highest write_TS(Xi) of all versions of X that is also less
than or equal to TS(T), and read _TS(Xi) > TS(T), then abort
and roll-back T; otherwise create a new version Xi and
read_TS(X) = write_TS(Xj) = TS(T).
If transaction T issues read_item (X), find the version i of X
that has the highest write_TS(Xi) of all versions of X that is
also less than or equal to TS(T), then return the value of Xi
to T, and set the value of read _TS(Xi) to the largest of
TS(T) and the current read_TS(Xi).
Slide 18- 30
Database Concurrency Control
Multiversion technique based on timestamp ordering
To ensure serializability, the following two rules are used.
If transaction T issues write_item (X) and version i of X has the
highest write_TS(Xi) of all versions of X that is also less than
or equal to TS(T), and read _TS(Xi) > TS(T), then abort and
roll-back T; otherwise create a new version Xi and read_TS(X)
= write_TS(Xj) = TS(T).
Slide 18- 31
Database Concurrency Control
Multiversion Two-Phase Locking Using Certify
Locks
Concept
Slide 18- 32
Database Concurrency Control
Multiversion Two-Phase Locking Using Certify Locks
Steps
1. X is the committed version of a data item.
2. T creates a second version X’ after obtaining a write lock on X.
3. Other transactions continue to read X.
4. T is ready to commit so it obtains a certify lock on X’.
5. The committed version X becomes X’.
6. T releases its certify lock on X’, which is X now.
Compatibility tables for
Read Write Read Write Certify
Read yes no Read yes no no
Write no no Write no no no
Certify no no no
read/write locking scheme read/write/certify locking scheme
Slide 18- 33
Database Concurrency Control
Multiversion Two-Phase Locking Using Certify
Locks
Note:
Slide 18- 34
Database Concurrency Control
Validation (Optimistic) Concurrency Control Schemes
In this technique only at the time of commit serializability
is checked and transactions are aborted in case of non-
serializable schedules.
Three phases:
1. Read phase
2. Validation phase
3. Write phase
1. Read phase:
A transaction can read values of committed data items.
However, updates are applied only to local copies
(versions) of the data items (in database cache).
Slide 18- 35
Database Concurrency Control
Validation (Optimistic) Concurrency Control Schemes
2. Validation phase: Serializability is checked before transactions write
their updates to the database.
This phase for Ti checks that, for each transaction Tj that is either
Slide 18- 36
Database Concurrency Control
Validation (Optimistic) Concurrency Control
Schemes
3. Write phase: On a successful validation
transactions’ updates are applied to the
database; otherwise, transactions are restarted.
Slide 18- 37
Database Concurrency Control
Granularity of data items and Multiple Granularity Locking
A lockable unit of data defines its granularity. Granularity
can be coarse (entire database) or it can be fine (a tuple
or an attribute of a relation).
Data item granularity significantly affects concurrency
control performance. Thus, the degree of concurrency is
low for coarse granularity and high for fine granularity.
Example of data item granularity:
1. A field of a database record (an attribute of a tuple)
2. A database record (a tuple or a relation)
3. A disk block
4. An entire file
5. The entire database
Slide 18- 38
Database Concurrency Control
Granularity of data items and Multiple Granularity
Locking
The following diagram illustrates a hierarchy of
f1 f2
r111 ... r11j r111 ... r11j r111 ... r11j r111 ... r11j r111 ... r11j r111 ... r11j
Slide 18- 39
Database Concurrency Control
Granularity of data items and Multiple Granularity Locking
To manage such hierarchy, in addition to read and write,
Slide 18- 40
Database Concurrency Control
Granularity of data items and Multiple Granularity
Locking
These locks are applied using the following
Slide 18- 41
Database Concurrency Control
Granularity of data items and Multiple Granularity Locking
The set of rules which must be followed for producing serializable
schedule are
1. The lock compatibility must adhered to.
2. The root of the tree must be locked first, in any mode..
3. A node N can be locked by a transaction T in S or IX mode only if
the parent node is already locked by T in either IS or IX mode.
4. A node N can be locked by T in X, IX, or SIX mode only if the
parent of N is already locked by T in either IX or SIX mode.
5. T can lock a node only if it has not unlocked any node (to enforce
2PL policy).
6. T can unlock a node, N, only if none of the children of N are
currently locked by T.
Slide 18- 42
Database Concurrency Control
Granularity of data items and Multiple Granularity Locking: An example of a serializable execution:
T1 T2 T3
IX(db)
IX(f1)
IX(db)
IS(db)
IS(f1)
IS(p11)
IX(p11)
X(r111)
IX(f1)
X(p12)
S(r11j)
IX(f2)
IX(p21)
IX(r211)
Unlock (r211)
Unlock (p21)
Unlock (f2)
S(f2)
Slide 18- 43
Database Concurrency Control
Granularity of data items and Multiple Granularity Locking: An example
of a serializable execution (continued):
T1 T2 T3
unlock(p12)
unlock(f1)
unlock(db)
unlock(r111)
unlock(p11)
unlock(f1)
unlock(db)
unlock (r111j)
unlock (p11)
unlock (f1)
unlock(f2)
unlock(db)
Slide 18- 44
Summary
Databases Concurrency Control
1. Purpose of Concurrency Control
2. Two-Phase locking
3. Limitations of CCMs
4. Index Locking
5. Lock Compatibility Matrix
6. Lock Granularity
Slide 18- 45