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

DBMS-21

The document discusses Two-Phase Locking (2PL) and Timestamp-Based Protocols for concurrency control in databases. It outlines the phases of 2PL, variations such as Strict and Rigorous 2PL, and the process of lock conversions, while also explaining the timestamp method and its advantages in avoiding deadlocks. Additionally, it addresses challenges with timestamp ordering protocols and introduces Thomas' Write Rule to enhance concurrency and recoverability.
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)
8 views

DBMS-21

The document discusses Two-Phase Locking (2PL) and Timestamp-Based Protocols for concurrency control in databases. It outlines the phases of 2PL, variations such as Strict and Rigorous 2PL, and the process of lock conversions, while also explaining the timestamp method and its advantages in avoiding deadlocks. Additionally, it addresses challenges with timestamp ordering protocols and introduces Thomas' Write Rule to enhance concurrency and recoverability.
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

Two-Phase Locking &

Timestamp Based
Protocols
Chittaranjan Pradhan

Database Management
Two-Phase Locking

System 21 Protocol
Variations of Two-Phase
Locking
Lock Conversions

Two-Phase Locking & Timestamp Timestamp-Based


Protocols
Timestamps

Based Protocols Timestamp Ordering


Protocols
Problems with Timestamp
Ordering Protocols
Thomas’ Write Rule

Chittaranjan Pradhan
School of Computer Engineering,
KIIT University
21.1
Two-Phase Locking &
Two-Phase Locking Protocol Timestamp Based
Protocols
Chittaranjan Pradhan

Two-Phase Locking Protocol


Two-Phase Locking
Protocol
Two-phase locking protocol is a protocol which ensures Variations of Two-Phase
Locking
serializability Lock Conversions

Timestamp-Based
Protocols
This protocol requires that each transaction issues lock and Timestamps

unlock requests in two phases. The two phases are: Timestamp Ordering
Protocols
Problems with Timestamp
• Growing phase: Here, a transaction acquires all required Ordering Protocols
Thomas’ Write Rule
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

21.2
Two-Phase Locking &
Two-Phase Locking Protocol... Timestamp Based
Protocols
Chittaranjan Pradhan

Two-Phase Locking
Protocol
Variations of Two-Phase
Locking
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
• Two-phase locking may limit the amount of concurrency
that can occur in a schedule 21.3
Two-Phase Locking &
Variations of Two-Phase Locking Timestamp Based
Protocols
Chittaranjan Pradhan

Variations of Two-Phase Locking Two-Phase Locking


Protocol
Conservative or Static 2PL Variations of Two-Phase
Locking
Lock Conversions
• Lock all the data items that you need before transaction
Timestamp-Based
starts execution by predeclaring its read-set and write-set Protocols
Timestamps

• Conservative 2PL is a deadlock-free protocol Timestamp Ordering


Protocols
Problems with Timestamp
• It is difficult to implement because of the need to Ordering Protocols
Thomas’ Write Rule
predeclare the read-set and write-set
Strict 2PL
• A transaction T doesn’t release any of its exclusive locks
until after it commits or aborts
• No other transaction can read/write an item that is written
by T unless T has committed
• Strict 2PL is not deadlock-free

21.4
Two-Phase Locking &
Variations of Two-Phase Locking... Timestamp Based
Protocols
Chittaranjan Pradhan

Two-Phase Locking
Protocol
Variations of Two-Phase Locking... Variations of Two-Phase
Locking
Lock Conversions
Rigorous 2PL
Timestamp-Based
• A transaction T doesn’t release any of its locks until after it Protocols
Timestamps

commits or aborts Timestamp Ordering


Protocols
Problems with Timestamp
Ordering Protocols
Thomas’ Write Rule
• Strict 2PL holds write-locks until it commits; whereas
Rigorous 2PL holds all locks
• Conservative 2PL must lock all its data items before it
starts, so once the transaction starts it is in shrinking
phase; whereas Rigorous 2PL doesn’t unlock any of its
data items until after it terminates

21.5
Two-Phase Locking &
Lock Conversions Timestamp Based
Protocols
T7 Chittaranjan Pradhan
Read(a1);
Read(a2);
Read(a3); Two-Phase Locking
... Protocol
Variations of Two-Phase
Read(an); Locking
Write(a1); Lock Conversions

Timestamp-Based
T8 Protocols
Read(a1); Timestamps
Timestamp Ordering
Read(a3); Protocols
Display(a1+a3); Problems with Timestamp
Ordering Protocols
Thomas’ Write Rule

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
21.6
Two-Phase Locking &
Lock Conversions... Timestamp Based
Protocols
Chittaranjan Pradhan

Thus, the two-phase locking protocol with lock conversions:


Two-Phase Locking
• First Phase: Protocol
Variations of Two-Phase
• Can acquire a lock-S on item Locking
Lock Conversions
• Can acquire a lock-X on item
Timestamp-Based
• Can convert a lock-S to a lock-X (upgrade) Protocols
Timestamps
• Second Phase: Timestamp Ordering
Protocols

• Can release a lock-S Problems with Timestamp


