18 Concurrency Control
18 Concurrency Control
Quick Recap
Every transaction must follow ACID properties .
Concurrency control techniques are used to
ensure that the Isolation property for
concurrently executing transactions.
Why Concurrency Control management is
needed?
1 LOCK-S(A)
2 LOCK-S(A)
3 LOCK-X(B)
4 ……. ……
5 UNLOCK(A)
6 LOCK-X(C)
7 UNLOCK(B)
8 UNLOCK(A)
9 UNLOCK(C)
10 ……. ……
The Two-Phase Locking Protocol
• Transaction T1:
– Growing Phase is from steps 1-3.
– Shrinking Phase is from steps 5-7.
– Lock Point at 3
• Transaction T2:
– Growing Phase is from steps 2-6.
– Shrinking Phase is from steps 8-9.
– Lock Point at 6
The Two-Phase Locking Protocol
What is LOCK POINT ?
The Point at which the growing phase
ends, i.e., when transaction takes the final
lock it needs to carry on its work.
drawbacks of 2-PL.
Cascading Rollback is possible under 2-PL.
Deadlocks and Starvation is possible.
The Two-Phase Locking Protocol
The Two-Phase Locking Protocol ensures
Conflict Serializability but does not prevent
Cascading Rollback and Deadlock.
Types of 2PL
Strict 2PL,
Conservative 2PL and
Rigorous 2PL.
Strict 2-PL
All Exclusive(X) Locks held by the transaction
be released until after the Transaction
Commits.
Strict 2-PL ensures that our schedule is:
Recoverable
Cascadeless
Still Deadlocks are possible!
Rigorous 2-PL
All Exclusive(X) and Shared(S) Locks held by the
transaction be released until after the Transaction
Commits.
Rigorous 2-PL ensures that our schedule is
– Recoverable
– Cascadeless
still Deadlocks are possible!
The difference between Strict 2-PL and Rigorous
2-PL is that Rigorous is more restrictive and easy
to implement.
Conservative 2-PL/ Static 2-PL
• This protocol requires the transaction to lock all the items it
access before the Transaction begins execution by
predeclaring its read-set and write-set.
DEADLOCK IN DBMS
Deadlock in DBMS
In a database, a deadlock is an unwanted
situation in which two or more transactions
are waiting indefinitely for one another to give
up locks.
Deadlock is said to be one of the most feared
complications in DBMS as it brings the whole
system to a Halt.
Deadlock in DBMS
Suppose, Transaction T1 holds a lock on some
rows in the Students table and needs to
update some rows in the Grades table.
Simultaneously, Transaction T2 holds locks on
Grades table but needs to update the rows in the
Student table held by Transaction T1.
Now, the main problem arises. Transaction T1 will
wait for transaction T2 to give up lock, and
similarly transaction T2 will wait for transaction
T1 to give up lock.
Deadlock in DBMS
Deadlock in DBMS
As a consequence, All activity comes to a halt
and remains at a standstill forever unless the
DBMS detects the deadlock and aborts one of
the transactions.
Deadlock Avoidance
It is always better to avoid the deadlock rather than restarting or
aborting the database.
Deadlock avoidance method is suitable for smaller database
whereas deadlock prevention method is suitable for larger
database.
One method of avoiding deadlock is using application consistent
logic.
In the previous example, Transaction T1 simply waits for transaction
T2 to release the lock on Grades before it begins.
When transaction T2 releases the lock, Transaction T1 can proceed
freely.
Another method for avoiding deadlock is to apply both row level
locking mechanism and READ COMMITTED isolation level.
However, It does not guarantee to remove deadlocks completely.
Deadlock Detection
When a transaction waits indefinitely to obtain a
lock, The database management system should
detect whether the transaction is involved in a
deadlock or not.
Wait-for-graph is one of the methods for
detecting the deadlock situation.
This method is suitable for smaller database.
In this method a graph is drawn based on the
transaction and their lock on the resource.
If the graph created has a closed loop or a cycle,
then there is a deadlock.
Deadlock Detection
Deadlock prevention
For large database, deadlock prevention
method is suitable.
A deadlock can be prevented if the resources
are allocated in such a way that deadlock
never occur.
The DBMS analyzes the operations whether
they can create deadlock situation or not, If
they do, that transaction is never allowed to
be executed.
Deadlock prevention mechanism proposes two
schemes
1. Wait-Die Scheme
In this scheme, If a transaction request for a resource that
is locked by other transaction, then the DBMS simply
checks the timestamp of both transactions and allows the
older transaction to wait until the resource is available for
execution.
Deadlock prevention mechanism proposes two
schemes
Suppose, there are two transactions T1 and T2 .
actions: