Locking Protocols
Locking Protocols
UNIT IV - Transaction
Transaction Concepts – ACID Properties – Schedules – Serializability – Concurrency
Control – Need for Concurrency – Locking Protocols – Two Phase Locking – Deadlock –
Transaction Recovery – Save Points – Isolation Levels – SQL Facilities for Concurrency and
Recovery.
Locking Protocols
What is a Lock?
A lock is a variable associated with a data item that indicates whether it is currently in use
or available for other operations. Locks are essential for managing access to data during
concurrent transactions. When one transaction is accessing or modifying a data item, a
lock ensures that other transactions cannot interfere with it, maintaining data integrity
and preventing conflicts. This process, known as locking, is a widely used method to
ensure smooth and consistent operation in database systems.
Lock Based Protocols
Lock-Based Protocols in DBMS ensure that a transaction cannot read or write data until it
gets the necessary lock. Here’s how they work:
These protocols prevent concurrency issues by allowing only one transaction to
access a specific data item at a time.
Locks help multiple transactions work together smoothly by managing access to
the database items.
Locking is a common method used to maintain the serializability of transactions.
A transaction must acquire a read lock or write lock on a data item before
performing any read or write operations on it.
Types of Lock
1. Shared Lock (S): Shared Lock is also known as Read-only lock. As the name suggests
it can be shared between transactions because while holding this lock the
transaction does not have the permission to update data on the data item. S-lock is
requested using lock-S instruction.
S.No T1 T2
1 lock-X(B)
2 read(B)
3 B:=B-50
4 write(B)
5 lock-S(A)
6 read(A)
7 lock-S(B)
8 lock-X(A)
9 …… ……
1. Deadlock
In the given execution scenario, T1 holds an exclusive lock on B, while T2 holds a shared
lock on A. At Statement 7, T2 requests a lock on B, and at Statement 8, T1 requests a lock
on A. This situation creates a deadlock, as both transactions are waiting for resources held
by the other, preventing either from proceeding with their execution.
2. Starvation
Starvation is also possible if concurrency control manager is badly designed. For example:
A transaction may be waiting for an X-lock on an item, while a sequence of other
transactions request and are granted an S-lock on the same item. This may be avoided if
the concurrency control manager is properly designed.