Concurrency Control Protocol

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Lock-Based 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.

There are four types of lock protocols available:


1. Simplistic lock protocol
It is the simplest way of locking the data while transaction. Simplistic lock-based
protocols allow all the transactions to get the lock on the data before insert or delete or
update on it. It will unlock the data item after completing the transaction.

2. Pre-claiming Lock Protocol

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.

3. Two-phase locking (2PL)

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:

1. Upgrading of lock (from S(a) to X (a)) is allowed in growing phase.


2. Downgrading of lock (from X(a) to S(a)) must be done in shrinking phase.

Example:
The following way shows how unlocking and locking work with 2-PL.

Transaction T1:

o Growing phase: from step 1-3


o Shrinking phase: from step 5-7
o Lock point: at 3

Transaction T2:

o Growing phase: from step 2-6


o Shrinking phase: from step 8-9
o Lock point: at 6

Locking in a database management system is used for handling transactions in


databases. The two-phase locking protocol ensures serializable conflict schedules. A
schedule is called conflict serializable if it can be transformed into a serial schedule by
swapping non-conflicting operations.

Two-Phase Locking Types


Two-phase Locking is further classified into three types :

1. Strict two-phase locking protocol :


o The transaction can release the shared lock after the lock point.
o The transaction can not release any exclusive lock until the transaction is
committed.
o In strict two-phase locking protocol, if one transaction rollback then the other
transaction should also have to roll back. The transactions are dependent on
each other. This is called Cascading schedule

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.

It does not have cascading abort as 2PL does.

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.

Cascading Roll Back in 2PL


Let's understand cascading rollback in 2Pl with the help of an example :
Cascading rollback means if one transaction rollback then other transactions dependent
on it should also roll back.

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.

In the above transaction, LP denotes Lock Point.

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)

 In the above transaction in step 3, T1 cannot execute the exclusive lock in R2 as


the Exclusive lock is already obtained by transaction T2 in step 1.
 Also, in step 3, the Exclusive lock on R1 cannot be executed by T2 as T1 has
already applied the Exclusive lock on 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.

Hence, deadlock is possible in a two-phase locking protocol.

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

Validation Based Protocol


Validation phase is also known as optimistic concurrency control technique. In the
validation based protocol, the transaction is executed in the following three phases:

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.

Here each phase has the following different timestamps:

Start(Ti): It contains the time when Ti started its execution.

Validation (Ti): It contains the time when Ti finishes its read phase and starts its
validation phase.

Finish(Ti): It contains the time when Ti finishes its write 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.

Granularity: It is the size of data item allowed to lock.

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.

For example: Consider a tree which has four levels of nodes.

o The first level or higher level shows the entire database.


o The second level represents a node of type area. The higher level database
consists of exactly these areas.
o The area consists of children nodes which are known as files. No file can be
present in more than one area.
o Finally, each file contains child nodes known as records. The file has exactly those
records that are its child nodes. No records represent in more than one file.
o Hence, the levels of the tree starting from the top level are as follows:
1. Database
2. Area
3. File
4. Record
In this example, the highest level shows the entire database. The levels below are file,
record, and fields.

There are three additional lock modes with multiple granularity:

Intention Mode Lock


Intention-shared (IS): It contains explicit locking at a lower level of the tree but only
with shared locks.

Intention-Exclusive (IX): It contains explicit locking at a lower level with exclusive or


shared locks.
Shared & Intention-Exclusive (SIX): In this lock, the node is locked in shared mode,
and some node is locked in exclusive mode by the same transaction.

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:

o Transaction T1 should follow the lock-compatibility matrix.


o Transaction T1 firstly locks the root of the tree. It can lock it in any mode.
o If T1 currently has the parent of the node locked in either IX or IS mode, then the
transaction T1 will lock a node in S or IS mode only.
o If T1 currently has the parent of the node locked in either IX or SIX modes, then
the transaction T1 will lock a node in X, SIX, or IX mode only.
o If T1 has not previously unlocked any node only, then the Transaction T1 can lock
a node.
o If T1 currently has none of the children of the node-locked only, then Transaction
T1 will unlock a node.

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.

You might also like