Open In App

Predicate Locking

A lock, in DBMS, is a variable that is associated with a data item. Locks in DBMS help synchronize access to the database items by concurrent transactions.

Lock Based Protocol in DBMS is used to eliminate concurrency problems for simultaneous transactions, (this refers to a situation wherein one transaction results in changing the results of another transaction’s search query.  The lock-based protocol comes into the picture by locking or isolating a particular transaction to a single user. Locking helps the system to appear as though all transactions appeared sequentially.



Flight seat booking

While booking a flight seat, if two users are booking the same seat at the same time there are going to be many types of conflict between the two transactions. Therefore, we need to implement a system wherein if one user has searched for existing bookings for a seat within a certain time window, another user is not allowed to concurrently insert or update another booking for the same seat and time range. (concurrently he/she can insert bookings for other seats, or for the same seat at a different time that doesn’t affect the ongoing booking.)

Implementation of Predicate Locking

Here comes into the picture predicate locking. It works similarly to the shared/exclusive lock, but rather than belonging to a particular object (e.g., one row in a table), it is common to all objects that satisfy some particular search condition(s), such as:



SELECT * FROM bookings WHERE seat_id = 543 AND

end_time > ‘2022-09-24 17:00’ AND start_time < ‘2022-09-24 18:00’;

A predicate lock works by restricting access to predicate rather than access to records, the access restrictions are as follows:

The idea of predicate lock is to also take into account those objects that might be added in the probable future, such objects are known as phantoms. In case of any matching between the predicate locks, the database will discard or stop any sort of write skew and race conditions.

Advantages of predicate locking:

Disadvantages of predicate locking:

Article Tags :