Last modified: May 16, 2025
This article is written in: 🇺🇸
Shared and exclusive locks are used in database systems for managing concurrent access to data. They ensure that transactions occur without conflicting with each other, maintaining the integrity and consistency of the database.
[Resource: Data Item X]
|
|-- Transaction A wants to READ --> Acquires SHARED LOCK
|-- Transaction B wants to READ --> Acquires SHARED LOCK
|
[Both can read simultaneously]
[Resource: Data Item Y]
|
|-- Transaction C wants to WRITE --> Acquires EXCLUSIVE LOCK
|
[No other transaction can read or write until the lock is released]
In the diagram above, Transactions A and B both acquire shared locks on Data Item X, allowing them to read the data at the same time without interference. Transaction C, however, obtains an exclusive lock on Data Item Y to perform a write operation, preventing other transactions from accessing it until the operation is complete.
After reading the material, you should be able to answer the following questions:
Shared locks allow multiple transactions to read the same data concurrently. They are vital for operations where data needs to be read without being modified, ensuring that the data remains consistent for all reading transactions.
Imagine a library database where several users are looking up the same book information. Each user's transaction places a shared lock on the book's data, allowing everyone to read the information simultaneously without any conflicts.
Exclusive locks grant a single transaction the sole right to read and modify a piece of data. This lock type is necessary when a transaction needs to ensure that no other transactions can interfere with its operation, such as when updating or deleting data.
Consider an online banking system where a user is transferring money from one account to another. The transaction places an exclusive lock on both account records to prevent other transactions from reading or modifying the balances until the transfer is complete, ensuring the accuracy of the transaction.
Understanding how shared and exclusive locks interact is essential for managing database concurrency effectively.
Lock Held \ Requested | Shared Lock Requested | Exclusive Lock Requested |
Shared Lock Held | Allowed | Not Allowed |
Exclusive Lock Held | Not Allowed | Not Allowed |
These examples illustrate row-level locking behavior common to most modern relational databases—PostgreSQL, MySQL/InnoDB, MariaDB, SQL Server, and Oracle—which support shared (S) and exclusive (X) locks at the row level. They do not apply to engines or table types without row-level locking (e.g., MySQL’s MyISAM), nor to NoSQL stores that use different concurrency controls.
Locking behavior may vary slightly by isolation level and vendor syntax; the following examples assume the default READ COMMITTED isolation level.
In databases with row-level locking, a shared lock (S) permits multiple transactions to read the same rows concurrently but prevents any transaction from modifying them until all shared locks are released.
-- Applies in PostgreSQL, MySQL/InnoDB, SQL Server, Oracle
BEGIN TRANSACTION;
SELECT * FROM Employees WHERE Department = 'Sales';
-- Shared (S) lock on matching rows until COMMIT
COMMIT;
An exclusive lock (X) is required for row modifications. If another transaction holds a shared or exclusive lock on the same row, the update waits (or may deadlock under certain patterns).
-- Applies in PostgreSQL, MySQL/InnoDB, SQL Server, Oracle
BEGIN TRANSACTION;
UPDATE Employees SET Salary = Salary * 1.05 WHERE Department = 'Sales';
-- Request X lock: waits until no other S or X locks exist on those rows
COMMIT;
Step | Transaction | Action | Lock Held | Outcome |
1 | T1 (Reader) | SELECT ... FOR SHARE (implicit) |
S on Sales rows | Allows other S locks; blocks X locks |
2 | T2 (Updater) | UPDATE ... |
Requests X on Sales rows | Waits until T1 commits and releases its S lock |
3 | T1 | COMMIT |
Releases S | T2 acquires X lock, performs update, then COMMIT |
Note: Some databases (e.g., Oracle) require explicit
SELECT ... FOR UPDATE
to acquire row locks for reads; others implicitly lock onUPDATE
.
Efficient database systems strive to balance the need for high concurrency with the necessity of maintaining data integrity. Locks play a pivotal role in achieving this balance. Here are the key concepts:
To optimize database performance while ensuring data integrity, the following practices are recommended:
Deadlocks occur when two or more transactions are waiting indefinitely for each other to release locks.
Deadlock Scenario:
Transaction 1:
LOCK Resource A
WAIT for Resource B
Transaction 2:
LOCK Resource B
WAIT for Resource A
In this situation, Transaction 1 holds a lock on Resource A and waits for Resource B, while Transaction 2 holds a lock on Resource B and waits for Resource A. Neither can proceed, resulting in a deadlock.
Strategies to Prevent Deadlocks: