Lect-Transactions-Week 11 (TEL)
Lect-Transactions-Week 11 (TEL)
DATABASE
SYSTEM PRINCIPLES
TRANSACTION MANAGEMENT
QUESTION
Deadlock
• Each set of transactions is waiting for a resource
(e.g., lock) currently held by another transaction in
the set.
• None can make progress.
DEADLOCKS
• Timeout
• Waits-for Graph
• Timestamps-based
DEADLOCK DETECTION BY TIMEOUT
The Idea
Structure
• Detection of deadlock
• Occurrence of a cycle.
• Prevention:
• Refuse to allow an action that creates a cycle.
• Roll back any transaction that may cause a cycle.
EXAMPLE
T1 T2 T3 T4 T1 T2
l1(A);r1(A);
l1(A); l2(C);
l2(C);r2(C); r1(A); r2(C);
l1(B); l2(A);
l3(B);r3(B);
w1(B); w2(A);
l4(D);r4(D); u1(A); u2(C);
u1(B); u2(A);
l2(A); Denied
l3(C); Denied T3 T4
l4(A); Denied
l3(B); l4(D);
l1(B); Denied r3(B); r4(D);
l3(C); l4(A);
T -> U: (a) U holds a w3(C); w4(A);
lock on A, (b) T is
T4 u3(B); u4(D);
waiting for a lock u3(C); u4(A);
on A, (c) T cannot
get desired lock on
A unless U first T3 T2 T1
releases it.
DEADLOCK DETECTION BY TIMESTAMPS
• Can be large.
• Analyzing it for cycles each time a transaction has to wait
for a lock can be time-consuming.
Solution Policies
Wait-Die Scheme T1 T2 T3 T4
l1(A);r1(A);
A transaction T is
l2(A); Dies
waiting for a lock
that is held by l3(B);r3(B);
transaction U l4(A); Dies
a) If T is older
l3(C); w3(C);
than U (TS(T) <
TS(U)) then T is u3(B); u3(C);
allowed to wait. l1(B);w1(B);
b) If U is older
u1(A);u1(B);
than T, then T
“dies”; it is l4(A); l4(D);
rolled back. l2(A); Waits
r4(D); w4(A);
Wound-Wait T1 T2 T3 T4
If by the time the wound
l1(A);r1(A);
takes effect, U has already
A transaction T is waiting l2(A); Waits finished and released the
for a lock that is held by locks => no roll back.
transaction U. l3(B);r3(B);
u2(A); u2(C);
l4(A); l4(D);
Wound Is fatal
r4(D); w4(A);
U must roll back and u4(A); u4(D);
relinquish to T the lock(s)
that T needs from U. l3(B);r3(B);
l3(C); w3(C);
u3(B); u3(C);
COMPARISON
Commonality Differences
ONE-PASS ALGORITHMS
Deadlock Detection and Prevention
Time-out
Waits-for graph
Timestamps-based