Open In App

Levels of Locking in DBMS

The locking in a database can be done at 4 levels, which start with the database at the highest level and down via table and page to the row at the lowest level.

Before we discuss the levels of locking, we should know about the types of locks or lock mode. So, there are 5 locks types in the locking and these are discussed below :



Drawbacks of locking:

  1. May not free from recoverability.
  2. Not free from deadlock.
  3. Not free from starvation.
  4. Not free from cascading rollback.

Level of Hierarchy in Locking

Now, let’s discuss the levels of locking one by one.



1. Database Level :
At the database level, the full or complete database is locked. Now, If let’s say there are two relations in a database. If  say, R1 and R2, where R1 uses tables, then R2 cannot use them. You will always find a shared lock on this level that is used whenever a transaction is connected to a database. On this level, we use shared locking to prevent dropping of the database or restoring a database backup over the database in use. Let’s see an example. When we use a SELECT statement to read some data, a shared lock will be applied on the database level, an intent shared lock will be applied on the table and on the page level, and a shared lock on the row itself.

Applications of Database level –

2. Table Level : 
At the table level, the full table or relation is locked. Now, If let’s say there are two relations in a database say, R1 and R2, where R1 uses tables, then R2 cannot use it. But, two transactions can access the same database only if they are accessing different relations. A transaction using a level of the table will hold shared and/or exclusive table locks. At the table level, there are 5 different types of locks. i.e, Exclusive (X), Shared (S), Intent exclusive (IX), Intent shared (IS), and Shared with intent exclusive (SIX) and these locks have already been discussed above.

Applications of Table level –

3. Page Level :
The Page-level always consists of fixed size i.e, power of 2 or 2i type. A table can span several pages and a page can contain several tuples of one or more relations. At the page level, an intent shared lock (IS) will be imposed. This lock is capable of locking a table, shared, or exclusive page. An intent exclusive lock (IX) or intent update lock (IU) will be imposed if there’s a case of DML statements (i.e. insert, update, delete).

Applications of Page level –

4. Row Level : 
This level of locking is less restrictive as compared to other levels of locking.  At the row level, if a concurrent transaction is accessing different rows in the same relationship, even if the rows are located on the same page, then this can be accepted by database systems. Here at this level, DBMS allows concurrent transactions to access the rows of the same relation even in the case of where the rows are on the same page. At this level, we can apply 3 different types of locks, i.e, Exclusive, Shared, Update and these locks have already been discussed above and also, particular rows are locked in a query on this level.

Applications of Row level –

Article Tags :