0% found this document useful (0 votes)
159 views

Database Management System 22: Locking Protocols

The document discusses various locking protocols used in database management systems. It describes the two-phase locking protocol, which requires transactions to acquire all locks in a growing phase without releasing any, followed by a shrinking phase where all locks are released. It ensures serializability but not freedom from deadlock. Strict two-phase locking avoids cascading rollbacks by requiring exclusive locks to be held until commit or abort. Lock conversions allow upgrading shared locks to exclusive locks in the growing phase and downgrading exclusive locks in the shrinking phase.

Uploaded by

089ASHUTOSH PATI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views

Database Management System 22: Locking Protocols

The document discusses various locking protocols used in database management systems. It describes the two-phase locking protocol, which requires transactions to acquire all locks in a growing phase without releasing any, followed by a shrinking phase where all locks are released. It ensures serializability but not freedom from deadlock. Strict two-phase locking avoids cascading rollbacks by requiring exclusive locks to be held until commit or abort. Lock conversions allow upgrading shared locks to exclusive locks in the growing phase and downgrading exclusive locks in the shrinking phase.

Uploaded by

089ASHUTOSH PATI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Locking Protocols

Chittaranjan Pradhan

Database Management Two-Phase Locking


Protocol

System 22
Lock Conversions

Timestamp-Based
Protocols

Locking Protocols
Timestamps
Timestamp Ordering
Protocols
Problems with Timestamp
Ordering Protocols
Thomas’ Write Rule

Chittaranjan Pradhan
School of Computer Engineering,
KIIT University
22.1
Locking Protocols
Two-Phase Locking Protocol
Chittaranjan Pradhan

Two-Phase Locking
Two-Phase Locking Protocol Protocol
Lock Conversions
Two-phase locking protocol is a protocol which ensures Timestamp-Based
serializability Protocols
Timestamps
Timestamp Ordering
Protocols

This protocol requires that each transaction issues lock and Problems with Timestamp
Ordering Protocols

unlock requests in two phases. The two phases are: Thomas’ Write Rule

• Growing phase: Here, a transaction acquires all required


locks without unlocking any data, i.e. the transaction may
not release any lock
• Shrinking phase: Here, a transaction releases all locks
and cannot obtain any new lock
The point in the schedule where the transaction has obtained
its final lock is called the lock point of the transaction

Transactions can be ordered according to their lock points

22.2
Locking Protocols
Two-Phase Locking Protocol...
Chittaranjan Pradhan

Two-Phase Locking
Protocol
Lock Conversions

Timestamp-Based
Protocols
Timestamps
Timestamp Ordering
Protocols
Problems with Timestamp
Ordering Protocols
Thomas’ Write Rule

• Two transactions cannot have conflicting locks


• No unlock operation can precede a lock operation in the
same transaction
• No data are affected until all locks are obtained, i.e. until
the transaction is in its locked point
22.3
Locking Protocols
Two-Phase Locking Protocol...
Chittaranjan Pradhan

Two-Phase Locking Protocol


Two-Phase Locking
Two-phase locking does not ensure freedom from deadlock. Protocol
Lock Conversions
Along with the serializability property, the schedules should be Timestamp-Based
cascadeless in nature Protocols
Timestamps
Timestamp Ordering
Protocols
Cascading rollback is possible under two-phase locking Problems with Timestamp
Ordering Protocols
protocol Thomas’ Write Rule

T4 T5 T6
Lock-X(A);
Read(A);
Lock-S(B);
Read(B);
Write(A);
Schedule7 Unlock(A);
Lock-X(A);
Read(A);
Write(A);
Unlock(A);
Lock-S(A);
Read(A);

22.4
Locking Protocols
Two-Phase Locking Protocol...
Chittaranjan Pradhan

Two-Phase Locking
Protocol
Lock Conversions

• Cascading rollbacks can be avoided by a modification of Timestamp-Based


Protocols
the original two-phase locking protocol called strict Timestamps

two-phase locking protocol. In this protocol, a transaction Timestamp Ordering


Protocols

must hold all its exclusive locks till it commits or aborts Problems with Timestamp
Ordering Protocols
Thomas’ Write Rule

• Rigorous two-phase locking protocol is even stricter, which


requires that all the locks be held until the transaction
commits or aborts. In this protocol, transactions can be
serialized in the order in which they commit

• Most database systems implement either strict or rigorous


two-phase locking

22.5
Locking Protocols
Lock Conversions
Chittaranjan Pradhan
T7
Read(a1);
Read(a2); Two-Phase Locking
Protocol
Read(a3);
Lock Conversions
...
Read(an); Timestamp-Based
Protocols
Write(a1); Timestamps
Timestamp Ordering
Protocols
T8
Problems with Timestamp
Read(a1); Ordering Protocols
Read(a3); Thomas’ Write Rule

