Unit-5 DBMS
Unit-5 DBMS
Unit-5 DBMS
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.
AD
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.
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:
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 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.
AD
Where,
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.
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.
Thomas Write Rule provides the guarantee of serializability order for the protocol. It
improves the Basic Timestamp Ordering Algorithm.
o If TS(T) < R_TS(X) then transaction T is aborted and rolled back, and operation
is rejected.
o If TS(T) < W_TS(X) then don't execute the W_item(X) operation of the
transaction and continue processing.
o If neither condition 1 nor condition 2 occurs, then allowed to execute the
WRITE operation by transaction Ti and set W_TS(X) to TS(T).
If we use the Thomas write rule then some serializable schedule can be permitted
that does not conflict serializable as illustrate by the schedule in a given figure:
Figure: A Serializable Schedule that is not Conflict Serializable
In the above figure, T1's read and precedes T1's write of the same data item. This
schedule does not conflict serializable.
Thomas write rule checks that T2's write is never seen by any transaction. If we delete
the write operation in transaction T2, then conflict serializable schedule can be
obtained which is shown in below figure.
o Whenever more than one transaction is being executed, then the interleaved
of logs occur. During recovery, it would become difficult for the recovery
system to backtrack all logs and then start recovering.
o To ease this situation, 'checkpoint' concept is used by most DBMS.
Multi-version protocol aims to reduce the delay for read operations. It maintains
multiple versions of data items. Whenever a write operation is performed, the
protocol creates a new version of the transaction data to ensure conflict-free and
successful read operations.
Stores a complete history of all changes Provides highest Most complex of the
History-
made to a record, allowing for easy level of data MVCC techniques
based
rollback of transactions consistency
With MVCC, the database can allow multiple transactions to read and write data
without locking the entire database.
Fewer issues with multiple transactions trying to access the same data
MVCC helps reduce conflicts between transactions accessing the same data.
Since MVCC allows multiple transactions to read data at the same time, it
improves the speed of reading data.
MVCC ensures that data is protected from being changed by other transactions
while a transaction is making changes to it.
Deadlocks occur when two or more transactions are waiting for each other to
release a lock, causing the system to come to a halt. MVCC can reduce the
number of these occurrences.
AD
MVCC Locking
Creates a new version of a record with an Locks a record during write operation
Approach incremented version number during write
operation
Concurrent read operations can occur on the old Concurrent read operations are not
Read
version of the record while the new version is allowed until the lock is released
operations
being updated
Writes operate on a new version of the record, Only one write can occur at a time, as
Write
allowing multiple writes to occur simultaneously the record is locked during the write
operations
operation
No locks are needed, which eliminates the need Locks are used, which can lead to
Locks
for contention and deadlock management contention and deadlock issues
Record The database grows in size as new versions of The database remains the same size, as
versions records are created, leading to potential bloat no new versions of records are created