Title: CS542
1CS542
- Concurrency Control
- Aborts and Deadlocks
Professor Elke A. Rundensteiner
2Two More Topics
- Strict 2P Locking
- Aborts can cause Cascading Aborts
- Recoverable Schedules
- Handling Deadlocks
- Optimistic
- Pessimistic
32 Phase Locking Protocol
- locks
- held by
- Ti
- Time
- Growing Shrinking
- Phase Phase
4Concurrency control recovery
- Example Ti To
- Wi(A)
- rj(A)
- Commit To
- Abort Ti
- reads A even though Ti written A has not
committed. - Rollback ? Cascading rollback? (But committed!)
5- Note
- Schedule is conflict serializable
- But Schedule is not recoverable
6- Need to make final decision for each
transaction - commit decision - system guarantees transaction
will or has completed, no matter what ( takes
effect ) - abort decision - system guarantees transaction
will or has been rolled back - (has no effect)
7To model this, two new actions
- Ci - transaction Ti commits
- Ai - transaction Ti aborts
8Back to example Tj Ti Wj(A) ri(A
) Ci ? can we commit here?
...
...
...
...
9Back to example Tj Ti Wj(A) ri(A
) Ci ? No, cannot commit Dont
commit if you use (read) a variable written by
any other Tj that did not yet commit.
...
...
...
...
10Definition
- Ti reads from Tj in S (Tj ?S Ti) if
- wj(A) ltS ri(A)
- aj ltS ri(A) (lt does not precede)
- (3) If wj(A) ltS wk(A) ltS ri(A) then
- ak ltS ri(A)
11Definition
- Schedule S is recoverable if
- whenever Tj ?S Ti and j ? i and Ci ? S
- then Cj ltS Ci
- Idea Transactions only commit after all
transactions they read from have been committed
first. - (allows dirty reads for maximal concurrency)
12Back to example Tj Ti Wj(A) ri(A
) Cj Ci ? Yes.
...
...
...
...
13- Note in transactions, reads and writes
precede commit or abort - ? If Ci ? Ti, then ri(A) lt Ci
- wi(A) lt Ci
- ? If Ai ? Ti, then ri(A) lt Ai
- wi(A) lt Ai
-
14How to achieve recoverable schedules?
15Strict 2PL With 2PL, hold locks until commit
- Ti Tj
- Wi(A)
- Ci
- ui(A)
- rj(A)
...
...
...
...
...
...
...
16- S is recoverable if each transaction commits only
after all transactions from which it read have
committed. - S avoids cascading rollback if each transaction
may read only those values written by committed
transactions. - S is strict if each transaction may read and
write only items previously written by committed
transactions.
17Recoverable
Avoids cascading rollback
Strict
SERIAL
Avoid Cascading Aborts
Conflict Serializable
18Now what to do about deadlocks? (which
can arise for our 2PL and even
strict-2P protocol).
19Schedule with Deadlock
- T1 T2
- l1(A) Read(A) l2(B)Read(B)
- A A100Write(A) B Bx2Write(B)
- l1(B) l2(A)
delayed
delayed
20Deadlocks
- Detection
- Wait-for graph
- Prevention
- Resource ordering
- Timeout
- Wait-die
- Wound-wait
21Deadlock Detection
- Build Wait-For graph
- Use lock table structures
- Build incrementally or periodically
- When cycle found, rollback victim
T5
T2
T1
T7
T4
T6
T3
22Deadlock Prevention Resource Ordering
- Order all elements A1, A2, , An
- A transaction T can lock Ai after Aj only if i gt
j
Problem Ordered lock requests not realistic in
most cases
23Timeout
- If transaction waits more than L sec., roll
it back! - Simple scheme
- Hard to select L
24Wait-die
- Transactions are given a timestamp when they
arrive . ts(Ti) - Ti can only wait for Tj if ts(Ti)lt ts(Tj)
...else die
25Example
- T1
- (ts 10)
- T2
- (ts 20)
- T3
- (ts 25)
wait
wait
26Wound-wait
- Transactions given a timestamp when they arrive
ts(Ti) - Ti wounds Tj if ts(Ti)lt ts(Tj)
- else Ti waits
- Wound Tj rolls back and gives lock to Ti
27Example
- T1
- (ts 10)
- T2
- (ts 20)
- T3
- (ts 15)
Not wait (instead wound T2)
wait
28Summary
- Cascading rollbacks and
- Recoverable schedules
- Deadlocks
- Prevention
- Detection