Unit5 DBMS
Unit5 DBMS
Lock-based Protocols
A lock is a data variable which is associated with a data item. This lock
signifies that operations that can be performed on the data item. Locks
help synchronize access to the database items by concurrent
transactions.
A shared lock is also called a Read-only lock. With the shared lock, the
data item can be shared between transactions. This is because you will
never have permission to update data on the data item.
For example, consider a case where two transactions are reading the
account balance of a person. The database will let them read by placing
a shared lock. However, if another transaction wants to update that
account's balance, shared lock prevent it until the reading process is
over.
2. Exclusive Lock (X):
With the Exclusive Lock, a data item can be read as well as written. This
is exclusive and can't be held concurrently on the same data item. X-lock
is requested using lock-x instruction. Transactions may unlock the data
item after finishing the 'write' operation.
Starvation
T1 T2
1 LOCK-S(A)
2 LOCK-S(A)
3 LOCK-X(B)
4 ……. ……
5 UNLOCK(A)
6 LOCK-X(C)
7 UNLOCK(B)
8 UNLOCK(A)
9 UNLOCK(C)
10 ……. ……
What is LOCK POINT ?The Point at which the growing phase ends, i.e.,
when transaction takes the final lock it needs to carry on its work. Now
look at the schedule, you’ll surely understand.
Drawbacks:
• Cascading Rollback is possible under 2-PL.
• Deadlocks and Starvation is possible.
Cascading Rollbacks in 2-PL –
Let’s see the following Schedule:
•
• Take a moment to analyze the schedule. Yes, you’re correct,
because of Dirty Read in T2 and T3 in lines 8 and 12 respectively,
when T1 failed we have to rollback others also. Hence Cascading
Rollbacks are possible in 2-PL. I have taken skeleton schedules
as examples because it’s easy to understand when it’s kept
simple. When explained with real time transaction problems with
many variables, it becomes very complex.
• Deadlock in 2-PL –
Consider this simple example, it will be easy to understand.Say we
have two transactions T1 and T2.
• Schedule: Lock-X1(A) Lock-X2(B) Lock-X1(B) Lock-X2(A)
• Drawing the precedence graph, you may detect the loop. So
Deadlock is also possible in 2-PL.
Now recall the rules followed in Basic 2-PL, over that we make some
extra modifications. Let’s now see what are the modifications and what
drawbacks they solve.
Strict 2-PL –
This requires that in addition to the lock being 2-Phase all Exclusive(X)
Locks held by the transaction be released until after the Transaction
Commits.
Rigorous 2-PL –
This requires that in addition to the lock being 2-Phase all Exclusive(X)
and Shared(S) Locks held by the transaction be released until after the
Transaction Commits.
Note the difference between Strict 2-PL and Rigorous 2-PL is that
Rigorous is more restrictive, it requires both Exclusive and Shared locks
to be held until after the Transaction commits and this is what makes the
implementation of Rigorous 2-PL more easy.
Timestamp-based Protocols
This rule states if TS(Ti) < W-timestamp(X), then the operation is rejected
and Ti is rolled back.
Time-stamp ordering rules can be modified to make the schedule view
serializable.
Instead of making Ti rolled back, the 'write' operation itself is ignored.
Other protocols for concurrency control keep the old values of a data item
when the item is updated. These are known as multiversion
concurrency control, because several versions (values) of an item are
maintained. When a transaction requires access to an item,
an appropriate version is chosen to maintain the serializability of the
currently executing schedule, if possible. The idea is that some read
operations that would be rejected in other techniques can still be accepted
by reading an older version of the item to maintain serializability. When a
transaction writes an item, it writes a new version and the old version(s)
of the item are retained. Some multiver-sion concurrency control
algorithms use the concept of view serializability rather than conflict
serializability.
An obvious drawback of multiversion techniques is that more storage is
needed to maintain multiple versions of the database items. However,
older versions may have to be maintained anyway—for example, for
recovery purposes. In addition, some database applications require older
versions to be kept to maintain a history of the evolution of data item
values. The extreme case is a temporal database (see Secton 26.2),
which keeps track of all changes and the times at which they occurred. In
such cases, there is no additional storage penalty for multiversion
techniques, since older versions are already maintained.
Several multiversion concurrency control schemes have been proposed.
We discuss two schemes here, one based on timestamp ordering and the
other based on 2PL. In addition, the validation concurrency control
method (see Section 22.4) also maintains multiple versions.
In this method, several versions X1, X2, ..., Xk of each data item X are
maintained. For each version, the value of version Xi and the following two
timestamps are kept:
read_TS(Xi). The read timestamp of Xi is the largest of all the
timestamps of transactions that have successfully read version Xi.
write_TS(Xi). The write timestamp of Xi is the timestamp of the
transaction that wrote the value of version Xi.
Whenever a transaction T is allowed to execute a write_item(X)
operation, a new version Xk+1 of item X is created, with both
the write_TS(Xk+1) and the read_TS(Xk+1) set to TS(T). Correspondingly,
when a transaction T is allowed to read the value of version Xi, the value
of read_TS(Xi) is set to the larger of the current read_TS(Xi) and TS(T).
To ensure serializability, the following rules are used:
If transaction T issues a write_item(X) operation, and
version i of X has the highest write_TS(Xi) of all versions of X that is
also less than or equal to TS(T), and read_TS(Xi) > TS(T), then abort and
roll back transaction T; otherwise, create a new
version Xj of X with read_TS(Xj) = write_TS(Xj) = TS(T).
In this multiple-mode locking scheme, there are three locking modes for
an item: read, write, and certify, instead of just the two modes (read, write)
discussed previously. Hence, the state of LOCK(X) for an item X can be
one of read-locked, write-locked, certify-locked, or unlocked. In the
standard locking scheme, with only read and write locks (see Section
22.1.1), a write lock is an exclusive lock. We can describe the relationship
between read and write locks in the standard scheme by means of
the lock compatibility table shown in Figure 22.6(a). An entry
of Yes means that if a transaction T holds the type of lock specified in the
column header
transactions. It uses the local copy of the data for the rollback
cascading rollbacks.
The Validation based protocol works based upon the following three
phases:
multiple data items are being read in this phase and the protocol
local variable that holds the data item instead of writing it to the
database.
with the actual values in the database and to check the view
serializability condition.
• Write Phase: The write phase ensures valid data to be written to
the validation protocol. There are three timestamps that control the
when the data item being read and executed in the read phase of
timestamp when T1 completed the read phase and started with the
validation phase.
protocol conditions.
the transaction.
Transaction T1 Transaction T2
Read(A) Read(A)
Read(B) A=A-40
<Validate> Read(B)
Display(B+A) B=B+80
<Validate>
Write(A)
Write(B)
database. In the next step, the transaction T2 also reads the same data
with operation Read (B). Now the validation based protocol comes into
the action in the transaction T1 that validates the time stamp of the start
T2.
the timestamps. In the example shown in the table indicates both the
are performed by the transaction T2 using Write (A) and Write (B)
statements.
protocol in DBMS.
Advantages
Below are mentioned the advantages:
• The validation based protocol considers high priority to the greater
Disadvantages
Below are mentioned some of the disadvantages:
the transactions.
Conclusion
This protocol is implemented in various enterprise systems such as the
multi-user environment.
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.
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:
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 R a9 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.