Types of Two Phase Locking (Strict, Rigorous & Conservative) in DBMS


Two Phase Locking (2PL) is a fundamental technique used in database management systems to ensure the consistency and isolation of concurrent transactions. In this article, we will discuss the three categories of 2PL: strict 2PL, rigorous 2PL, and conservative 2PL, and explain how they differ in terms of their locking protocols. We will also provide code examples with explanations to illustrate how these categories can be implemented in practice.

Introduction to Two-Phase Locking

Before diving into the specific categories of 2PL, let's first review the basics of 2PL. Two Phase Locking is a technique used to control concurrent access to shared resources in a database management system. The basic idea behind 2PL is to ensure that a transaction can only acquire locks on resources after it has released all of its existing locks. This prevents deadlocks, which can occur when two or more transactions are waiting for each other to release a lock.

The two phases of 2PL are the growing phase and the shrinking phase. In the growing phase, a transaction acquires locks on resources as it needs them. In the shrinking phase, a transaction releases locks on resources that it no longer needs. The transition between the two phases is called the commit point, and it marks the point at which a transaction is considered to have completed its execution.

Strict Two Phase Locking (Strict 2PL)

Strict 2PL is the most restrictive form of 2PL. In strict 2PL, a transaction is not allowed to release any locks until it has reached the commit point. This means that a transaction will hold all of its locks until it has completed its execution and is ready to be committed.

One advantage of strict 2PL is that it guarantees serializability, which is the highest level of isolation among transactions. In other words, the results of concurrent transactions executed under strict 2PL will be the same as if they were executed one after the other.

The disadvantage of strict 2PL is that it can lead to decreased concurrency and increased contention for resources, as transactions are not able to release locks until they are committed.

# Strict 2PL example def strict_2pl(transaction_a, transaction_b): # Begin transaction A transaction_a.begin() # Transaction A acquires lock on resource X transaction_a.lock(resource_x) # Transaction A performs operation on resource X transaction_a.operate(resource_x) # Commit transaction A transaction_a.commit() # Begin transaction B transaction_b.begin() # Transaction B acquires lock on resource X transaction_b.lock(resource_x) # Transaction B performs operation on resource X transaction_b.operate(resource_x) # Commit transaction B transaction_b.commit()

As shown in the example above, in strict 2PL, transaction A must acquire and hold the lock on resource X until it has completed its execution and reached the commit point. Similarly, transaction B must also acquire and hold the lock on resource X until it has completed its execution and reached the commit point.

Rigorous Two Phase Locking (Rigorous 2PL)

Rigorous 2PL is similar to strict 2PL, but with a slight relaxation of the locking protocol. In rigorous 2PL, a transaction is allowed to release a lock if it is certain that it will not need the lock again. For example, if a transaction is reading a resource and it knows that it will not need to write to the resource, it can release the lock after reading.

The advantage of rigorous 2PL is that it allows for increased concurrency, as transactions are able to release locks that they no longer need. This can lead to less contention for resources and improved performance.

The disadvantage of rigorous 2PL is that it can be more difficult to implement, as the system must be able to determine when a transaction can safely release a lock. Additionally, rigorous 2PL does not guarantee serializability, as the released locks may be acquired by other transactions in an order that would not have been possible in a serial execution.

# Rigorous 2PL example def rigorous_2pl(transaction_a, transaction_b): # Begin transaction A transaction_a.begin() # Transaction A acquires lock on resource X for reading transaction_a.lock_shared(resource_x) # Transaction A reads resource X data = transaction_a.read(resource_x) # Transaction A releases lock on resource X transaction_a.unlock(resource_x) # Begin transaction B transaction_b.begin() # Transaction B acquires lock on resource X for writing transaction_b.lock(resource_x) # Transaction B updates resource X with new data transaction_b.write(resource_x, data) # Commit transaction B transaction_b.commit()

As shown in the example above, in rigorous 2PL, transaction A acquires a shared lock on resource X for reading. After it has read the resource, it releases the lock, as it no longer needs it. Meanwhile, transaction B acquires an exclusive lock on resource X for writing. Since transaction A has already released its lock, transaction B is able to acquire the lock and update the resource without any contention.

Conservative Two Phase Locking (Conservative 2PL)

Conservative 2PL is a less restrictive form of 2PL than strict 2PL and rigorous 2PL. In conservative 2PL, a transaction is allowed to release any lock at any time, regardless of whether it will need the lock again.

The advantage of conservative 2PL is that it allows for maximum concurrency, as transactions are able to release locks at any time. This can lead to the best performance in terms of throughput and response time.

The disadvantage of conservative 2PL is that it does not guarantee serializability and can lead to inconsistent results if not implemented carefully. Additionally, it does not prevent deadlocks which could cause transaction to hang.

# Conservative 2PL example def conservative_2pl(transaction_a, transaction_b): # Begin transaction A transaction_a.begin() # Transaction A acquires lock on resource X transaction_a.lock(resource_x) # Transaction A performs operation on resource X transaction_a.operate(resource_x) # Transaction A releases lock on resource X transaction_a.unlock(resource_x) # Begin transaction B transaction_b.begin() # Transaction B acquires lock on resource X transaction_b.lock(resource_x) # Transaction B performs operation on resource X transaction_b.operate(resource_x) # Commit transaction B transaction_b.commit()

As shown in the example above, in conservative 2PL, transaction A acquires and releases the lock on resource X at any time. Similarly, transaction B also acquires and releases the lock on resource X at any time. This allows for maximum concurrency, as both transactions can operate on the resource simultaneously without waiting for the other to release its lock.

It's important to note that while conservative 2PL allows for maximum concurrency, it does not guarantee consistency and isolation among transactions. Therefore, it is important to have a good understanding of the system's behavior when implementing conservative 2PL, and to take measures to ensure that the system remains in a consistent state.

To sum up, Two Phase Locking (2PL) is a fundamental technique used in database management systems to ensure the consistency and isolation of concurrent transactions. The three categories of 2PL: strict 2PL, rigorous 2PL, and conservative 2PL, differ in terms of their locking protocols and can have different trade-offs in terms of concurrency and consistency. By understanding the properties of each category, it is possible to choose the most appropriate 2PL strategy for a given application.

Conclusion

  • Strict 2PL is the most restrictive form of 2PL and guarantees serializability, but may lead to decreased concurrency and increased contention for resources.

  • Rigorous 2PL is similar to strict 2PL but allows for increased concurrency, but does not guarantee serializability and can be more difficult to implement.

  • Conservative 2PL is a less restrictive form of 2PL that allows for maximum concurrency but does not guarantee consistency and isolation among transactions, and increases deadlock chances.

It's important to consider the requirements of the system before deciding which type of 2PL to use. It is also important to ensure that the system remains in a consistent state and to take measures to prevent deadlocks while using Conservative 2PL.

Updated on: 16-Jan-2023

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements