Adbms CH 1.b
Adbms CH 1.b
b
Concurrency Control Techniques
Concurrency Control Techniques
Concurrency control techniques are those techniques used to ensure the
isolation property of concurrently executing transactions.
Most of the techniques ensure serializability of schedules by using protocols
that guarantees serializability.
Some of the main techniques used to control concurrent execution of
transactions are
1. Concurrency Control Based on Locking.
2. Concurrency Control Based on Timestamp Ordering.
3. Multi version Concurrency Control Techniques
4. Optimistic Concurrency Control Techniques
1. Concurrency Control Based on Locking
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.
One lock with each data item in the database.
Locks are used to synchronize(coordinate) the access to the data item.
A lock guarantees exclusive use of a data item to a current transaction.
A transaction acquires a lock prior to data access and the lock is released
(unlocked) when the transaction is completed.
So that another transaction can lock the data item for its exclusive use.
Cont’d.
The use of locks based on the assumption that conflict between
transactions is likely to occur is often referred to as pessimistic
locking.
Most multiuser DBMSs automatically initiate and enforce locking
procedures.
All lock information is managed by a lock manager, which is
responsible for assigning and controlling the locks used by the
transactions.
Lock Granularity
For example, the DBMS will not allow two transactions to read the same
database object even though both transactions not update the database.
2. Shared/Exclusive Locks (Read/Write Locks)
The labels “shared” and “exclusive” indicate the nature of the lock.
An exclusive lock exists when access is reserved specifically for the
transaction that locked the object.
The exclusive lock must be used when the potential for conflict exists.
A shared lock exists when concurrent transactions are granted read
access on the basis of a common lock.
A shared lock produces no conflict as long as all the concurrent
transactions are read-only.
Cont’d
Multiple-mode lock.
There are three locking operations: read_lock(X), write_lock(X),
and unlock(X).
A lock associated with an item X, LOCK(X), now has three
possible states: read-locked, write-locked or unlocked.
A read-locked item is also called share-locked because other
transactions are allowed to read the item
A write-locked item is called exclusive-locked because a single
transaction exclusively holds the lock on the item.
Cont’d
A shared lock is issued when a transaction wants to read data from the
database and no exclusive lock is held on that data item.
An exclusive lock is issued when a transaction wants to update (write) a
data item and no locks are currently held on that data item by any other
transaction.
Shared locks allow several read transactions to read the same data item
concurrently.
The exclusive lock is granted if and only if no other locks are held on the
data item.
This condition is known as the mutual exclusive rule: only one transaction
at a time can own an exclusive lock on the same object.
Cont’d
A Transaction is allowed under certain conditions to convert the lock from one
state to another.
• Upgrading:
– Convert the lock from shared to exclusive by issuing write lock(X) after
its read lock(X).
– The transaction must be the only one that has the read lock or it must
wait.
• Downgrading:
– Convert the lock from exclusive to shared by issuing read_lock(X) after
the write_lock(X).
Cont’d
Locks prevent data inconsistencies, but they can lead to two major
problems:
1. The resulting transaction schedule might not be serializable.
2. The schedule might create deadlocks. (A deadlock occurs when
two transactions wait indefinitely for each other to unlock data. )
To solve the above mentioned issues additional protocols concerning the
positioning of locking and unlocking operations are followed in every
transaction.
Two-Phase Locking (2PL) to Ensure Serializability
Example
o If TS(T) < WTS(X), then read(X) is rejected (as the TO rule is violated).
[then a younger transaction has already written to the data item so abort and
roll-back T and reject the operation.]
o If WTS(X) <= TS(T), then execute read(X) of T and update RTS(X) to the
largest of TS(T).
Cont’d
Case 2 (Write): Transaction T issues a write(X) operation
o If RTS(X) <= TS(T) and WTS(X) <= TS(T), then execute write(X) of T
and update WTS(X) to TS(T).
Basic TO Algorithm Example
Two transactions T1 and T2. Initially RTS=0 and WTS=0 for data items X, Y
Timestamps are as follows: TS(T1)=10 and TS(T2)=20
T1 T2
TS(T1)=10 TS(T2)=20
1. A1 = Read(X)
2. A1 = A1 – k
3. Write(X, A1)
1. A1 = Read(X)
2. A1 = A1 * 1.01
3. Write(X, A1)
4. A2 = Read(Y)
5. A2 = A2 + k
6. Write(Y, A2)
4. A2 = Read(Y)
5. A2 = A2 * 1.01
6. Write(Y, A2)
RTS(X) : 20 4. A2 = Read(Y)
WTS(X) : 20 5. A2 = A2 + k
RTS(Y) : 10 6. Write(Y, A2)
WTS(Y) : 10 4. A2 = Read(Y) RTS(X) : 20
5. A2 = A2 * 1.01 WTS(X) : 20
6. Write(Y, A2) RTS(Y) : 20
WTS(Y) : 20
The schedule given above is serializable
Example 2
Discuss whether the following schedule is serializable or not? Justify your
answer using Basic TO Algorithm.
T1 T2
TS(T1)=10 TS(T2)=20
1. A1 = Read(X)
2. A1 = A1 – k
3. Write(X, A1)
1. A1 = Read(X)
2. A1 = A1* 1.01
3. Write(X, A1)
4. A2 = Read(Y)
5. A2 = A2 * 1.01
6. Write(Y, A2)
4. A2 = Read(Y)
5. A2 = A2 + k
6. Write(Y, A2)
Cont’d
If WTS(X) < TS(T), then delay T until the transaction T’ that wrote
X has terminated (committed or aborted).
Strict Timestamp Ordering
If RTS(X) <= TS(T) and WTS(X) <= TS(T), then delay T until the
transaction T’ that wrote and read X has terminated (committed or
aborted).
3. Multi version Concurrency Control (MVCC)
When any transaction writes an item it writes a new version and the
old version of the item is retained.
Disadvantage
More storage (RAM and Disk) is required to maintain multiple versions.
Multi version 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 RTS (read timestamp) and a WTS (write timestamp) are
associated.
WTS(Xk+1) = TS(T).
RTS(Xk+1) = TS(T).
Note:
The techniques are called optimistic because they assume that little
interference will occur and there is no need to do checking during
transaction execution.
Thank you!