Shared and Exclusive Locks or Read/Write Locks

Need of Shared/Read and Exclusive/Write Lock

The binary lock is too restrictive for data items because at most one transaction can hold on a given item whether the transaction is reading or writing. To improve it we have shared and exclusive locks in which more than one transaction can access the same item for reading purposes.i.e. the read operations on the same item by different transactions are not conflicting.

What are Shared and Exclusive Locks

In this types of lock, system supports two kinds of lock :

  • Exclusive(or Write) Locks and
  • Shared(or Read) Locks.

Shared Locks

If a transaction Ti has locked the data item A in shared mode, then a request from another transaction Tj on A for :

  • Write operation on A : Denied. Transaction Tj has to wait until transaction Ti unlock A.
  • Read operation on A : Allowed.
Example :
  T 
S(A)
R(A)     
W(A)     ×

Exclusive Locks

If a transaction Ti has locked a data item a in exclusive mode then request from some another transaction Tj for-

  • Read operation on A : Denied
  • Write operation on A : Denied

COMPATIBLE Table for Shared and Exclusive Locks :

Compatiible table for Shared and Exclusive Lock

What does the Compatibility Table means ?
  • If a transaction has lock a data item in shared mode, then another transaction can lock the same data item in shared mode.
  • If a transaction has lock a data item in shared mode, then another transaction cannot lock the same data item in exclusive mode.
  • If a transaction has lock a data item in exclusive mode, then another transaction cannot lock the same data item in shared mode as well as exclusive mode.

Operations Used with Shared and Exclusive Locks

  1. Read_lock(A) or s(A)
  2. Write_lock(A) or X(A)
  3. Unlock(X) or U(A)

Implementation of Shared and Exclusive Locks

Shared and exclusive locks are implemented using 4 fields :

  1. Data_item_name
  2. LOCK
  3. Number of Records and
  4. Locking_transaction(s)

Again to save space, items that are not in the lock table are considered to be unlocked. The system maintains only those records for the items that are currently locked in the lock table.

Value of LOCK(A) : Read Locked or Write Locked
  • If LOCK(A) = write-locked – The value of locking transaction is a single transaction that holds the exclusive(write) Lock on A.
  • If LOCK(A) = read-locked – The value of locking transaction is a list of one or more transactions that hold the Shared(read) on A.

Transaction Rules for Shared and Exclusive Locks

Every transaction must obey the following rules :

  1. A transaction T must issue the operation s(A) or read_lock(A) or x(A) or write_lock(A) before any read(A) operation is performed in T.
  2.  A transaction T must issue the operation x(A) or write_lock(A) before any write(A) operation is performed in T.
  3. After completion of all read(A) and write(A) operations in T, a transaction T must issue an unlock(A) operation.
  4. If a transaction already holds a read (shared) lock or a write (exclusive) lock on item A, then T will not issue an unlock(A) operation.
  5. A transaction that already holds a lock on item A, is allowed to convert the lock from one locked state to another under certain conditions.
    • Upgrading the Lock by Issuing a write_lock(A) Operation or Conversion of read_lock() to write_lock() :
      • Case 1 – When Conversion Not Possible : A transaction T will not issue a write_lock(A) operation if it already holds a read (shared) lock or write (exclusive) lock on item A.
      • Case 2 – When Conversion Possible : If T is the only transaction holding a read lock on A at the time it issues the
        write_lock(A) operation, the lock can be upgraded;
    • Downgrading the Lock by Issuing a read_lock(A) or Conversion of write_lock() to read_lock() :
      A transaction T downgrade from the write lock to a read lock by acquiring the write_lock(A) or x(A), then the read_lock(A) or s(A) and then releasing the write_lock(A) or x(A).

Points About Shared and Exclusive Locking :

  • Shared and Exclusive Lock results more concurrency than Binary Locking.
  • Shared and Exclusive Lock may not ensure serializability i.e. Non Serializable Schedule is possible to execute using shared and exclusive locking.

Previous Home Next
Binary Locks 2 Phase Locking

     

Incoming search terms:

  • shared lock and exclusive lock in dbms
  • locking in dbms
  • Locks in dbms
  • difference between shared and exclusive lock in dbms
  • what are types of locks in dbms
  • exclusive lock def
  • what is the difference between shared and exclusive lock in a transaction
  • differentiate read and write locks in tabular form
  • difference between shared lock and exclusive lock
  • define lock share location lock exclusive lock in dbms

Leave a Reply