44

I am a little unsure about read and write locks and just need someone to check if these facts about read/write locks are correct.

This is in reference to databases in general.

Read Locks:

  1. Multiple read locks can be acquired by multiple threads at the same time.
  2. When a thread has a read lock on a row/table, no thread can update/insert/delete data from that table. (Even if the thread trying to write data doesn't require a write lock.)
  3. A row/table cannot have a read and a write lock at the same time.

Write Locks:

  1. When a row/table has a write lock, it cannot be read by another thread if they have a read lock implemented in them but can be read by other threads if no read lock is implemented (i.e simple Select query)

Thanks for the clarification. I cant find direct assertions to these statements on the internets.

nknj
  • 2,436
  • 5
  • 31
  • 45

3 Answers3

26

In database management theory, locking is used to implement isolation among multiple database users txn. This is the "I" in the acronym ACID (Atomicity, Consistency, Isolation, Durability). Locks are applied by a TX (transaction) to data, which may block other TXs from accessing the same data during the TX's life.

Simple Locking: Two main types of locks can be requested:

  • Shared lock: Read lock i.e. Any other TX(or multiple TXs) can read but not write (lock shares with multiple txs i.e. Shared lock)
  • Exclusive lock: Write lock i.e. No other TX can read or write (lock not shares with any other txs i.e. exclusive lock)

Multiple Locking: Two Phase Locking (2PL) is a concurrency control method that guarantees serializability.

  • A Growing/Expanding/First Phase: locks are acquired and no locks are released.
  • A Shrinking/Contracting/Second Phase: locks are released and no locks are acquired.
Premraj
  • 72,055
  • 26
  • 237
  • 180
21

Read Locks:

  1. Multiple read locks can be acquired by multiple threads at the same time.

    True. Multiple read locks can exist at the same time. (Read lock has another name: Shared lock)

  2. When a thread has a read lock on a row/table, no thread can update/insert/delete data from that table. (Even if the thread trying to write data doesn't require a write lock.)

    True. The write transaction should wait for read locks to finish reading.

  3. A row/table cannot have a read and a write lock at the same time.

    True. If you have the write lock before the read lock, the write lock will block other transactions to read or write the same table. If you have the read lock before the write lock, the read lock will block the write transactions until the reading transaction finishes.

Write Locks:

  1. When a row/table has a write lock, it cannot be read by another thread if they have a read lock implemented in them but can be read by other threads if no read lock is implemented (i.e simple Select query)

    Sorry, I don't understand this statement. But when a table has a write lock (Exclusive Lock), it can not be read or written by another transaction.

Werner
  • 14,324
  • 7
  • 55
  • 77
Gen Wan
  • 1,979
  • 2
  • 12
  • 19
  • I think for the last statement, what @nknj means is that if the other transaction has a transaction isolation level set as READ_UNCOMMITED - in which case it need not take a shared lock and is allowed to read the row which is not yet committed. – Utkarsh Jun 05 '21 at 20:29
2

It depends on the isolation level used.

amit kumar
  • 20,438
  • 23
  • 90
  • 126
  • Does it mean that there in no specific meaning of locks, it's an abstraction in isolation levels? – Johnny_D Nov 22 '12 at 10:31
  • No, locks have a very specific meaning. Isolation is about the way locks are used. – amit kumar Nov 23 '12 at 07:17
  • Can you please share some link, or maybe article name about locks. Thanks. – Johnny_D Nov 23 '12 at 07:50
  • I'm not an expert in this, though have intermediate knowledge. But here is something about locks: http://en.wikipedia.org/wiki/Lock_(computer_science) and about transaction processing in databases where isolation is needed: http://en.wikipedia.org/wiki/Transaction_processing – amit kumar Nov 25 '12 at 09:26
  • 1
    Down voting since the answer could've been a lot more elaborate. – Niklas Ekman Oct 19 '16 at 17:23