Display(a1+a3);

Lock Conversions
Lock Upgrade: This is the process in which a shared lock is
upgraded to an exclusive lock

Lock Downgrade: This is the process in which an exclusive


lock is downgraded to a shared lock

Lock upgrading can take place only in the growing phase,


where as lock downgrading can take place only in the shrinking
phase
22.6
Locking Protocols
Lock Conversions...
Chittaranjan Pradhan

Thus, the two-phase locking protocol with lock conversions: Two-Phase Locking
Protocol

• First Phase: Lock Conversions

Timestamp-Based
• Can acquire a lock-S on item Protocols
Timestamps
• Can acquire a lock-X on item Timestamp Ordering
• Can convert a lock-S to a lock-X (upgrade) Protocols
Problems with Timestamp
Ordering Protocols
• Second Phase: Thomas’ Write Rule

• Can release a lock-S


• Can release a lock-X
• Can convert a lock-X to a lock-S (downgrade)
Like the basic two-phase locking protocol, two-phase locking
with lock conversion generates only conflict-serializable
schedules and transactions can be serialized by their lock
points

If the exclusive locks are held until the end of the transaction,
then the schedules became cascadeless

22.7
Locking Protocols
Lock Conversions...
Chittaranjan Pradhan

T7 T8 Two-Phase Locking
Lock-S(a1); Protocol
Lock Conversions
Read(a1); Timestamp-Based
Lock-S(a1); Protocols
Timestamps
Read(a1); Timestamp Ordering

Lock-S(a2); Protocols
Problems with Timestamp
Read(a2); Ordering Protocols
Thomas’ Write Rule
Lock-S(a3);
Read(a3);
Schedule8 Lock-S(a3);
Read(a3);
Display(a1+a3);
Unlock(a1);
Unlock(a3);
...
Lock-S(an);
Read(an);
Upgrade(a1);
Write(a1);

22.8
Locking Protocols
Timestamp-Based Protocols
Chittaranjan Pradhan
Timestamp-Based Protocols
The timestamp method for concurrency control doesn’t need Two-Phase Locking
Protocol
any locks and therefore this method is free from deadlock Lock Conversions

situation Timestamp-Based
Protocols
Timestamps
Timestamp Ordering
Locking methods generally prevent conflicts by making Protocols
Problems with Timestamp
transaction to wait; whereas timestamp methods do not make Ordering Protocols
Thomas’ Write Rule
the transactions to wait. Rather, transactions involved in a
conflicting situation are simply rolled back and restarted

A timestamp is a unique identifier created by the Database


system that indicates the relative starting time of a transaction.
Timestamps are generated either using the system clocks or
by incrementing a logical counter every time a new transaction
starts

Timestamp protocol is a concurrency control protocol in which


the fundamental goal is to order the transactions globally in
such a way that older transactions get priority in the event of a
conflict 22.9
Locking Protocols
Timestamps
Chittaranjan Pradhan

Two-Phase Locking
Timestamps Protocol
Lock Conversions
Timestamp TS(Ti ) is assigned by the database system before Timestamp-Based
the transaction Ti starts its execution Protocols
Timestamps
Timestamp Ordering
Protocols

The timestamps of the transactions determine the serializability Problems with Timestamp
Ordering Protocols

order. Thus, if TS(Ti ) < TS(Tj ), then the system must ensure Thomas’ Write Rule

that the produced schedule is equivalent to a serial schedule in


which Ti appears before Tj

There are two timestamp values associated with each data


item Q:
• W-Timestamp(Q): It denotes the largest timestamp of any
transaction that executed write(Q) operation successfully
• R-Timestamp(Q): It denotes the largest timestamp of any
transaction that executed read(Q) operation successfully

22.10
Locking Protocols
Timestamp Ordering Protocols
Chittaranjan Pradhan

Timestamp Ordering Protocols


Two-Phase Locking
This ensures that any conflicting read and write operations are Protocol
Lock Conversions
executed in timestamp order
Timestamp-Based
• Suppose transaction Ti issues read(Q): Protocols
Timestamps

• If TS(Ti ) < W-TS(Q), then Ti needs to read a value of Q that Timestamp Ordering
Protocols
was already overwritten. Hence, the read operation is Problems with Timestamp
Ordering Protocols
rejected and Ti is rolled back Thomas’ Write Rule

• If TS(Ti ) ≥ W-TS(Q), then the read operation is executed,


