Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read Locks and Write Locks

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.

like image 275
nknj Avatar asked Oct 10 '11 12:10

nknj


People also ask

What are read and write locks?

In many situations, data is read more often than it is modified or written. In these cases, you can allow threads to read concurrently while holding the lock and allow only one thread to hold the lock when data is modified. A multiple-reader single-writer lock (or read/write lock) does this.

What are read locks?

It is used to prevent concurrent updates. A read lock is a shared lock, multiple readers can held a read lock simultaneously. A read lock conflicts when some other threads takes an update lock.

How does read/write lock work?

ReadWriteLock is an advanced thread lock mechanism. It allows multiple threads to read a certain resource, but only one to write it, at a time. The idea is, that multiple threads can read from a shared resource without causing concurrency errors.

What is read and write lock in SQL?

It means that an exclusive lock can hold only one transaction on a resource at the same time. The user of this lock is known as a writer. This lock is imposed when the transaction wants to modify the page or row data.

Which lock supports both read & write operation on lock?

Exclusive Lock (X) : With the Exclusive Lock, a data item can be read as well as written. Also called write lock.

Which locks are referred as read locks?

Non-exclusive locks are granted for read-only access. For this reason, non-exclusive locks are also sometimes called read locks. Since multiple lockers can simultaneously hold read locks on the same object, read locks are also sometimes called shared locks.


3 Answers

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.
like image 107
Premraj Avatar answered Sep 20 '22 04:09

Premraj


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.

like image 37
Gen Wan Avatar answered Sep 17 '22 04:09

Gen Wan


It depends on the isolation level used.

like image 23
amit kumar Avatar answered Sep 17 '22 04:09

amit kumar