Chapter 3 Concurrency Control
Chapter 3 Concurrency Control
Chapter Three
Example:
◦ In concurrent execution environment,
If T1 conflicts with T2 over a data item A, then
the existing concurrency controller decides if
T1 or T2 should get A and which transaction
should be rolled-back or waits.
3
1. Concurrency control using Locks
◦ A lock is a mechanism to control concurrent
access to a data item.
Locking is an operation which secures
(a) permission to Read
(b) permission to Write a data item for a
transaction.
◦ Notation :
- Li(X) :Transaction Ti requests a lock on
database element X
◦ Unlocking is an operation which removes these
permissions from the data item.
◦ Notation :
- Ui (X): Transaction Ti releases (“unlocks”) its
lock on database element X
◦ Lock and Unlock are Atomic operations. 4
Concurrency control using Locks(cont.…)
Types of locks and system lock tables
Binary locks
◦ Two states (values)
▪ Locked (1)
- Item cannot be accessed
▪ Unlocked (0)
- Item can be accessed when requested
o A distinct lock is associated with each database item X
o If the value of the lock on X is 1, item X can not be
accessed by a database operation that request the item
o Too restrictive for database items because at most one
transaction can hold a lock on a given item
Shared/Exclusive (or read /write) locks
◦ Allow several transactions to access the same item x if
they all access x for reading purposes 5
Lock and unlock operations for binary locks
6
Shared/Exclusive (or Read/Write) Locks
Read operations on the same item are not conflicting
Must have exclusive lock to write
Three locking operations
o read_lock(X)
o write_lock(X)
o unlock(X)
In shared/exclusive method, data items can be locked in
two modes :
◦ 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. 7
Locking and unlocking
operations for two-mode
(read/write, or
shared/exclusive) locks.
8
Shared/Exclusive (or Read/Write) Locks…
When we use the shared/exclusive locking scheme, the system must
enforce the following rules:
11
Lock compatibility
• 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 transactions
• Any number of transactions can hold shared locks on an
item,
o But if any transaction holds an exclusive lock on the
item no other transaction may hold any lock on the item.
• If a lock cannot be granted, the requesting
transaction will be made to wait until all
incompatible locks held by other transactions have
been released
o Lock compatibility matrix
12
Cont…
The DBMS has lock manager subsystem to
control locks
◦ Lock Manager:
Manages locks on data items.
◦ Lock table:
Lock manager uses it to store the identify of
transaction that lock the data item
13
Lock Table Black rectangles indicate granted
locks, white ones indicate waiting
requests
Lock table also records the type of
lock granted or requested
New request is added to the end
of the queue of requests for the
data item, and granted if it is
compatible with all earlier locks
Unlock requests result in the
request being deleted, and later
requests are checked to see if they
Granted can now be granted
Waiting If transaction aborts, all waiting or
granted requests of the transaction
are deleted
14
Cont…
Using binary or read write locks in transactions as described
earlier by itself does not guarantee serializability :
18
Two-Phase Locking Protocol (2 PL)…
T’1 and T’2 follow two-phase policy but they are subject to deadlock
19
Variations of Two-Phase Locking
Two-phase locking policy generates two locking algorithms
◦ (a) Basic
◦ (b) Conservative
◦ Basic 2PL:
Technique described on previous slides
Transaction locks data items incrementally. This may
cause deadlock.
◦ Conservative (static) 2PL:
Requires a transaction to lock all the items it accesses
before the transaction begins
Predeclare read-set and write-set.
Prevents deadlock by locking all desired data items
before transaction begins execution (Deadlock-free
protocol). 20
Dealing with Deadlock and Starvation
Deadlock
o Occurs when each transaction T in a set is waiting for some item
locked by some other transaction T’.
o Both transactions stuck in a waiting queue.
Example of deadlock situation
21
Dealing with Deadlock and Starvation…
T1 T2
read_lock (Y);
read_item (Y);
unlock (y)
read_lock (X);
read_item (X);
unlock (X)
write_lock (X);
write_lock (Y);
22
Deadlock prevention
23
Deadlock prevention (cont…)
2. Making a decision about what to do with a
transaction involved in a possible deadlock
situation:
◦ Should it be blocked and made it to wait or
should it be aborted
◦ Should the transaction preempt and abort
another transaction
These concepts use transaction timestamp TS(T)
which is a unique identifier assigned to each
transaction based on the order they started
If transaction T1 starts before transaction T2, then
TS (T1) < TS(T2)
24
Deadlock prevention (cont.…)
Two schemes that prevent dead lock based on time stamp
includes
o Wait-die
o Wound-wait
Suppose that transaction Ti tries to lock an item X but is not
able to do so because X is locked by some other transaction
Tj with a conflicting lock. The rules followed by these two
schemes are as follows:
◦ Wait – die: If TS(Ti) < TS(Tj) i.e Ti is older than Tj, then
Ti is allowed to wait ; other wise, abort Ti (Ti dies) and
restart it later with the same time stamp
30
3. Multiversion concurrency control techniques
31
Multiversion concurrency control Based on
timestamp ordering
32
Multiversion concurrency control Based on
timestamp ordering(Cont.…)
• Whenever a transaction T is allowed to
execute a write_item(X) operation, a new
version Xk+1 of item X is created with both
the write_TS(Xk+1) and read_TS(Xk+1) set
to TS(T).
• Correspondingly, when a transaction is
allowed to read the value of xi, the value of
read_TS(xi) is set to the larger of the
current read_TS(xi) and TS(T)
33
Multiversion concurrency control Based on
timestamp ordering(Cont.…)
To ensure serializability, the following two rules are used.
1) 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 Xj of X with read_TS(Xj) = write_TS(Xj)
= TS(T)
2) 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 transaction T, and set the value of
read _TS(Xi) to the largest of TS(T) and the current
read_TS(Xi)
◦ Rule 2 guarantees that a read will never be rejected.
34
4. Validation (Optimistic) Concurrency
Control Schemes
In this technique, serializability is checked only at the time
of commit and transactions are aborted in case of
non-serializable schedules
No checking is done while transaction is executing
In this scheme, updates in the transaction are not applied
directly to the database item until it reaches its commit
point
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)
35
Validation Concurrency Control (Cont.…)
36
Granularity of data items and Multiple
Granularity Locking (MGL)
A lockable unit of data(size) 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.
Larger the data item size, lower the degree of concurrency
permitted(eg. entire disk block locked.
Smaller the data item size, more locks required(Higher
overhead)
Thus, the degree of concurrency is low for coarse granularity and high
for fine granularity.
Best item size depends on transaction type
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 database table
4. The entire database 37
Multiple Granularity Locking (Cont.…)
Lock can be requested at any level
The following diagram illustrates a hierarchy of
granularity from coarse to fine
5. T can lock a node only if it has not unlocked any node (to enforce
2PL policy).