DBMS Unit-5
DBMS Unit-5
When several transactions execute concurrently in the database, however, the isola-
tion property may no longer be preserved. To ensure that it is, the system must control
the interaction among the concurrent transactions. The mechanism used to control the
interaction of transactions is called concurrency control scheme.
There are number of concurrency control schemes.
5. Multi-version protocol
Note: The transaction makes the lock request to the concurrency control manager.
Transaction can process only after lock request is granted.
Compatibility function
Given a set of lock modes, we can define a compatibility function on them as follows.
Let A and B represent arbitrary lock modes. Suppose that a transaction Ti requests a
lock of mode A on item Q on which transaction Tj (Ti 6= Tj ) currently holds a lock of
mode B. If transaction Ti can be granted a lock on Q immediately, in spite of the presence
of the mode B lock, then we say mode A is compatible with mode B. Such a function can
be represented conveniently by a matrix. An element comp(A, B) of the matrix has the
value true if and only if mode A is compatible with mode B.
Note:
2
• A transaction requests an exclusive lock on data item Q by executing the lock-X(Q)
instruction.
Note:
To access a data item, transaction Ti 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, Ti is made to wait until all incompatible locks held by other transactions
have been released.
Example: Consider the following two transactions T1 and T2 with locking modes.
Note: When deadlock occurs, the system must roll back one of the two transactions.
Locking protocol This is the set of rules indicating when a transaction may lock and
unlock each of the data items.
Note: A schedule S is legal under a given locking protocol if S is a possible schedule
for a set of transactions that follow the rules of the locking protocol.
Note: A locking protocol ensures conflict serializability if and only if all legal schedules
are conflict serializable.
Starvation
Suppose a transaction T2 has a shared-mode lock on a data item, and another transaction
T1 requests an exclusive-mode lock on the data item. Clearly, T1 has to wait for T2 to
release the shared-mode lock. Meanwhile, a transaction T3 may request a shared-mode
lock on the same data item. The lock request is compatible with the lock granted to
T2 , so T3 may be granted the shared-mode lock. At this point T2 may release the lock,
but still T1 has to wait for T3 to finish. But again, there may be a new transaction T4
that requests a shared-mode lock on the same data item, and is granted the lock before
3
4
T3 releases it. In fact, it is possible that there is a sequence of transactions that each
requests a shared-mode lock on the data item, and each transaction releases the lock a
short while after it is granted, but T1 never gets the exclusive-mode lock on the data item.
The transaction T1 may never make progress, and is said to be starved. This situation is
said to be starvation.
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.
Example: Transactions T3 and T4 are locked in two phase. While, transactions T1 and
T2 are not locked in two phase.
Lock point: Lock point of a transaction is a point in the schedule where the transaction
has obtained its final lock (the end of its growing phase).
Note: Two-phase locking does not ensure freedom from deadlock.
Observe that transactions T3 and T4 are in two phase, but, in schedule 2, they are
deadlocked.
Lock Conversion
Upgrade: We denote conversion from shared to exclusive modes by upgrade.
5
Downgrade: We denote conversion from exclusive to shared by downgrade.
Note: Lock conversion cannot be allowed arbitrarily. Rather, upgrading can take
place in only the growing phase, whereas downgrading can take place in only the shrink-
ing phase.
Note: Strict two-phase locking and rigorous two-phase locking (with lock conversions)
are used extensively in commercial database systems.
Note: A simple but widely used scheme automatically generates the appropriate lock
and unlock instructions for a transaction, on the basis of read and write requests from
the transaction:
• When a transaction Ti issues a read(Q) operation, the system issues a lock- S(Q)
instruction followed by the read(Q) instruction.
• WhenTi issues a write(Q) operation, the system checks to see whether Ti already
holds a shared lock on Q. If it does, then the system issues an upgrade( Q) instruc-
tion, followed by the write(Q) instruction. Otherwise, the system issues a lock-X(Q)
instruction, followed by the write(Q) instruction.
• All locks obtained by a transaction are unlocked after that transaction commits or
aborts.
The partial ordering implies that the set D may now be viewed as a directed acyclic
graph, called a database graph. Here, we will consider graph with rooted tree. Therefore,
we will study tree protocol.
In the tree protocol, the only lock instruction allowed is lock-X. Each transaction
Ti can lock a data item at most once, and must observe the following rules:
4. A data item that has been locked and unlocked by Ti cannot subsequently be
relocked by Ti .
All schedules that are legal under the tree protocol are conflict serializable.
6
7
T10 : lock-X(B); lock-X(E); lock-X(D); unlock(B); unlock(E); lock-X(G); unlock(D); un-
lock(G).
T11 : lock-X(D); lock-X(H); unlock(D); unlock(H).
T12 : lock-X(B); lock-X(E); unlock(E); unlock(B).
T13 : lock-X(D); lock-X(H); unlock(D); unlock(H).
One possible schedule in which these four transactions participated appears in the fol-
lowing figure:-
Observe that the schedule in this figure is conflict serializable. It can be shown not only
that the tree protocol ensures conflict serializability, but also that this protocol ensures
freedom from deadlock.
The tree protocol in this figure does not ensure recoverability and cascadelessness.
Advantage:
1. The tree-locking protocol has an advantage over the two-phase locking protocol in
that, unlike two-phase locking, it is deadlock-free, so no rollbacks are required.
2. The tree-locking protocol has another advantage over the two-phase locking protocol
in that unlocking may occur earlier. Earlier unlocking may lead to shorter waiting
times, and to an increase in concurrency.
To implement this scheme, we associate with each data item Q two timestamp values:
• W-timestamp(Q) denotes the largest timestamp of any transaction that executed
write(Q) successfully.
• R-timestamp(Q) denotes the largest timestamp of any transaction that executed
read(Q) successfully.
These timestamps are updated whenever a new read(Q) or write(Q) instruction is exe-
cuted.
8
1.3.1 Timestamp-Ordering Protocol
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 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 system rejects the write operation and
rolls Ti back.
(b) If TS(Ti) < W-timestamp(Q), then 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 ).
If a transaction Ti is rolled back by the concurrency-control scheme as result of issuance
of either a read or write operation, the system assigns it a new timestamp and restarts
it.
Example: Consider transactions T14 and T15 . Transaction T14 displays the contents of
accounts A and B:
T14 : read(B);
read(A);
display(A + B).
Transaction T15 transfers $50 from account A to account B, and then displays the con-
tents of both:
T15 : read(B);
B := B - 50;
write(B);
read(A);
A := A + 50;
write(A);
display(A + B).
9
10
4. This protocol can generate schedules that are not recoverable.
2 Multiple Granularity
Consider the following granularity hierarchy. This tree consists of four levels of nodes.
The highest level represents the entire database. Below it are nodes of type area; the
database consists of exactly these areas. Each area in turn has nodes of type file as its
children. Each area contains exactly those files that are its child nodes. No file is in more
than one area. Finally, each file has nodes of type record. As before, the file consists of
exactly those records that are its child nodes, and no record can be present in more than
one file.
This protocol uses the following compatibility matrix to lock the data items. There
is an intention mode associated with shared mode, and there is one with exclusive mode.
If a node is locked in intention-shared (IS) mode, explicit locking is being done at a
lower level of the tree, but with only shared-mode locks. Similarly, if a node is locked
in intention-exclusive (IX) mode, then explicit locking is being done at a lower level,
with exclusive-mode or shared-mode locks. Finally, if a node is locked in shared and
intention-exclusive (SIX) mode, the sub-tree rooted by that node is locked explicitly in
shared mode, and that explicit locking is being done at a lower level with exclusive-mode
locks.
11
12
4. It can lock a node Q in X, SIX, or IX mode only if it currently has the parent of Q
locked in either IX or SIX mode.
5. It can lock a node only if it has not previously unlocked any node (that is, Ti is two
phase).
6. It can unlock a node Q only if it currently has none of the children of Q locked.
Example:
Consider the tree shown in the above figure and these transactions:
• Suppose that transaction T18 reads record ra2 in file Fa . Then, T18 needs to lock
the database, area A1 , and Fa in IS mode (and in that order), and finally to lock
ra2 in S mode.
• Suppose that transaction T19 modifies record ra9 in file Fa . Then, T19 needs to lock
the database, area A1 , and file Fa in IX mode, and finally to lock ra2 in X mode.
• Suppose that transaction T20 reads all the records in file Fa . Then, T20 needs to
lock the database and area A1 (in that order) in IS mode, and finally to lock Fa in
S mode.
• Suppose that transaction T21 reads the entire database. It can do so after locking
the database in S mode.
Clearly, transactions T18 , T20 , and T21 can access the database concurrently. Transaction
T19 can execute concurrently with T18 , but not with either T20 or T21 .
This protocol enhances concurrency and reduces lock overhead. It is particularly use-
ful in applications that include a mix of
• Long transactions that produce reports from an entire file or set of files
3 Multiversion Schemes
In multiversion concurrency control schemes, each write(Q) operation creates a new ver-
sion of Q. When a transaction issues a read(Q) operation, the concurrencycontrol manager
selects one of the versions of Q to be read. The concurrency-control scheme must ensure
that the version to be read is selected in a manner that ensures serializability.
13
3.1 Multiversion Timestamp Ordering
With each data item Q, a sequence of versions < Q1 , Q2 , ..., Qm > is associated. Each
version Qk contains three data fields:
1. If transaction Ti issues a read(Q), then the value returned is the content of version
Qk .
Versions that are no longer needed are removed according to the following rule. Suppose
that there are two versions, Qk and Qj , of a data item, and that both versions have a
W-timestamp less than the timestamp of the oldest transaction in the system. Then, the
older of the two versions Qk and Qj will not be used again, and can be deleted.
Note:
4 Deadlock Handling
A system is in a deadlock state if there exists a set of transactions such that every trans-
action in the set is waiting for another transaction in the set. More precisely, 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. We can use
a deadlock prevention protocol to ensure that the system will never enter a deadlock
14
state. Alternatively, we can allow the system to enter a deadlock state, and then try to
recover by using a deadlock detection and deadlock recovery scheme.
Note: Prevention is commonly used if the probability that the system would enter a
deadlock state is relatively high; otherwise, detection and recovery are more efficient.
A deadlock exists in the system if and only if the wait-for graph contains a cycle. Each
transaction involved in the cycle is said to be deadlocked. To detect deadlocks, the sys-
tem needs to maintain the wait-for graph, and periodically to invoke an algorithm that
searches for a cycle in the graph.
15
• Transaction T25 is waiting for transactions T26 and T27 .
Since the graph has no cycle, the system is not in a deadlock state.
Suppose now that transaction T28 is requesting an item held by T27 . The edge T28 → T27 is
added to the wait-for graph, resulting in the new system state in following figure.
This time, the graph contains the cycle
T26 → T28 → T27 → T26 .
implying that transactions T26 , T27 , and T28 are all deadlocked.
(a) How long the transaction has computed, and how much longer the transaction
will compute before it completes its designated task.
(b) How many data items the transaction has used.
(c) How many more data items the transaction needs for it to complete.
(d) How many transactions will be involved in the rollback.
select sum(balance)
from account
where branch-name = ’Perryridge’
16
Transaction T29 requires access to all tuples of the account relation pertaining to the
Perryridge branch.
Let T30 be a transaction that executes the following SQL insertion:
Let S be a schedule involving T29 and T30 . We expect there to be potential for a conflict
for the following reason:
• If T29 uses the tuple newly inserted by T30 in computing sum(balance), then T29
read a value written by T30 . Thus, in a serial schedule equivalent to S, T30 must
come before T29 .
• If T29 does not use the tuple newly inserted by T30 in computing sum(balance), then
in a serial schedule equivalent to S, T29 must come before T30 .
The second of these two cases is curious. T29 and T29 do not access any tuple in common,
yet they conflict with each other! In effect, T29 and T29 conflict on a phantom tuple. If
concurrency control is performed at the tuple granularity, this conflict would go unde-
tected. This problem is called the phantom phenomenon.
To prevent the phantom phenomenon, we allow T29 to prevent other transactions from
creating new tuples in the account relation with branch-name = “Perryridge.”
2. Explain the phantom phenomena. Discuss a Time Stamp Protocol that avoids the
phantom phenomena.
5. What is deadlock? What are necessary conditions for it? How it can be detected
and recovered?
7. Write the salient features of graph based locking protocol with suitable example.
17
18
10. What is Two phase Locking (2PL)? Describe with the help of example.
11. What are multi version schemes of concurrency control? Describe with the help of
an example. Discuss the various Time stamping protocols for concurrency control
also.
16. Describe major problems associated with concurrent processing with examples.
What is the role of locks in avoiding these Problems.
17. Explain the phantom phenomenon. Devise a time stamp based protocol that avoids
the phantom phenomenon.
19