Module - 6
Module - 6
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:
• It is also known as a Read-only lock. In a shared lock, the data item
can only read by the transaction.
• 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:
In the exclusive lock, the data item can be both reads as well as
written by the transaction.
This lock is exclusive, and in this lock, multiple transactions do not
modify the same data simultaneously.
Granting of Locks
When a transaction requests a lock on a data item in a particular
mode, and no other transaction has a lock on the same data item in
a conflicting mode, the lock can be granted. However, care must be
taken to avoid the following scenario. Suppose a transaction T2 has a
shared-mode lock on a data item, and another transaction T1
requests an exclusive-mode lock on the data item. Clearly, T1 has to
wait for T2 to release the shared-mode lock. Meanwhile, a
transaction T3 may request a shared-mode lock on the same data
item. The lock request is compatible with the lock granted to
T2, so T3 may be granted the shared-mode lock. At this point T2 may
release the lock, but still T1 has to wait for T3 to finish. But again,
there may be a new transaction T4 that requests a shared-mode lock
on the same data item, and is granted the lock before T3 releases it.
In fact, it is possible that there is a sequence of transactions that
each requests a shared-mode lock on the data item, and each
transaction releases the lock a short while after it is granted, but T1
never gets the exclusive-mode lock on the data item. The transaction
T1 may never make progress, and is said to be starved.
We can avoid starvation of transactions by granting locks in the
following manner:
When a transaction Ti requests a lock on a data item Q in a
particular mode M, the concurrency-control manager grants the lock
provided that
1. There is no other other transaction holding a lock on Q in amode
that conflicts with M.
2. There is no other transaction that is waiting for a lock on Q, and
that made its lock request before Ti.
Thus, a lock request will never get blocked by a lock request that is
made later.
Disadvantages of locking
• Lock management overhead.
• Deadlock detection/resolution.
• Concurrency is significantly lowered, when congested nodes are
locked.
• To allow a transaction to abort itself when mistakes occur, locks
can’t be released until the end of transaction, thus currency is
significantly lowered
• (Most Important) Conflicts are rare. (We might get better
performance by not locking, and instead checking for conflicts at
commit time.)
Timestamp Ordering Protocol
• The Timestamp Ordering Protocol is used to order the transactions
based on their Timestamps. The order of transaction is nothing but
the ascending order of the transaction creation.
• The priority of the older transaction is higher that's why it
executes first. To determine the timestamp of the transaction, this
protocol uses system time or logical counter.
• The lock-based protocol is used to manage the order between
conflicting pairs among transactions at the execution time. But
Timestamp based protocols start working as soon as a transaction is
created.
• Let's assume there are two transactions T1 and T2. Suppose the
transaction T1 has entered the system at 007 times and transaction
T2 has entered the system at 009 times. T1 has the higher priority,
so it executes first as it is entered the system first.
• The timestamp ordering protocol also maintains the timestamp of
last 'read' and 'write' operation on a data.
Basic Timestamp ordering protocol works as follows:
1. Check the following condition whenever a transaction Ti issues
a Read (X) operation:
• If W_TS(X) >TS(Ti) then the operation is rejected.
• If W_TS(X) <= TS(Ti) then the operation is executed.
• Timestamps of all the data items are updated.
2. Check the following condition whenever a transaction Ti issues
a Write(X) operation:
• If TS(Ti) < R_TS(X) then the operation is rejected.
• If TS(Ti) < W_TS(X) then the operation is rejected and Ti is rolled
back otherwise the operation is executed.
Where,
TS(TI) denotes the timestamp of the transaction Ti.
R_TS(X) denotes the Read time-stamp of data-item X.
W_TS(X) denotes the Write time-stamp of data-item X.
Advantages and Disadvantages of TO protocol:
• TO protocol ensures serializability since the precedence graph is as
follows:
Since the graph has no cycle, the system is not in a deadlock state.
Suppose now that transaction T28 is requesting an item held by T27.
The edge T28 → T27 is added to the wait-for graph, resulting in the
new system state . This time, the graph contains the cycle
T26 → T28 →T27 →T26
implying that transactions T26, T27, and T28 are all deadlocked.
Consequently, the question arises: When should we invoke the
detection algorithm?
The answer depends on two factors:
1. How often does a deadlock occur?
2. How many transactions will be affected by the deadlock?
If deadlocks occur frequently, then the detection algorithm should
be invoked more frequently than usual. Data items allocated to
deadlocked transactions will be unavailable to other transactions
until the deadlock can be broken. In addition, the number of cycles
in the graph may also grow. In the worst case, we would invoke the
detection algorithm every time a request for allocation could not be
granted immediately.