Concurrency Control Protocol
Concurrency Control Protocol
Concurrency Control Protocol
In this type of protocol, any transaction cannot read or write data until it acquires an
appropriate lock on it. There are two types of lock:
1. Shared lock:
o It is also known as a Read-only lock. In a shared lock, the data item can only read
by the transaction.
o It can be shared between the transactions because when the transaction holds a
lock, then it can't update the data on the data item.
2. Exclusive lock:
o In the exclusive lock, the data item can be both reads as well as written by the
transaction.
o This lock is exclusive, and in this lock, multiple transactions do not modify the
same data simultaneously.
o Pre-claiming Lock Protocols evaluate the transaction to list all the data items on
which they need locks.
o Before initiating an execution of the transaction, it requests DBMS for all the lock
on all those data items.
o If all the locks are granted then this protocol allows the transaction to begin.
When the transaction is completed then it releases all the lock.
o If all the locks are not granted then this protocol allows the transaction to rolls
back and waits until all the locks are granted.
o The two-phase locking protocol divides the execution phase of the transaction
into three parts.
o In the first part, when the execution of the transaction starts, it seeks permission
for the lock it requires.
o In the second part, the transaction acquires all the locks. The third phase is
started as soon as the transaction releases its first lock.
o In the third phase, the transaction cannot demand any new locks. It only releases
the acquired locks.
There are two phases of 2PL:
Growing phase: In the growing phase, a new lock on the data item may be acquired by
the transaction, but none can be released.
Shrinking phase: In the shrinking phase, existing lock held by the transaction may be
released, but no new locks can be acquired.
In the below example, if lock conversion is allowed then the following phase can
happen:
Example:
The following way shows how unlocking and locking work with 2-PL.
Transaction T1:
Transaction T2:
o The first phase of Strict-2PL is similar to 2PL. In the first phase, after acquiring all
the locks, the transaction continues to execute normally.
o The only difference between 2PL and strict 2PL is that Strict-2PL does not release
a lock after using it.
o Strict-2PL waits until the whole transaction to commit, and then it releases all the
locks at a time.
o Strict-2PL protocol does not have shrinking phase of lock release.
o
2. Rigorous two-phase locking protocol :
o The transaction cannot release either of the locks, i.e., neither shared lock
nor exclusive lock.
o Serializability is guaranteed in a Rigorous two-phase locking protocol.
o Deadlock is not guaranteed in the rigorous two-phase locking protocol.
3. Conservative two-phase locking protocol :
o The transaction must lock all the data items it requires in the transaction
before the transaction begins.
o If any of the data items are not available for locking before execution of
the lock, then no data items are locked.
o The read-and-write data items need to be known before the transaction
begins. This is not possible normally.
o Conservative two-phase locking protocol is deadlock-free.
o Conservative two-phase locking protocol does not ensure a strict
schedule.
S no. T1 T2 T3
1 Lock-X(R1)
2 Read(R1)
3 Write(R1)
4 Lock-S(R2) LP
5 Read(R2)
6 Unlock-R1,Unlock-R2
7 Lock-X(R1) LP
8 Read(R1)
9 Write(R1)
10 Unlock-R1
11 Lock-S(R1) LP
12 Read-R1
Because of Write(R1) in T1 and after that, Read(R1) in T2 and Read(R1) in T3, there is a
dirty read which means the data written by transaction T1 is being read by transaction
T2 and T3. So if T1 Rollback, T2 and T3 also have to rollback, causing cascading Rollback.
So cascading Rollback is possible in a two-phase locking protocol.
Before the lock point in all three transactions, T1, T2, and T3, there is a growing phase in
transactions, respectively, and after LP, the shrinking phase starts.
Deadlock in 2PL
Let's understand deadlock in a two-phase locking protocol with the help of an example.
S.no T1 T2
1 Lock-X(R1) Lock-X(R2)
2 Read(R1) Read(R2)
3 Lock-X(R2) Lock-X(R1)
So, if we draw a wait-for graph, we can see that T1 waits for T2 and T2 waits for T1,
which creates conflict, and the waiting time never ends, which leads to a deadlock.
Conclusion
Locking is used to handle concurrent transactions in databases
Growing and shrinking phases are two phases of 2PL.
Locks are only obtained in the growing phase.
Locks are neither obtained nor released in the shrinking phase.
Cascading rollback is possible in a two-phase locking protocol
Deadlock is also possible in a two-phase locking protocol.
o
1. Read phase: In this phase, the transaction T is read and executed. It is used to
read the value of various data items and stores them in temporary local variables.
It can perform all the write operations on temporary variables without an update
to the actual database.
2. Validation phase: In this phase, the temporary variable value will be validated
against the actual data to see if it violates the serializability.
3. Write phase: If the validation of the transaction is validated, then the temporary
results are written to the database or system otherwise the transaction is rolled
back.
Validation (Ti): It contains the time when Ti finishes its read phase and starts its
validation phase.
o This protocol is used to determine the time stamp for the transaction for
serialization using the time stamp of the validation phase, as it is the actual phase
which determines if the transaction will commit or rollback.
o Hence TS(T) = validation(T).
o The serializability is determined during the validation process. It can't be decided
in advance.
o While executing the transaction, it ensures a greater degree of concurrency and
also less number of conflicts.
o Thus it contains transactions which have less number of rollbacks.
Multiple Granularity
Let's start by understanding the meaning of granularity.
Multiple Granularity:
o It can be defined as hierarchically breaking up the database into blocks which can
be locked.
o The Multiple Granularity protocol enhances concurrency and reduces lock
overhead.
o It maintains the track of what to lock and how to lock.
o It makes easy to decide either to lock a data item or to unlock a data item. This
type of hierarchy can be graphically represented as a tree.
Compatibility Matrix with Intention Lock Modes: The below table describes the
compatibility matrix for these lock modes:
It uses the intention lock modes to ensure serializability. It requires that if a transaction
attempts to lock a node, then that node must follow these protocols:
Observe that in multiple-granularity, the locks are acquired in top-down order, and locks
must be released in bottom-up order.
o If transaction T1 reads record Ra9 in file Fa, then transaction T1 needs to lock the
database, area A1 and file Fa in IX mode. Finally, it needs to lock Ra2 in S mode.
o If transaction T2 modifies record Ra9 in file Fa, then it can do so after locking the
database, area A1 and file Fa in IX mode. Finally, it needs to lock the Ra9 in X
mode.
o If transaction T3 reads all the records in file Fa, then transaction T3 needs to lock
the database, and area A in IS mode. At last, it needs to lock Fa in S mode.
o If transaction T4 reads the entire database, then T4 needs to lock the database in
S mode.