Last modified: May 16, 2025

This article is written in: 🇺🇸

Shared and Exclusive Locks

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:

  1. What are shared and exclusive locks in database systems, and how do they differ in terms of access permissions for transactions?
  2. How do shared and exclusive locks interact with each other, and what does the lock compatibility matrix illustrate about their behavior?
  3. Can you provide examples of scenarios where shared locks are appropriate and where exclusive locks are necessary to maintain data integrity?
  4. What best practices can be followed to balance concurrency and data integrity when using shared and exclusive locks in transactions?
  5. How do deadlocks occur in the context of shared and exclusive locks, and what strategies can be implemented to prevent and resolve them?

Understanding Shared Locks

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.

Exploring Exclusive Locks

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.

Interaction Between Shared and Exclusive Locks

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

Practical Examples with Commands

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.

Shared vs. Exclusive Locks: Applicability

Locking behavior may vary slightly by isolation level and vendor syntax; the following examples assume the default READ COMMITTED isolation level.

Example: Reading Data (Shared Lock)

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;

Example: Updating Data (Exclusive Lock)

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;

Lock Interaction Timeline

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 on UPDATE.

Considerations and Variations

Balancing Concurrency and Integrity

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:

Best Practices for Using Locks

To optimize database performance while ensuring data integrity, the following practices are recommended:

Deadlocks and How to Handle Them

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:

Table of Contents

    Shared and Exclusive Locks
    1. Understanding Shared Locks
    2. Exploring Exclusive Locks
    3. Interaction Between Shared and Exclusive Locks
    4. Practical Examples with Commands
      1. Shared vs. Exclusive Locks: Applicability
      2. Example: Reading Data (Shared Lock)
      3. Example: Updating Data (Exclusive Lock)
      4. Lock Interaction Timeline
      5. Considerations and Variations
    5. Balancing Concurrency and Integrity
    6. Best Practices for Using Locks
    7. Deadlocks and How to Handle Them