Ordering Protocols
• Can release a lock-X Thomas’ Write Rule

• 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

21.7
Two-Phase Locking &
Lock Conversions... Timestamp Based
Protocols
Chittaranjan Pradhan

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

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

21.8
Two-Phase Locking &
Timestamp-Based Protocols Timestamp Based
Protocols
Timestamp-Based Protocols Chittaranjan Pradhan

The timestamp method for concurrency control doesn’t need


any locks and therefore this method is free from deadlock Two-Phase Locking
Protocol
situation Variations of Two-Phase
Locking
Lock Conversions

Timestamp-Based
Locking methods generally prevent conflicts by making Protocols

transaction to wait; whereas timestamp methods do not make Timestamps


Timestamp Ordering

the transactions to wait. Rather, transactions involved in a Protocols


Problems with Timestamp

conflicting situation are simply rolled back and restarted Ordering Protocols
Thomas’ Write Rule

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 21.9
Two-Phase Locking &
Timestamps Timestamp Based
Protocols
Chittaranjan Pradhan

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

Timestamp-Based
Protocols
The timestamps of the transactions determine the serializability Timestamps

order. Thus, if TS(Ti ) < TS(Tj ), then the system must ensure Timestamp Ordering
Protocols

that the produced schedule is equivalent to a serial schedule in Problems with Timestamp
Ordering Protocols

which Ti appears before Tj Thomas’ Write Rule

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

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

• If TS(Ti ) < W-TS(Q), then Ti needs to read a value of Q that Timestamp-Based


Protocols
was already overwritten. Hence, the read operation is Timestamps

rejected and Ti is rolled back Timestamp Ordering


Protocols
• If TS(Ti ) ≥ W-TS(Q), then the read operation is executed, Problems with Timestamp
Ordering Protocols
and R-TS(Q) is set to maximum of R-TS(Q) and TS(Ti ) Thomas’ Write Rule

• 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 )
21.11
Two-Phase Locking &
Timestamp Ordering Protocols... Timestamp Based
Protocols
Chittaranjan Pradhan

Two-Phase Locking
Protocol
If a transaction Ti is rolled back by the concurrency-control Variations of Two-Phase

scheme, the system assigns it a new timestamp and restarts it Locking


Lock Conversions

Timestamp-Based
T1 T2 T1 T2 T1 T2 Protocols
Read(A); Read(A); Read(A); Timestamps
A:=A-100; A:=A-100; A:=A-100; Timestamp Ordering
Write(A); Read(A); Write(A); Protocols
Read(A); Temp:=0.2*A; Read(A); Problems with Timestamp
Ordering Protocols
Temp:=0.2*A; A:=A-Temp; Temp:=0.2*A;
Thomas’ Write Rule
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

21.12
Two-Phase Locking &
Timestamp Ordering Protocols... Timestamp Based
Protocols
Chittaranjan Pradhan

From the above examples, we can say that the timestamp Two-Phase Locking
Protocol
ordering protocol always ensures conflict serializability. This is Variations of Two-Phase
Locking
because conflicting operations are processed in timestamp Lock Conversions

order Timestamp-Based
Protocols
Timestamps
Timestamp Ordering
The protocol ensures freedom from deadlock, since no Protocols
Problems with Timestamp
transaction ever waits Ordering Protocols
Thomas’ Write Rule

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

21.13
Two-Phase Locking &
Problems with Timestamp Ordering Protocols Timestamp Based
Protocols
Chittaranjan Pradhan

Problems with Timestamp Ordering Protocols Two-Phase Locking


Protocol
Suppose Ti aborts, but Tj has read a data item written by Ti . Variations of Two-Phase
Locking

Then, Tj must abort; if Tj had been allowed to omit earlier, the Lock Conversions

Timestamp-Based
schedule is not recoverable. Further, any transaction that has Protocols

read a data item written by Tj must abort. This can lead to Timestamps
Timestamp Ordering

cascading rollback; i.e. chain of rollbacks Protocols


Problems with Timestamp
Ordering Protocols
Thomas’ Write Rule

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

21.14
Two-Phase Locking &
Thomas’ Write Rule Timestamp Based
Protocols
Chittaranjan Pradhan

Thomas’ Write Rule Two-Phase Locking


Protocol
One of the problems with basic timestamp ordering protocol is Variations of Two-Phase
Locking

that it does not guarantee recoverable schedules Lock Conversions

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

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

21.15
Two-Phase Locking &
Thomas’ Write Rule... Timestamp Based
Protocols
Chittaranjan Pradhan

Two-Phase Locking
Protocol
Thomas’ Write Rule... Variations of Two-Phase
Locking

• Ti issues Read(Q): Lock Conversions

Timestamp-Based
• Remains same Protocols
Timestamps
• Ti issues write(Q): Timestamp Ordering
Protocols
• If TS(Ti ) < R-TS(Q), Ti is aborted, rolled back and is Problems with Timestamp
Ordering Protocols
restarted with a new timestamp Thomas’ Write Rule

• 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

21.16

You might also like