0% found this document useful (0 votes)
23 views13 pages

Lect-Transactions-Week 11 (TEL)

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)
23 views13 pages

Lect-Transactions-Week 11 (TEL)

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/ 13

CE4031/CZ4031

DATABASE
SYSTEM PRINCIPLES
TRANSACTION MANAGEMENT
QUESTION

How are deadlocks detected and


prevented?
DEADLOCKS

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

You release the lock first. Why should I? You release


Once I have finished my the lock first and wait until
task, you can continue. I complete my task.
DEADLOCKS

Detection and Prevention

• Timeout
• Waits-for Graph
• Timestamps-based
DEADLOCK DETECTION BY TIMEOUT

The Idea

• At least one of the transactions need to be aborted


and restarted (roll back).
• Releases its locks or other resources.
• Put a limit on how long a transaction may be active,
and if a transaction exceeds this time, roll it back.
DEADLOCK DETECTION BY WAITS-FOR GRAPH

Structure

• A node for each transaction that currently holds any lock


or waiting for one.
• An edge from node T to U if there is some DB element A
s.t.:
• U holds a lock on A,
• T is waiting for a lock on A,
• T cannot get desired lock on A unless U first releases it.

Detection & Prevention

• 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

Limitations of Waits-for Graph

• Can be large.
• Analyzing it for cycles each time a transaction has to wait
for a lock can be time-consuming.

Solution Policies

• Associate with each T a timestamp. • Wait-Die


• Only for deadlock detection Scheme
• Timestamp for deadlock never
changes. • Wound-Wait
Scheme
WAIT-DIE SCHEME

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);

TS(T1) < TS(T2) < TS(T3) < u4(A); u4(D);


TS(T4) l2(A); l2(C);
r2(C); w2(A);
u2(A); u2(C);
WOUND-WAIT SCHEME

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);

a) If T is older than U l4(A); Waits


(TS(T) < TS(U)) then it
l1(B);w1(B); Wounded
“wounds” U.
b) If U is older than T, u1(A);u1(B);
then T waits for the l2(A); l2(C);
lock(s) held by U.
r2(C); w2(A);

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

• In both schemes, older • Wound-wait


transactions kill of newer • Rollback is rare.
transactions. • Rolled back
• No starvation transactions
• Guarantees every have done more
transaction eventually work by
completes. acquiring locks.
• Wait-die
Waits-for vs. TS • Roll backs more
• Waits-for graph minimizes transactions.
the number of times we must • Rolled back
abort a transaction due to transactions
deadlock. tend to do little
• TS-based schemes may roll work as they are
back even when there is no still in lock-
deadlock. gathering stage.
CONCLUSION

ONE-PASS ALGORITHMS
Deadlock Detection and Prevention
Time-out
Waits-for graph
Timestamps-based

You might also like