DBMS_UNIT-5_protocols-1[1]
DBMS_UNIT-5_protocols-1[1]
• concurrency-control
• Lock-Based Protocols
• The Two-Phase Locking Protocol
CONCURRENCY CONTROL
• When several transactions execute concurrently in the
database, the isolation property may no longer be preserved.
• To handle this, the system must control the interaction among
the concurrent transactions.
• This control is achieved through concurrency-control schemes.
The concurrency-control schemes are all based on the
serializability property. All the schemes ensure that the
schedules are serializable.
Lock-Based Protocols
• A lock is a mechanism to control concurrent access to a data item
• Data items can be locked in two modes :
1. exclusive (X) mode. Data item can be both read as well as
written. X-lock is requested using lock-X instruction.
2. shared (S) mode. Data item can only be read. S-lock is
requested using lock-S instruction.
• Lock requests are made to the concurrency-control manager by the
programmer.
• Transaction can proceed only after request is granted.
Lock-Based Protocols (Cont.)
• A transaction may be granted a lock on an item if the requested lock is
compatible with locks already held on the item by other transactions
• Any number of transactions can hold shared locks on an item, But if
any transaction holds an exclusive on the item no other transaction
may hold any lock on the item.
• If a lock cannot be granted, the requesting transaction is made to wait
till all incompatible locks held by other transactions have been
released. The lock is then granted.
• Lock-compatibility matrix
Lock-Based Protocols (Cont.)
• Example of a transaction performing locking:
T2: lock-S(A);
read (A);
unlock(A);
lock-S(B);
read (B);
unlock(B);
display(A+B)
• Locking as above is not sufficient to guarantee serializability — if A
and B get updated in-between by any other transaction in schedule,
the read of A and B, the displayed sum would be wrong.
The Two-Phase Locking
Protocol
• A locking protocol is a set of rules followed by all transactions
while requesting and releasing locks. Locking protocols restrict the
set of possible schedules.
• This protocol ensures conflict-serializable schedules.
• Phase 1: Growing Phase
• Transaction may obtain locks
• Transaction may not release locks
• Phase 2: Shrinking Phase
• Transaction may release locks
• Transaction may not obtain locks
The Two-Phase Locking
Protocol (Cont.)
• 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.
• The point in the schedule where the transaction has obtained its final
lock is called the lock point of the transaction. Transactions can be
ordered according to their lock points.
• this ordering is a serializability ordering for the transactions.
The Two-Phase Locking
Protocol (Cont.)
• Another variant of two-phase locking is the rigorous two-phase
locking protocol, which requires that all locks be held until the
transaction commits.
• We can easily verify that, with rigorous two-phase locking,
transactions can be serialized in the order in which they commit.
• Most database systems implement either strict or rigorous two-
phase locking.
The Two-Phase Locking
Protocol (Cont.)
• 2PL does not ensure freedom from deadlock. Cascading rollback may
occur which can be avoided by a modification of two-phase locking
called the strict two-phase locking protocol.
• Strict two-phase locking protocol: This protocol requires not only
that locking be two phase, but also that all exclusive-mode locks
taken by a transaction be held until that transaction commits.
• It ensures that any data written by an uncommitted transaction are
locked in exclusive mode until the transaction commits, preventing
any other transaction from reading the data.
Lock Conversions
• a refinement of the basic two-phase locking protocol, in which
lock conversions are allowed.
• Upgrade: Conversion from shared to exclusive modes.
• Downgrade: Conversion from exclusive to shared.
• Upgrading can take place in only the growing phase, whereas
downgrading can take place in only the shrinking phase.
Lock Conversions
• Two-phase locking with lock conversions:
– First Phase:
• can acquire a lock-S on item
• can acquire a lock-X on item
• can convert a lock-S to a lock-X (upgrade)
– Second Phase:
• can release a lock-S
• can release a lock-X
• can convert a lock-X to a lock-S (downgrade)
• This protocol assures serializability.
Content
T1 Tx W-timestamp(Q) =10 T1 Tx
TS(T1 ) =5
write(X) write(X)
R- write(X)
i.e. TS(Tx ) =10 read(X)
timestamp(Q)=
10 write(X) write(X)
Advantage
s