Concurrency Control in DBMS
Concurrency Control in DBMS
When several transactions execute concurrently without any rules and protocols, various problems arise that may harm
the data integrity of several databases. These problems are known as concurrency control problems. Therefore several
rules are designed, to maintain consistency in the transactions while they are executing concurrently which are known
as concurrency control protocols.
What is concurrency control in DBMS?
A transaction is a single reasonable unit of work that can retrieve or may change the data of a database. Executing each
transaction individually increases the waiting time for the other transactions and the overall execution also gets delayed.
Hence, to increase the throughput and to reduce the waiting time, transactions are executed concurrently.
Example: Suppose, between two railway stations, A and B, 5 trains have to travel, if all the trains are set in a row and
only one train is allowed to move from station A to B and others have to wait for the first train to reach its destination
then it will take a lot of time for all the trains to travel from station A to B. To reduce time all the trains should be
allowed to move concurrently from station A to B ensuring no risk of collision between them.
When several transactions execute simultaneously, then there is a risk of violation of the data integrity of several
databases. Concurrency Control in DBMS is a procedure of managing simultaneous transactions ensuring
their atomicity, isolation, consistency and serializability.
Concurrency Control Problems
Several problems that arise when numerous transactions execute simultaneously in a random manner are referred to as
Concurrency Control Problems.
Dirty Read Problem
The dirty read problem in DBMS occurs when a transaction reads the data that has been updated by another transaction
that is still uncommitted. It arises due to multiple uncommitted transactions executing simultaneously.
Example: Consider two transactions A and B performing read/write operations on a data DT in the database DB. The
current value of DT is 1000: The following table shows the read/write operations in A and B transactions
Transaction A reads the value of data DT as 1000 and modifies it to 1500 which gets stored in the temporary buffer.
The transaction B reads the data DT as 1500 and commits it and the value of DT permanently gets changed to 1500 in
the database DB. Then some server errors occur in transaction A and it wants to get rollback to its initial value, i.e.,
1000 and then the dirty read problem occurs.
Unrepeatable Read Problem
The unrepeatable read problem occurs when two or more different values of the same data are read during the read
operations in the same transaction.
Example: Consider two transactions A and B performing read/write operations on a data DT in the database DB. The
current value of DT is 1000: The following table shows the read/write operations in A and B transactions.
Transaction A and B initially read the value of DT as 1000. Transaction A modifies the value of DT from 1000 to 1500
and then again transaction B reads the value and finds it to be 1500. Transaction B finds two different values of DT in
its two different read operations.
Transaction B initially reads the value of DT as 1000. Transaction A deletes the data DT from the database DB and
then again transaction B reads the value and finds an error saying the data DT does not exist in the database DB.
The two methods outlined below can be used to convert between the locks:
1. Conversion from a read lock to a write lock is an upgrade.
2. Conversion from a write lock to a read lock is a downgrade.
Types of Lock-Based Protocols
There are basically four lock based protocols in DBMS namely Simplistic Lock Protocol, Pre-claiming Lock
Protocol, Two-phase Locking Protocol, and Strict Two-Phase Locking Protocol. Let's go through each of these
lock-based protocols in detail.
Simplistic Lock Protocol
The simplistic method is defined as the most fundamental method of securing data during a transaction. Simple
lock-based protocols allow all transactions to lock the data before inserting, deleting, or updating it. After the
transaction is completed, the data item will be unlocked.
Pre-Claiming Lock Protocol
Pre-claiming Lock Protocols are known to assess transactions to determine which data elements require locks. Prior
to actually starting the transaction, it asks the Database management system for all of the locks on all of the data
items. The pre-claiming protocol permits the transaction to commence if all of the locks are obtained. Whenever
the transaction is finished, the lock is released. This protocol permits the transaction to roll back if all of the locks
are not granted and then waits until all of the locks are granted.
The transaction table is shown in the example associated with transaction T1 and transaction T2. It represents the
schedule produced using validation protocol.
The concurrent transaction process starts with T1 with a reading operation as Read (A) where A is the numeric data
element in the database. In the next step, the transaction T2 also reads the same data variable A after some time.
Transaction T2 performs an arithmetic operation by subtracting constant value 40 from the variable A. It is represented
as A=A-40 in the transaction table. The next step is a read operation on transaction T2 where it’s reading another
numerical value of variable B as the Read (B). After the read operation completed, the transaction T2 immediately
performs an arithmetic operation on the variable B. It uses the addition operator ‘+’ for adding a constant value as 80
to variable B. The addition operation is represented as B=B+80 in the transaction table.
In the next step of the concurrent transaction, T1 reads the variable B with operation Read (B). Now the validation
based protocol comes into the action in the transaction T1 that validates the time stamp of the start phase of T2 lesser
than the finishing phase time stamp of Transaction T1 and that is a lesser timestamp as the validate phase of Transaction
T2.
Similarly, in the Transaction T2, the validation based protocol validates the timestamps. In the example shown in the
table indicates both the validation based protocol is provided with a valid result based on the timestamp condition. And,
as the conclusive operations write operations are performed by the transaction T2 using Write (A) and Write (B)
statements.
Time Tc Tf
T1
T2
T3
T4
1
Page 5 2
Page 1
Page 4 3
Page 2 5 4
Page 3
6
Page 6
Current Shadow
Page Tale Pages Page Table
1
Page 5(old)
2
Page 1
3
Page 4
4
Page 2(old)
5
Page 3
6
Page 6
Page 2(new)
Page 5(new)
Advantages
No overhead of maintaining transaction log.
Recovery is quite faster, as there is no any redo or undo operations required.
Disadvantages
Copying the entire page table very expensive.
Data are scattered or fragmented.
After each transaction, free pages need to be collected by garbage collector. Difficult to extend
this technique to allow concurrent transaction
What is deadlock? Explain wait-for-graph. When it occurs?
OR
Define deadlock. Explain wait-for-graph. Explain different conditions that lead
to deadlock.
Deadlock-A deadlock is a condition when two or more transactions are executing and each transaction is
waiting for the other to finish but none of them are ever finished. So all the transactions will wait for infinite
time and not a single transaction is completed.
Wait-for-graph
Table 1
Held by Wait for
Transaction 1 Transaction 2
R1 R3 R1 R3
P1 P2 P3 P1 P2 P3
R2 R4 R2 R4
No cycle (circular chain) Cycle (circular chain) created (P2, R3, P3, R4, P2) So
created So no deadlock deadlock
When a deadlock is detected, the system must recover from the deadlock.
The most common solution is to roll back one or more transactions to break the deadlock. Choosing which
transaction to abort is known as Victim Selection.
In the above wait-for graph transactions R3 and R4 are deadlocked.
In order to remove deadlock one of the transaction out of these two (R3, R4) transactions must be roll backed.
We should rollback those transactions that will incur the minimum cost.
When a deadlock is detected, the choice of which transaction to abort can be made using following criteria:
The transaction which have the fewest locks the transaction that has done the least work the transaction that
is farthest from completion
Explain deadlock prevention methods.
OR
Explain methods to prevent deadlock.
Deadlock prevention
A protocols ensure that the system will never enter into a deadlock state. Some prevention
strategies :
Require that each transaction locks all its data items before it begins
execution (predeclaration).
Impose partial ordering of all data items and require that a transaction
canlock data items only in the order specified by the partial.
Following schemes use transaction timestamps for the sake of deadlock
prevention alone.
1. Wait-die scheme — non-preemptive
• If an older transaction is requesting a resource which is held by younger
transaction, then older transaction is allowed to wait for it till it is available.
• If an younger transaction is requesting a resource which is held by older
transaction, then younger transaction is killed.
2. Wound-wait scheme — preemptive
• If an older transaction is requesting a resource which is held by younger
transaction, then older transaction forces younger transaction to kill the
transaction and release the resource.
• If an younger transaction is requesting a resource which is held by older
transaction, then younger transaction is allowed to wait till older
transaction will releases it.
3. Timeout-Based Schemes :
• A transaction waits for a lock only for a specified amount of time. After
that, the wait times out and the transaction is rolled back. So deadlocks
never occur.
• Simple to implement; but difficult to determine good value of the timeout
interval.
Wait/Die Wound/Wait
O needs a resource held by Y O waits Y dies
Y needs a resource held by O Y dies Y waits
Lock Based Protocol in DBMS
The database management system (DBMS) stores data that can connect with one another as well as can
be altered at any point. There are instances where more than one user may attempt to access the same
data item simultaneously, resulting in concurrency.
As a result, there is a requirement to handle concurrency in order to handle the concurrent processing
of transactions across many databases in the picture. Lock-based protocol in DBMS is an example of
such an approach.