Skip to the content.

Row-level locking is a type of database locking mechanism where individual rows in a table are locked during a transaction, rather than the entire table.

It’s used to allow high concurrency by letting multiple transactions work on different rows of the same table simultaneously without conflicts.


πŸ”‘ Key Points

  1. Granularity:

    • Lock is applied per row.
    • Finer-grained than table-level locks (which lock the whole table).
  2. Purpose:

    • Prevent lost updates and data inconsistencies when multiple transactions modify the same table.
  3. Behavior:

    • Other transactions can read or update different rows in the same table.
    • Transactions trying to update the same row will wait until the lock is released.
  4. Types of Row Locks:

    • Exclusive Lock (X Lock): Only one transaction can modify the row.
    • Shared Lock (S Lock): Multiple transactions can read but cannot write.

πŸ–₯ Example (SQL)

-- Transaction A locks row with id=1
SELECT * FROM accounts WHERE id = 1 FOR UPDATE;

-- Transaction B tries to update the same row
UPDATE accounts SET balance = balance + 100 WHERE id = 1;
-- B will wait until A commits

-- Transaction C updates row with id=2
UPDATE accounts SET balance = balance + 50 WHERE id = 2;
-- C executes immediately (different row)

βœ… Advantages

⚠️ Disadvantages


πŸ’‘ Analogy: