Lock Based Protocols | Database Management System

Lock Based Protocols | Database Management System

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 concurrency-control manager, Transaction can proceed only after request is granted.


>> 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. Example of a transaction performing locking:

T2: lock-S(A),
read (B),
unlock (B),
display (A+B)

Locking as above is not sufficient to guarantee serializability – if A and B get updated in-between the read of A and B, the displayed sum would be wrong.

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.


Neither T, nor T. can make progress – executing lock-S(B) causes T, to wait for T, to release its lock on B, while executing lock-X(A) causes T, to wait for T, to release its lock on A.

Such a situation is called a deadlock.

To handle a deadlock one of 15 or T, must be rolled back and its locks released.

The potential for deadlock exists in most locking protocols.

Deadlocks are a necessary evil.

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.

>> The same transaction is repeatedly rolled back due to deadlocks.

>> Concurrency control manager can be designed to prevent Starvation.

Two-Phase Locking Protocol

>> This is a protocol which 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 protocol assures serializability. It can be proved that the transactions can be serialized in the order of their lock points (i.e., the point where a transaction acquired its final lock).

>> Two-phase locking does not ensure freedom from deadlocks

>> Cascading roll-back is possible under two-phase locking. To avoid this, follow a modified protocol called strict two-phase locking. Here a transaction must hold all its exclusive locks till it commits/aborts.

>> Rigorous two-phase locking is even stricter: here ail locks are held till commit/abort. In this protocol transactions can be serialized in the order in which they commit.

There can be conflict serializable schedules that cannot be obtained if two-phase locking is used. However, in the absence of extra information (e.g., ordering of access to data), two-phase locking is needed for conflict serializability in the following sense:

Given a transaction Ti that does not follow two-phase locking, we can find a transaction Tj that uses two-phase locking, and a schedule for Ti and Tj that is not conflict serializable.

>> Can allow 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. But still relies on the programmer to insert the various locking instructions.

Automatic Acquisition of Locks

A simple automated algorithm can place lock requests for a transaction Ti issuing the standard read/write instructions:

The operation read (D) is processed as:

>> if Ti has a lock on D then read (D) otherwise

>> request a lock-S on D (may be necessary to wait for a lock-X)

>> when lock-S request is granted, then read (D)

The operation write (D) is processed as:

>> if Ti has a lock-X on D then write (D) otherwise

>> if Ti has a lock-S on D then upgrade lock on D to lock-X may have to wait for upgrade

>> otherwise request a new lock-X

>> finally write(D) when receive upgrade or new lock

All locks are released after Commit or abort.

Example on Auto Lock insertion


A transaction Ti requesting a lock-S on D is given as : Sli (D).

A transaction Ti requesting a lock-X on D is given as : xli (D).

A transaction Ti unlocking a data item D is given as : uli(D).


Given the transaction, insert lock operations according to 2PL:
T1: r1(A), r1(C), W1(B), W1(C),

Basic 2PL:

S1 (A), r1(A); x11(C), r1(C), X11 (B), W1(B), W1(C); C1; Lu||1(A); ul1(B), ul1(C);

Conservative 2PL:

atomics (sl/1(A), x/1(C), x1(B)

r1(A), r1(C), W1(B), W1(C), c1, ull(A), ul1(B), Lil 1(C),

Strict. 2PL:

S11 (A), r1 (A); x11 (C), r1(C); x11(B); ul:1 (A), W1(B); w 1(C); C1, ul1(B), ul 1(C);

Rigorous 2PL:

s/1(A); r1(A), XI1(C); r1(C): X11(B); w1(B); w1(C): C1; ul1(A), սl1(B), սl1(C):