Unit 4 - 2
Unit 4 - 2
UNIT - IV TRANSACTIONS
RECOVERABLE SCHEDULES
UNIT - IV TRANSACTIONS
LOCKING
• lock_item
• unlock_item
RULES
Upgrading:
Conversion from read-lock(X) to write-lock(X)
Downgrading:
Conversion from write-lock(X) to read-lock(X)
Locking Protocol
T1 T2
lock-s(A) //Shared lock on A
read(A)
lock-s(B) // Shared lock on B
read(B)
lock-s(A) // Shared lock on A
read(A)
//releasing lock on A
unlock(A)
unlock(A) //releasing lock on A
//releasing lock on B
unlock(B)
Two-phase Locking Protocol
T1 T2
lock-x(A) // Exclusive lock on A
read(A)
lock-s(A) //Requesting Read lock
on A, not granted
write(A)
//releasing lock on A
unlock(A)
lock-s(A) //Shared lock on A
read(A)
unlock(A) //releasing lock on A
commit
commit
Two-phase Locking Protocol
Consider two transactions T1, T2
T1 T2
R(A)
W(A)
R(A)
W(A)
R(B)
W(B)
R(B)
W(B)
commit
commit
Two-phase Locking Protocol
UNIT - IV TRANSACTIONS
TIMESTAMP
second.
TIMESTAMP
in the system
serializability order.
• W-timestamp(X)
largest timestamp of any transaction that executed
write(Q) successfully
• R-timestamp(X)
largest timestamp of any transaction that executed
read(Q) successfully.
TIMESTAMP-ORDERING PROTOCOL
Operation:
1.Suppose that transaction Ti issues read(X)
T1 T2
R(X)
TS of T1:1
TS of T2:2 W(X)
R(X)
TIMESTAMP-ORDERING PROTOCOL
T1 T2 R-timestamp(X) :0
W-timestamp(X) :0
TS of T1:1 R(X)
TS of T2:2 W(X)
R(X)
• T1 issues R(X)
• TS-T1 is 1
• TS-T2 >W-timestamp(X), Read(X) is allowed
• R-timestamp is set to 1
TIMESTAMP-ORDERING PROTOCOL
T1 T2 R-timestamp(X) :1
TS of T1:1 R(X) W-timestamp(X) :0
TS of T2:2 W(X)
R(X)
• T2 issues W(X)
• TS-T2 is 2
• TS-T2 >R-timestamp(X), Write(X) is allowed
• W-timestamp is set to 2
TIMESTAMP-ORDERING PROTOCOL
T1 T2 R-timestamp(X) :1
TS of T1:1 R(X) W-timestamp(X) :2
TS of T2:2 W(X)
R(X)
• T1 issues R(X)
• TS-T1 is 1
• TS-T1 <W-timestamp(X), Read(X) is allowed
• Rollback T1
Thomas’s Write Rule
T1 T2 R-timestamp(X) :1
TS of T1:1 R(X) W-timestamp(X) :2
TS of T2:2 W(X)
R(X)
• T1 issues R(X)
• TS-T1 is 1
• TS-T1 <W-timestamp(X), Read(X) is allowed
• Rollback T1
Thomas’s Write Rule
UNIT - IV TRANSACTIONS
DEADLOCK
1. Deadlock prevention
2. Deadlock detection and recovery
Deadlock prevention
Deadlock prevention protocol to ensure that the system
will never enter a deadlock state
1. wait-die
• Non-preemptive technique
• Ti requests a data item currently held by Tj
2.wound-wait
• Preemptive technique
• Ti requests a data item currently held by Tj
• Ti is allowed to wait only if it has a timestamp larger
than that of Tj (that is, Ti is younger than Tj )
Lock timeouts
• another approach for prevention
• a transaction that has requested a lock waits for at
most a specified amount of time.
• Lock not granted, Transaction 🡪 time out
• Transaction rollbacks, restarts
DEADLOCK DETECTION & RECOVERY
DEADLOCK DETECTION
An algorithm that examines the state of the system
is invoked periodically to determine whether a deadlock
has occurred.
RECOVERY
If there is deadlock, then the system must attempt to
recover from the deadlock.
DEADLOCK DETECTION & RECOVERY
Deadlock Detection
• Use wait-for graph
• This graph consists of a pair G = (V, E), where V is a
set of vertices and E is a set of edges. The set of
vertices consists of all the transactions in the system.
• If transaction Ti is waiting for transaction Tj to release a
Deadlock Detection
wait-for graph
• The edge is removed only when transaction Tj is no
T1 T2
lock-x(Y)
lock-X(X)
W(Y)
W(X)
lock-x(X)
DEADLOCK DETECTION
No Cycle
No Deadlock
DEADLOCK DETECTION
Cycle (T18->T20->T19-T18)
Deadlock
DEADLOCK RECOVERY
1. Selection of a victim
• Select the transaction to rollback
• Select the transaction with minimum cost
Cost
a.How long the transaction has computed, and how
much longer the transaction will compute before it
completes its designated task?
b. How many data items the transaction has used?
DEADLOCK RECOVERY
1. Selection of a victim
Cost
c. How many more data items the transaction needs for
it to complete?
d. How many transactions will be involved in the
rollback?
DEADLOCK RECOVERY
2. Rollback
How far this transaction should be rolled back?
i. Total Rollback
Abort the transaction and then
restart it.
ii. Partial Rollback
Roll back the transaction only as far as
necessary to break the deadlock.
DEADLOCK RECOVERY
3. Starvation
• Must ensure that a transaction can be picked as a
victim only a (small) finite number of times.
• The most common solution is to include the number
of rollbacks in the cost factor.