Uojk Cbouaj
Uojk Cbouaj
Uojk Cbouaj
Unit - V
Concurrency Control
Concurrency Control
1. Concurrent Execution of Transactions
o Transaction-processing systems usually allow multiple transactions to run
concurrently. Allowing multiple transactions to update data concurrently
causes several complications. However, there are two good reasons for
allowing concurrency:
o Improved throughput and resource utilization. A transaction consists
of many steps. Some involve I/O activity; others involve CPU activity. The
CPU I/O activities can be done in parallel. While a read or write on behalf of
one transaction is in progress on one disk, another transaction can be
running in the CPU, while another disk may be executing a read or write on
behalf of a third transaction. All of this increases the throughput of the
system—that is, the number of transactions executed in a given amount of
time. Correspondingly, the processor and disk utilization also increase.
This happens when the transaction T 2 read the data item A that has been
modified by another transaction T1, which has not yet committed. Such a
read is called a dirty read.
As an example, consider the following set of transactions (T 1& T2) and the
schedule for executing T1& T2.
T1: Transfers $100 from account A to account B
T2: Increments the balances of account A and account B by 10%
T1 T2
Read(B)
Write(B)
Commit
If the two transactions are allowed to execute as per the schedule shown above,
then the outcome of this execution will be different from the normalexecution like
if the two instructions are executed one after another. This type ofanomalies
leaves the database in an inconsistency state.
T1 T2
A has been
Read(A) Read(A) modified
Write(A)
Commit
Read(A)
Gets different Write(A)
value of A
Commit
RW conflict
Again, the execution of transactions T 1& T2 using the above schedule leads to
database inconsistency.
T1 T2
Write(X)
Write(Y)
Write(Y) Blind write
Write(X)
Commit
Commit
WW conflict
Again, the execution of transactions T 1& T2 using the above schedule leads to
database inconsistency.
3. Lock-Based Protocols
One way to ensure serializability is to require that data items be accessed in a
mutually exclusive manner; that is, while one transaction is accessing a data
item no other transaction can modify that data item. A DBMS typically uses a
locking protocol to achieve this.
Locks
There are various modes in which a data item may be locked.
1. Shared Lock: If a transaction Ti has obtained a shared-mode lock
(denoted by S) on item Q, then Ti can read, but cannot write, Q.
2. Exclusive Lock: If a transaction Ti has obtained an exclusive-mode lock
(denoted by X) on item Q, then Ti can both read and write Q.
Following figure shows compatibility of locks.
From the figure it is clear that shared lock is compatible with shared lock, but
not with exclusive lock. At any time, several shared-mode locks can be held
simultaneously (by different transactions) on a particular data item. A
subsequent exclusive-mode lock request has to wait until the currently held
shared-mode locks are released.
2. Shrinking phase. A transaction may release locks, but may not obtain any
new locks.
Initially, a transaction is in the growing phase. The transaction acquires locks
as needed. Once the transaction releases a lock, it enters the shrinking phase,
and it can issue no more lock requests.
T1 T2
Lock-X(A)
Read(A)
Write(A)
Lock-X(B)
Unlock(A)
Lock-X(A)
Read(A)
Write(A)
Read(B)
Write(B)
Unlock(B)
Lock-X(B)
Unlock(A)
Read(B)
Write(B)
Unlock(B)
Schedule – 1
As an example, consider the following set of transactions (T 1& T2)
T1: Transfers $100 from account A to account B
T2: Increments the balances of account A and account B by 10%
T1& T2can be executed using schedule – 1under Two-phase locking protocol.
Advantages
It ensures serializability
It is a simple and straightforward protocol.
Disadvantages
T1 T2
Lock-X(A)
Read(A)
Write(A)
Lock-X(B)
Read(B)
Write(B)
Unlock(A)
Unlock(B)
Lock-X(A)
Read(A)
Write(A)
Lock-X(B)
Read(B)
Write(B)
Unlock(A)
Unlock(B)
T1 T2
Lock-X(A)
Read(A)
Write(A)
Lock-X(B)
Read(B)
Write(B)
Unlock(A)
Unlock(B)
Lock-X(A)
Read(A)
Write(A)
Lock-X(B)
Read(B)
Write(B)
Unlock(A)
Unlock(B)
4. Timestamp-Based Protocols
Timestamps
With each transaction Ti in the system, we associate a unique fixed
timestamp, denoted by TS(Ti). This timestamp is assigned by the database
system before the transaction Ti starts execution.
Usually, the value of the system clock is used as the timestamp; that is, a
transaction’s timestamp is equal to the value of the system clock when the
transaction enters the system.
Department of Information Technology A.Y.2019-20 II-II GEC Page 9
Database Management Systems UNIT-V
The timestamp-ordering protocol ensures that any conflicting read and write
operations are executed in timestamp order. This protocol operates as follows:
1. Suppose that transaction Ti issues read(Q).
a. If TS(Ti) <W-timestamp(Q), then Ti needs to read a value of Q that
was already overwritten. Hence, the read operation is rejected,
and Ti is rolled back.
b. If TS(Ti) ≥W-timestamp(Q), then the read operation is executed,
and R-timestamp(Q) is set to the maximum of R-timestamp(Q) and
TS(Ti).
2. Suppose that transaction Ti issues write(Q).
a. If TS(Ti) <R-timestamp(Q), then the value of Q that Ti is producing
was needed previously. Hence, the system rejects the write
operation and rolls Ti back.
b. If TS(Ti) <W-timestamp(Q), then Ti is attempting to write an
obsolete value of Q. Hence, the system rejects this write operation
and rolls Ti back.
c. Otherwise, the system executes the write operation and sets W-
timestamp(Q) to TS(Ti).
Since T16 starts before T17, we shall assume that TS(T 16) < TS(T17). The read(Q)
operation of T16 succeeds, as does the write(Q) operation of T 17.When T16
attempts its write(Q) operation, we find that TS(T 16) < W-timestamp(Q). Thus,
the write(Q) by T16 is rejected and transaction T16 must be rolled back.
Although the rollback of T16 is required by the timestamp-ordering protocol, it
is unnecessary. Since T17 has already written Q, the value that T16 is
attempting to write is one that will never need to be read. Any transaction Ti
with TS(Ti) < TS(T17) that attempts a read(Q) will be rolled back, since TS(Ti) <
W-timestamp(Q). Any transaction Tj with TS(Tj ) > TS(T 17) must read the value
of Q written by T17, rather than the value written by T16.
This observation leads to a modified version of the timestamp-ordering
protocol in which obsolete write operations can be ignored under certain
circumstances.
The modification to the timestamp-ordering protocol is called Thomas’ write
rule and is given as;
Suppose that transaction Ti issues write(Q)
1. If TS(Ti) < R-timestamp(Q), then the value of Q that Ti is producing was
previously needed. Hence, the system rejects the write operation and
rolls Ti back.
2. If TS(Ti) < W-timestamp(Q), then Ti is attempting to write an obsolete
value of Q. Hence, this write operation can be ignored.
3. Otherwise, the system executes the write operation and sets W-
timestamp(Q) to TS(Ti).
The difference between these rules and the timestamp-ordering protocol lies
in the second rule. The timestamp-ordering protocol requires that Ti be rolled
back if Ti issues write(Q) and TS(Ti) <W-timestamp(Q). However, here, in
Thomas’ Write Rule, we ignore the obsolete write.
Thomas’ write rule makes use of view serializability and increases
concurrency.
5. Deadlock Handling
A system is in a deadlock state if there exists a set of transactions such that
every transaction in the set is waiting for another transaction in the set.
For example there exists a set of waiting transactions {T0, T1, . . ., Tn} such
that T0 is waiting for a data item that T1 holds, and T1 is waiting for a data
item that T2 holds, and . . ., and Tn−1 is waiting for a data item that Tn holds,
and Tn is waiting for a data item that T0 holds. None of the transactions can
make progress in such a situation.
There are two principal methods for dealing with the deadlock problem.
1. deadlock prevention
2. deadlock detection and deadlock recovery scheme.
Deadlock Prevention
For example, suppose that transactions T22, T23, and T24 have
timestamps 5, 10, and 15, respectively. If T22 requests a data item held by
T23, then T22 will wait. If T24 requests a data item held by T23, then T24
will be rolled back.
Example, with transactions T22, T23, and T24, if T22 requests a data item
held by T23, then the data item will be preempted from T23, and T23 will
be rolled back. If T24 requests a data item held by T23, then T24 will wait.