and R-TS(Q) is set to maximum of R-TS(Q) and TS(Ti )
• Suppose transaction Ti issues write(Q):
• If TS(Ti ) < R-TS(Q), then the value of Q that Ti is producing
was needed previously, and the system assumed that the
value would never be produced. Hence, the system rejects
the write operation and rolls Ti back
• If TS(Ti ) < W-TS(Q), then Ti is attempting to write an
obsolete value of Q. Hence, the system rejects this write
operation and rolls Ti back
• Otherwise, the system executes the write operation and
sets W-TS(Q) to TS(Ti )
22.11
Locking Protocols
Timestamp Ordering Protocols...
Chittaranjan Pradhan

Two-Phase Locking
Protocol
Lock Conversions
If a transaction Ti is rolled back by the concurrency-control Timestamp-Based
scheme, the system assigns it a new timestamp and restarts it Protocols
Timestamps
Timestamp Ordering
T1 T2 T1 T2 T1 T2 Protocols
Read(A); Read(A); Read(A); Problems with Timestamp
Ordering Protocols
A:=A-100; A:=A-100; A:=A-100;
Thomas’ Write Rule
Write(A); Read(A); Write(A);
Read(A); Temp:=0.2*A; Read(A);
Temp:=0.2*A; A:=A-Temp; Temp:=0.2*A;
A:=A-Temp; Write(A); A:=A-Temp;
Write(A); Read(B); Read(B);
Read(B); Write(A); B:=B+100;
B:=B+100; Read(B); Write(A);
Write(B); B:=B+100; Read(B);
Read(B); Write(B); B:=B+ Temp;
B:=B+ Temp; B:=B+ Temp; Write(B);
Write(B); Write(B); Write(B);
Schedule3 Schedule4 Schedule5

22.12
Locking Protocols
Timestamp Ordering Protocols...
Chittaranjan Pradhan

Two-Phase Locking
From the above examples, we can say that the timestamp Protocol
Lock Conversions

ordering protocol always ensures conflict serializability. This is Timestamp-Based


Protocols
because conflicting operations are processed in timestamp Timestamps

order Timestamp Ordering


Protocols
Problems with Timestamp
Ordering Protocols

The protocol ensures freedom from deadlock, since no Thomas’ Write Rule

transaction ever waits

However, there is a possibility of starvation of long transactions


if a sequence of conflicting short transactions causes repeated
restarting of the long transaction

If a transaction is found to be getting restarted repeatedly;


conflicting transactions need to be temporarily blocked to
enable the transaction to finish

22.13
Locking Protocols
Problems with Timestamp Ordering Protocols
Chittaranjan Pradhan

Two-Phase Locking
Problems with Timestamp Ordering Protocols Protocol
Lock Conversions

Suppose Ti aborts, but Tj has read a data item written by Ti . Timestamp-Based


Protocols
Then, Tj must abort; if Tj had been allowed to omit earlier, the Timestamps
Timestamp Ordering
schedule is not recoverable. Further, any transaction that has Protocols
Problems with Timestamp
read a data item written by Tj must abort. This can lead to Ordering Protocols
Thomas’ Write Rule
cascading rollback; i.e. chain of rollbacks

The solutions to this type of problem are:


• A transaction is structured in such a way that its writes are
performed at the end of its processing. At the same time,
all writes of a transaction form an atomic action; i.e. no
transaction may execute while a transaction is being
written, or
• Wait for the data to be committed before reading it

22.14
Locking Protocols
Thomas’ Write Rule
Chittaranjan Pradhan

Two-Phase Locking
Thomas’ Write Rule Protocol
Lock Conversions

One of the problems with basic timestamp ordering protocol is Timestamp-Based


Protocols
that it does not guarantee recoverable schedules Timestamps
Timestamp Ordering
Protocols
Problems with Timestamp
A modification to the basic timestamp ordering protocol that Ordering Protocols
Thomas’ Write Rule
relaxes the conflict serializability can be used to provide
greater concurrency by rejecting obsolete write operations

T9 T10
Read(Q);
Write(Q);
Schedule9
Write(Q);
Read(M);
Write(M);

22.15
Locking Protocols
Thomas’ Write Rule...
Chittaranjan Pradhan

Two-Phase Locking
Protocol
Lock Conversions
Thomas’ Write Rule... Timestamp-Based
Protocols
• Ti issues Read(Q): Timestamps
Timestamp Ordering
• Remains same Protocols
Problems with Timestamp
Ordering Protocols
• Ti issues write(Q): Thomas’ Write Rule

• If TS(Ti ) < R-TS(Q), Ti is aborted, rolled back and is


restarted with a new timestamp
• If TS(Ti ) < W-TS(Q), Ti is attempting to write an obsolete
value of Q. Hence, this write operation can be ignored
• Otherwise, the system executes the write operation and
sets W-TS(Q) = TS(Ti )
Thomas’ write rule makes use of view Serializability by deleting
obsolete write operations from the transactions that issue them

22.16

You might also like