Difference between Shared Lock and Exclusive Lock



There are situations in a database in which if one transaction has to access data, other transactions should not be allowed to access the same data. In such a case, the lock is important to prevent other transactions from using the data that is being used by a transaction. Locks are of two types which include shared locks and exclusive locks. In this article, we will see the difference between shared lock and exclusive lock.

What is a Shared Lock?

Shared lock is a lock that does not permit to work on data by a transaction if it is being updated by another transaction. All other transactions are allowed only to read the data. Other transactions can work on the data only after the completion of the task and removal of the shared lock. Read integrity is supported by shared lock which helps other transactions to read the data.

What is Exclusive Lock?

Exclusive lock is a lock which prevents access to data by other transactions till the time the current transaction holding the exclusive lock is rolled back or committed. Only one transaction can have exclusive lock at a time. A data item can be read or written if there is an exclusive lock.

Difference between Shared Lock and Exclusive Lock

Here is the table which shows the difference between shared lock and exclusive lock.

Shared Lock Exclusive Lock
Shared lock allows read-only permission Exclusive lock allows read as well as write permissions.
Shared lock can be provided for those transactions that do not have exclusive lock. Exclusive lock can be placed on those transactions that do not have any kind of lock.
Other transactions cannot update data if a transaction having shared lock is working on it but it allows to read the data. Exclusive lock neither allows reading nor updating the data by other transactions.
Shared lock is used when a transaction does not have an exclusive lock. Exclusive lock is used when unlocked data has to be updated.
A large number of transactions are allowed to hold the shared lock. Only one transaction is allowed to hold an exclusive lock. If a transaction is holding an exclusive lock, other transactions have to wait till the transaction holding the lock is rolled back or committed.
Shared lock can be issued with the help of shared lock instructions. Exclusive lock can be requested with the help of exclusive lock instructions.

Conclusion

Shared locks and exclusive locks are used in databases. Shared locks are used when a transaction is updating data and other transactions get the read-only view. Shared lock can be placed on any number of transactions. Exclusive lock can be placed only on a single transaction. When that transaction is committed or rolled back then only other transactions can have the exclusive lock. Exclusive lock can be placed only on one transaction at a time.

FAQs on Shared Lock Vs. Exclusive Lock

1. Which lock can be placed on multiple transactions?

Shared lock can be placed on multiple transactions at a time. Exclusive lock can be placed only on a single transaction.

2. What types of instructions are used to place the locks?

Shared lock instruction is used to place the shared lock and exclusive lock instruction is used to place the exclusive lock.

3. On which transactions, exclusive locks can be placed?

Exclusive lock can be placed on those transactions that do not have any kind of lock. An exclusive lock can be placed only on one transaction at a time.

4. Which lock allows rad-only permission?

Read-only permission is given by shared lock as it can be placed on any number of transactions. The transactions having shared lock cannot be updated till the lock is removed. Exclusive lock can be placed only on one transaction.

5. On how many transactions can exclusive lock be placed at a time?

Exclusive lock can be placed only on one transaction at a time. If another transaction wants the exclusive lock, it has to wait to till the transaction on which the exclusive lock is placed is committed or rolled back.
Updated on: 2024-07-31T14:05:37+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements