Unit 3
Unit 3
CONCURRENCY CONTROL
The system must control the interaction among the concurrent transactions. This
control is achieve through one of concurrency control schemes. The concurrency control
schemes are based on the serializability property.
Several types of locks can be used in concurrency control. Among them two types are
important.
1. Binary locks.
2. Shared and Exclusive locks.
[Link] Binary Locks
A binary lock can have two states or values locked and unlocked (0 and 1, for
simplicity).
A distinct lock is associated with each data base item X.
If the value of the lock on X is 1, item X cannot be accessed by a database
operation that requests the item.
If the value of the lock on X is 0, the item can be accessed when
Concurrency Control 5.2
requested. We refer to the value of the lock associated with item X as Lock(X).
Two operations, lock_item and unlock_item must be included in the
transactions when binary locking is used.
A transaction requests access to an item X by issuing a lock_item(X)
operation.
If lock(X) = 1, the transaction is forced to wait otherwise, the transaction
sets lock(X) : = 1 (locks the item) and is allowed access
When the transaction is through using the item, it issues an unlock_item (X)
operation, which sets lock (X) : = 0 (unlocks the item) so that X may be accessed by
other transactions.
Hence a binary lock enforces mutual exclusion on the data item.
A description of the lock_item (X) and unlock-item (X) operations is shown below:
lock_item(X):
B : if lock(X) = 0 (* item is unlocked *)
then lock(X) 1 (*lock the item *)
else begin
wait (until lock(X) = 0 and the lock manager wakes up the transaction);
go to B;
end;
unlock_item(X):
lock(X) 0 (* unlock the item *)
if any transaction are waiting then wakeup one of the waiting transactions.
Lock and unlock operation for binary locks
In above algorithm, the wait command within the lock_item(X) operation is usually
implemented by putting the transaction on a waiting queue for the item X until X is unlocked
and the transaction is granted access to it. Other transactions that also want to access X are
placed on the same queue.
5.3 Database Management Systems
Hence, the wait command is considered to be outside the lock-item operation. The
DBMS has a lock manager subsystem to keep track of and control access to locks.
When the binary locking scheme is used, every transaction must obey the following
rules:
lock, then we say mode A is compatible with mode B such a function is represented by a
matrix. The matrix is shown in Fig. 5.1.
S S
S true false
X false false
Example: Let A and B be two accounts that are accessed by transactions T1 and T2. Transaction
T1 transfers $50 from account B to account A. It is shown in Fig. [Link] T2 displays
the total amount of money in accounts A and B. It is shown in Fig. 6.4.
Suppose that the values of accounts A and B are $100 and $200, respectively.
If these two transactions are executed serially, either in the order T1, T2 or the order T2, T1, then
transaction T2 will display the value $300.
In this case, transaction T2 displays $250, which is incorrect. The reason for this
mistake is that the transaction T1 unlocked data item B too early, as a result of which T2 saw an
inconsistent state.
Locking can lead to an undesirable situation. Consider the schedule 2 of Fig. 6.6. Here
T3 is holding an exclusive-mode lock on B and T4 is requesting a shared mode lock on B, T4 is
waiting for T3 to unlock B. Similarly, T4 is holding a shared mode lock on A and T3 is
requesting an exclusive-mode lock on A, T3 is wating for T4 to unlock A.
Thus, in this situation neither of transactions can proceed with normal execution. This
situation is called deadlock. When deadlock occurs, the system must rollback one of the two
transactions. Once a transaction has been rolled back, the data items that were locked by that
transaction are unlocked. These data items are then available to the other transaction, which
can continue with its execution.
T1 T2 Concurrency-control
manager
lock-X(B)
grant – X(B, T1)
read(B)
B: = B – 50
write(B)
unlock(B)
lock-S(A)
grant – S(A, T2)
read (A)
unlock (A)
lock-S(B)
T3 T3
lock-X(B)
read (B)
B: = B-50
write(B)
lock-S(A)
read (A)
lock-S(B)
lock-X(A)
Each transaction in the system should follow a set of rules, called locking protocol,
indicating when a transaction may lock and unlock each of the data items. Locking protocol
restricts the number of possible schedules. The set of all such schedules is a proper subset of
all possible serializable schedules.
When a transaction requests a lock on the data item in a particular mode and no other
transaction has a lock on the same data item in a conflicting mode, the lock can be granted.
However, care must be taken while granting the locks.
For example, consider a transaction T2 has a shared mode lock on data item Q, and
another transaction T1 requests an exclusive-mode lock on the same data item. In this case, T1
has to wait for T2 to release the shared - mode lock. Mean while, another transaction T3
requests the shared mode lock on Q. 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 releases the lock, but still T1 has to wait for T3 to release the lock.
Thus it is possible that sequence of transactions that each requests a shared-mode lock on Q,
and each transaction releases the lock a short while after it is granted, but T1 may never gets
the exclusive mode lock on the data item. The transaction T1 may never make progress 1 and
is said to be starved.
Starvation of transaction can be avoided by granting locks in the following manner.
When a transaction Ti requests a lock on a data item Q in a particular mode M, the
concurrency control manager grants the lock provided that
5.7 Database Management Systems
read (B);
B: = B-50;
write (B);
lock-X (A);
read (A);
A: = A=50;
write (A);
unlock (B);
unlock (A);
T5 T6 T5
lock-X(A)
read(A)
lock-S(B)
read(B)
write(A)
unlock(A)
lock-X(A)
read(A)
write(A)
unlock(A)
lock-S(A)
read(B)
Strict two-phase locking and rigorous two-phase locking (with lock conversions) are
used extensively in commercial database systems.
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.
When Ti 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) instruction, 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 timestamps of the transactions determine the serializability order. Thus, if TS(Ti)
< TS(Tj), then the system must ensure that the produced schedule is equivalent to a serial
schedule in which transaction Ti appears before transaction Tj.
To implement this scheme, two timestamps associated with each data item Q.
(i) W-timestamp (Q) denotes the largest timestamp of any transaction that
executed write (Q) successfully.
(ii) R-timestamp (Q) denotes the largest timestamp of any transaction that
executed read (Q) successfully.
These time stamps are updated whenever a new read(Q) or write(Q) instruction is
executed.
5.4.2 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:
(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).
(a) If TS(Ti) < R-timestamp (Q), then the value of Q that Ti is producing was
needed previously, and the system assumed that the value would never be
produced. Hence, the system rejects the write operation and rolls Ti back.
(c) Otherwise, the system executes the write operation and sets W-timestamp
(Q) to TS( Ti ).
Example: Consider two transactions T14 and T15. Transaction T14 displays the sum of
acocunts A and B, and transaction T15 transfers $50 from account B to account A and displays
the sum of both.
5.11 Database Management Systems
T14 T15
read (B)
read (B)
B: =B-50
write (B)
read (A)
read (A)
display (A+B)
A: = A+50
write (A)
display (A+B)
As shown in Fig. 6.10, in schedule 3, TS(T14) < TS(T15) and the schedule is possible
under timestamp protocol.
Advantages
1. The timestamp ordering protocol ensures conflict serializability. This is
because conflicting operations are processed in timestamp order.
Disadvantages
T16 T17
read (Q)
write (Q)
write (Q)
Fig.5.10 Schedule 4
Here T16 starts before T17, therefore TS(T16) < TS(T17). The read (Q) operation of
T16 succeeds, similarly the write (Q) operation of T17. When T16 attempts its write (Q)
operation, we find TS(T16) < W-timestamp (Q), since W-timestamp (Q) = TS(T17). Thus, the
write (Q) is rejected by the system and T16 is rolled back.
Any transaction Ti with TS(Ti) < TS(T17) that attempts a read (Q) will be rolled back,
since TS(Ti) < W-timestamp (Q).
3. Otherwise, the system executes the write operation and sets W-timestamp
(Q) to TS(Ti).
5.13 Database Management Systems
The difference between timestamp ordering process and Thomas’ write rule 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 those cases where
TS(Ti) R-timestamp (Q), we ignore the obsolete write. This modification to the
timestamp-ordering protocol is called Thomas write rule.
5.5 DEADLOCK
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.
In other words, there exists a set of waiting transactions 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. In such a
situation, none of the transactions can make progress.
There are two principal methods for dealing with the deadlock problem.
(i) Deadlock prevention: This approach ensures that system will never enter in
deadlock state.
(ii) Deadlock detection and recovery: This approach tries to recover from
deadlock if system enters in deadlock state.
5.5.1 Deadlock Prevention
There are two approaches for deadlock prevention:
(a) One approach ensures that no cyclic waits can occur by ordering the requests
for locks, or requiring all locks to be acquired together. This approach
requires that each transaction locks all data items before it begins execution. It is
required that, either all data items should be locked in one step, or none should be
locked.
Disadvantages of this approach are
1. It is hard to predict before the transaction begins, what data items need to be
locked.
2. Data-item utilization may be very low, since many of the data items may be
locked but unused for long time.
(b) The second approach for deadlock prevention is to use pre-emption and
transaction rollbacks. In pre-emption when a transaction T2 requests a lock
that transaction T1 holds, the lock granted to T1 may be pre-empted by rolling
Concurrency Control 5.14
Returning to same example, if T1 requests a data item hold by T2, then the data
item will be preempted by T2, and T2 will be rolled back. If T3 requests a data
item held by T2 then T3 will wait.
Differences between two schemes
In the wait-die scheme, an older transaction must wait for a younger one to release
its data item. Thus, the older the transaction gets, the more it tends to wait. By
contrast, in the wound-wait scheme, an older transaction never waits for a younger
transaction.
In the wait-die scheme, if a transaction Ti dies and is rolled back because it
requested a data item held by transaction Tj , then Ti may re-issue the same
sequence of requests when it is restarted. If the data item is still held by Tj, then Ti
will die again.
Thus Ti may die several times before acquiring the needed data item. Contrast this
series of events with what happens in the wound-wait scheme Transaction Ti is wounded and
rolled back because Tj requested a data item that it holds. When Ti is restarted and requests the
data item now being held by Tj, Ti waits. Thus, there may be fewer rollbacks in the wound-
wait scheme.
5.15 Database Management Systems
The major problem with both of these schemes is that unnecessary rollbacks may
occur.
5.5.2 Timeout-Based Schemes
This approach for deadlock handling is based on lock timeouts. In this approach, a
transaction that has requested a lock waits for at most a specified amount of time. If the lock
has not been granted within that time, the transaction is said to be time out, and it rolls back
itself and restarts. Thus, if there was a deadlock one or more transactions involved in the
deadlock will time out and roll back, allowing the others to proceed.
Advantages
1. This scheme is easy to implement.
2. It works well if transactions are short, and if long, waits are likely to be due to
deadlocks.
Disadvantages
1. It is hard to decide how long a transaction should wait. Too long waits results
in unnecessary delays once a deadlock has occurred, and too short a wait results
in transaction rollbacks even when there is no deadlock.
2. Starvation is also possible with this scheme.
5.5.4 Deadlock Detection and Recovery
This approach uses an algorithm that examines the state of the system periodically to
determine whether a deadlock has occurred. If one has occurred, then the system attempts to
recover from the deadlock.
[Link] Deadlock Detection
Deadlocks can be described in terms of directed graphs called a wait-for graph. This
graph consists of a pair G = (V, E), where
V-set of vertices consists of all transactions in the system.
E-set of edges.
T26 T28
T25
T27
T26 T28
T25
T27
When a deadlock detection algorithm determines that a deadlock exists, the system
must recover from the deadlock. The most common solution is to rollback one or more
transactions to break the deadlock.
1. Selection of a victim
(i) How long the transaction has computed, and how much longer the
transaction will compute before it completes its designated task ?
(ii) How many data items the transaction has used ?
(iii) How many more data items the transaction needs for it to complete ?
(iv) How many transactions will be involved in the rollback?
2. Rollback
Once we have decided to roll back particular transaction, we must determine how for this
transaction should be rolled back. The solutions are:
(i) Total rollback: Abort the transaction and then restarts it.
(ii) Partial rollback: It is more effective to roll back the transaction only as far as necessary
to break the deadlock. It requires the system to maintain additional information about the
state of all the running transactions. Specifically, the sequence of lock requests/grants and
updates performed by the transaction needs to be recorded.
3. Starvation
It is possible that, same transaction will be rolled back number of times to break the
deadlock. As a result, this transaction never completes its designated task, thus there is
starvation. To avoid this, we must ensure that transactions can be picked as a victim only
a small number of times.
Protocols that obey this are referred to as non-blocking protocols. In the following
two sections, we consider two common commit protocols suitable for distributed
DBMSs: two-phase commit (2PC) and three-phase commit (3PC), a non-blocking
protocol.
Assume that every global transaction has one site that acts as coordinator (or
transaction manager) for that transaction, which is generally the site at which the
transaction was initiated. Sites at which the global transaction has agents are called
participants (or resource managers).
Concurrency Control 5.18
Assume that the coordinator knows the identity of all participants and that each
participant knows the identity of the coordinator but not necessarily of the other
participants.
(ii) Participant
A participant can be in one of four states during the commit process:
INITIAL
PREPARED
ABORTED
COMMITTED
as shown in the state transition diagram in Figure. However, a participant may time out only
in the first two states as follows:
Timeout in the INITIAL state-The participant is waiting for a PREPARE message
from the coordinator, which implies that the coordinator must have failed while in the
INITIAL state. In this case, the participant can unilaterally abort the transaction. If it
subsequently receives a PREPARE message, it can either ignore it, in which case the
coordinator times out and aborts the global transaction, or it can send an ABORT message to
the coordinator.
Timeout in the PREPARED state-The participant is waiting for an instruction to
globally commit or abort the transaction. The participant must have voted to commit the
transaction, so it cannot change its vote and abort the transaction. Equally well, it cannot go
ahead and commit the transaction, as the global decision may be to abort.
A SAVEPOINT is a point in a transaction when you can roll the transaction back to a
certain point without rolling back the entire transaction.
The syntax for a SAVEPOINT command is as shown below.
SAVEPOINT SAVEPOINT_NAME;
This command serves only in the creation of a SAVEPOINT among all the
transactional statements. The ROLLBACK command is used to undo a group of transactions.
The syntax for rolling back to a SAVEPOINT is as shown below.
ROLLBACK TO SAVEPOINT_NAME;
Following is an example where you plan to delete the three different records from the
CUSTOMERS table. You want to create a SAVEPOINT before each delete, so that you can
ROLLBACK to any SAVEPOINT at any time to return the appropriate data to its original
state.
Example
Consider the CUSTOMERS table having the following records.
ID NAME AGE ADDRESS SALARY
1 Suresh 23 Chennai 5400
2 Mano 22 Cochin 6500
3 Subhiksha 23 Newyork 4500
4 Malar 24 Bangalore 4300
5 Sarath 22 Trichy 7500
6 Krishna 32 Coimbatore 6600
7 Ranchana 21 Hyderabad 5500
Isolation levels determine the type of phenomena that can occur during the execution
of concurrent transactions. eDeveloper sets this property only for the following databases:
MSSQL, Informix and DB2.
Three phenomena define SQL Isolation levels for a transaction:
Dirty Reads returns different results within a single transaction when an SQL operation an
uncommitted or modified record created by another transaction. Dirty Reads increases
concurrency, but reduces consistency.
Non-Repeatable Reads returns different results within a single transaction when an SQL
operation reads the same row in a table twice. Non-Repeatable Reads can occur when another
transaction modifies and commits a change to the row between transaction reads. Non-
repeatable reads increases consistency, but reduces concurrency.
Phantoms returns different results within a single transaction when an SQL operation
retrieves a range of data values twice. Phantoms can occur if another transaction inserted a
new record and committed the insertion between executions of the range retrieval.
Each Isolation level differs in the phenomena it allows:
Read uncommitted X X X
Read committed -- X X
Repeatable read -- -- X
Serializable -- -- --
Read Uncommitted – Read Uncommitted is the lowest isolation level. In this level, one
transaction may read not yet commited changes made by other transaction, thereby
allowing dirty reads. In this level, transactions are not isolated from each other.
5.25 Database Management Systems
Read Committed – This isolation level guarantees that any data read is committed at the
moment it is read. Thus it does not allows dirty read. The transaction hold a read or write
lock on the current row, and thus prevent other rows from reading, updating or deleting it.
Repeatable Read – This is the most restrictive isolation level. The transaction holds read
locks on all rows it references and write locks on all rows it inserts, updates, or deletes.
Since other transaction cannot read, update or delete these rows, consequently it avoids
non repeatable read.
There are two types of techniques, which can help a DBMS in recovering as well as
maintaining the atomicity of a transaction −
Maintaining the logs of each transaction, and writing them onto some stable storage
before actually modifying the database.
Maintaining shadow paging, where the changes are done on a volatile memory, and
later, the actual database is updated.
5.6.7 Log-based Recovery
Log is a sequence of records, which maintains the records of actions performed by a
transaction. It is important that the logs are written prior to the actual modification and stored
on a stable storage media, which is failsafe.
Log-based recovery works as follows −
The log file is kept on a stable storage media.
When a transaction enters the system and starts execution, it writes a log about it.
<Tn, Start>
When the transaction modifies an item X, it write logs as follows −
<Tn, X, V1, V2>
It reads Tn has changed the value of X, from V1 to V2.
When the transaction finishes, it logs −
<Tn, commit>
system and stored permanently in a storage disk. Checkpoint declares a point before which
the DBMS was in consistent state, and all the transactions were committed.
[Link] Recovery
When a system with concurrent transactions crashes and recovers, it behaves in the
following manner −
If the recovery system sees a log with <Tn, Start> and <Tn, Commit> or just <Tn,
Commit>, it puts the transaction in the redo-list.
If the recovery system sees a log with <Tn, Start> but no commit or abort log found, it
puts the transaction in undo-list.
All the transactions in the undo-list are then undone and their logs are removed. All the
transactions in the redo-list and their previous logs are removed and then redone before
saving their logs.
5.29 Database Management Systems
PART – A
Shared Lock: If a Transaction Ti has obtained a shared mode lock on data item Q,
then Ti can read, but cannot write Q.
Exclusive Lock: If the transaction Ti has obtained a shared mode Lock on item Q,
then Ti can read and also write Q.
6. Define deadlock?
Deadlock which defined as Neither of the transaction can ever proceed with its
normal execution.
7. Define the phases of two phase locking protocol
Growing phase: a transaction may obtain locks but not release any lock.
Shrinking phase: a transaction may release locks but may not obtain any new locks.
8. What are the time stamps associated with each data item?
Concurrency Control 5.30
W-timestamp (Q) denotes the largest time stamp if any transaction that
executed WRITE (Q) successfully.
R-timestamp (Q) denotes the largest time stamp if any transaction that
executed READ (Q) successfully.
Deadlock Prevention:
made in the system and how they are handled (system design).
The goal is to ensure that at least one of the necessary conditions for
deadlock can never hold.
Deadlock Avoidance:
PART – B