Difference between Shared Lock and Exclusive Lock (original) (raw)

Last Updated : 4 Dec, 2025

Ensuring Data Integrity and Isolation is essential when numerous transactions are accessing a database at once to avoid conflicts and preserve correctness. Locks are a typical way to manage access to data elements in a transaction, thereby guaranteeing the isolation characteristic. Locks are systems that stop many transactions from making changes to the same data at once, protecting the data's integrity. Shared Lock (S) and Exclusive Lock (X) are the two primary kinds of locks. The distinctions between shared and exclusive locks, as well as their uses, will all be covered in this article.

A data item is locked for reading using a Shared Lock (S), sometimes referred to as a read lock. Other transactions can get a shared lock on a data item that a transaction is holding, but they are not able to obtain an exclusive lock to alter it.

Key Characteristics of Shared Lock

Example of Shared Lock

Take into consideration two transactions that want to read A = 100, the same data item. Inconsistencies may arise if one transaction tries to change A while the other is still reading it. A shared lock, however, stops updates until every transaction has completed reading.

Advantages of Shared Lock

Disadvantages of Shared Lock

Locks are essential in maintaining consistency in databases. Shared and exclusive locks play key roles in concurrent transactions.

**What is an Exclusive Lock?

A data item is locked for writing using an Exclusive Lock (X), sometimes referred to as a write lock. No other transaction is able to access or alter a data item that it has an exclusive lock on until the lock is released.

Key Characteristics of Exclusive Lock

Example of Exclusive Lock

An example of an exclusive lock is when a transaction obtains an exclusive lock (X-lock) in order to update a value, such as A = 100, by subtracting 50. As a result, until the update is finished and the lock is released, no other transaction is allowed to read from or write to A.

Compatibility matrix for locks

Compatibility matrix for locks

Advantages of Exclusive Lock

Disadvantages of Exclusive Lock

Shared Lock Exclusive Lock
Lock mode is read only operation. Lock mode is read as well as write operation.
Shared lock can be placed on objects that do not have an exclusive lock already placed on them. Exclusive lock can only be placed on objects that do not have any other kind of lock.
Prevents others from updating the data. Prevents others from reading or updating the data.
Issued when transaction wants to read item that do not have an exclusive lock. Issued when transaction wants to update unlocked item.
Any number of transaction can hold shared lock on an item. Exclusive lock can be hold by only one transaction.
S-lock is requested using lock-S instruction. X-lock is requested using lock-X instruction.
**Example: Multiple transactions reading the same data **Example: Transaction updating a table row