Discussion 8
Transactions & Concurrency
Announcements
- Vitamin 8 (Xacts and Concurrency) is due Oct 28
- Vitamin 9 (Recovery) is due Nov 4
- MT2 review session
- MT2 is on Nov 7, 8-10pm
Agenda
Transactions
Transactions
Transactions
Transactions
Transactions
Concurrency
Serializability
Serializability
Serializability
Conflict Serializability
T1 | R(A) | R(B) | | | R(A) |
T2 | | | R(B) | W(B) | |
Conflict Serializability
stale read: T1 uses old value of B - if reversed order, output may differ
T1 | R(A) | R(B) | | | R(A) |
T2 | | | R(B) | W(B) | |
Conflict Serializability
Conflict Serializability
T1 | R(A) | R(B) | | | R(A) |
T2 | | | R(B) | W(B) | |
T1
T2
Conflict Serializability
T1
T2
T3
T4
T6
T5
View Serializability
View Serializability
T1 | R(A) | | W(A) | |
T2 | | W(A) | | |
T3 | | | | W(A) |
T1 | R(A) | W(A) | | |
T2 | | | W(A) | |
T3 | | | | W(A) |
Schedule 1
Schedule 2
View Serializability
T1 | R(A) | | W(A) | |
T2 | | W(A) | | |
T3 | | | | W(A) |
T1 | R(A) | W(A) | | |
T2 | | | W(A) | |
T3 | | | | W(A) |
Schedule 1
Schedule 2
View Serializability
T1 | R(A) | | W(A) | |
T2 | | W(A) | | |
T3 | | | | W(A) |
T1 | R(A) | W(A) | | |
T2 | | | W(A) | |
T3 | | | | W(A) |
Schedule 1
Schedule 2
View Serializability
T1 | R(A) | | W(A) | |
T2 | | W(A) | | |
T3 | | | | W(A) |
T1 | R(A) | W(A) | | |
T2 | | | W(A) | |
T3 | | | | W(A) |
Schedule 1
Schedule 2
Types of Serializability
All Schedules
Serial
View Serializable
Conflict Serializable
Serializable
Phantom Problem
Serializability Summary
Question 1
Conflict Serializability
Conflict Serializability
(a) Draw the dependency graph (precedence graph) for the schedule.
T1 | | R(A) | W(A) | R(B) | | | | | |
T2 | | | | | W(B) | R(C) | W(C) | W(A) | |
T3 | R(C) | | | | | | | | W(D) |
Conflict Serializability
(a) Draw the dependency graph (precedence graph) for the schedule.
T1 | | R(A) | W(A) | R(B) | | | | | |
T2 | | | | | W(B) | R(C) | W(C) | W(A) | |
T3 | R(C) | | | | | | | | W(D) |
Conflict Serializability
(b) Is this schedule conflict serializable? If so, what are all the conflict equivalent serial schedules? If not, why not?
T1 | | R(A) | W(A) | R(B) | | | | | |
T2 | | | | | W(B) | R(C) | W(C) | W(A) | |
T3 | R(C) | | | | | | | | W(D) |
Conflict Serializability
(b) Is this schedule conflict serializable? If so, what are all the conflict equivalent serial schedules? If not, why not?
Conflict equivalent to T3 , T1 , T2 and T1 , T3 , T2
(possible topological sorts of the dependency graph).
T1 | | R(A) | W(A) | R(B) | | | | | |
T2 | | | | | W(B) | R(C) | W(C) | W(A) | |
T3 | R(C) | | | | | | | | W(D) |
Conflict Serializability
(c) Draw the dependency graph (precedence graph) for the schedule.
T1 | R(A) | | R(B) | | | | W(A) | |
T2 | | R(A) | | R(B) | | | | W(B) |
T3 | | | | | R(A) | | | |
T4 | | | | | | R(B) | | |
Conflict Serializability
(c) Draw the dependency graph (precedence graph) for the schedule.
T1 | R(A) | | R(B) | | | | W(A) | |
T2 | | R(A) | | R(B) | | | | W(B) |
T3 | | | | | R(A) | | | |
T4 | | | | | | R(B) | | |
Conflict Serializability
(d) Is this schedule conflict serializable? If so, what are all the conflict equivalent serial schedules? If not, why not?
T1 | R(A) | | R(B) | | | | W(A) | |
T2 | | R(A) | | R(B) | | | | W(B) |
T3 | | | | | R(A) | | | |
T4 | | | | | | R(B) | | |
Conflict Serializability
(d) Is this schedule conflict serializable? If so, what are all the conflict equivalent serial schedules? If not, why not?
No, there’s a cycle between T1 and T2 in the dependency graph.
T1 | R(A) | | R(B) | | | | W(A) | |
T2 | | R(A) | | R(B) | | | | W(B) |
T3 | | | | | R(A) | | | |
T4 | | | | | | R(B) | | |
Locking
Simple Locking
Simple Locking
| NL (no lock held) | S | X |
NL (no lock held) | ✔ | ✔ | ✔ |
S | ✔ | ✔ | ❌ |
X | ✔ | ❌ | ❌ |
Deadlocks
Deadlock Avoidance
Deadlock Avoidance
Deadlock Detection
Tj
Ti
Question 2
Deadlocks
Deadlocks
(a) Draw a “waits-for” graph and state whether or not there is a deadlock.
T1 | S(A) | S(D) | | S(B) | | | | | |
T2 | | | X(B) | | | | X(C) | | |
T3 | | | | | S(D) | S(C) | | | X(A) |
T4 | | | | | | | | X(B) | |
Deadlocks
(a) Draw a “waits-for” graph and state whether or not there is a deadlock.
Deadlock between T1 , T2 , T3 .
T1 | S(A) | S(D) | | S(B) | | | | | |
T2 | | | X(B) | | | | X(C) | | |
T3 | | | | | S(D) | S(C) | | | X(A) |
T4 | | | | | | | | X(B) | |
Deadlocks
(b) If we try to avoid deadlock by using wait-die deadlock avoidance policy, would any transactions be aborted? Assume T1 priority > T2 > T3 > T4.
T1 | S(A) | S(D) | | S(B) | | | | | |
T2 | | | X(B) | | | | X(C) | | |
T3 | | | | | S(D) | S(C) | | | X(A) |
T4 | | | | | | | | X(B) | |
Deadlocks
(b) If we try to avoid deadlock by using wait-die deadlock avoidance policy, would any transactions be aborted? Assume T1 priority > T2 > T3 > T4.�Yes, T3 and T4 are aborted. When T4 attempts to acquire a lock on B, which is held by T2, T4 will abort since it is attempting to acquire a lock held by a transaction with higher priority. The same thing happens when T3 attempts to acquire a lock on A, which is held by T1.
T1 | S(A) | S(D) | | S(B) | | | | | |
T2 | | | X(B) | | | | X(C) | | |
T3 | | | | | S(D) | S(C) | | | X(A) |
T4 | | | | | | | | X(B) | |
Locking (2PL and Strict 2PL)
2-Phase Locking (2PL)
time
# locks held
release phase
acquisition phase
Cascading Aborts
T1 | +X(A) | -X(A) | | | | | | abort! |
T2 | | | +S(A) | | | | | |
T3 | | | +S(A) | +X(B) | -X(B) | | | |
T4 | | | | | | +S(B) | | |
T5 | | | | | | | +S(B) | |
Strict 2-Phase Locking (Strict 2PL)
# locks held
time
release all locks at end of xact
Strict 2PL and Cascading Aborts
T1 | +X(A) | -X(A) | | | | | | abort! |
T2 | | | +S(A) | | | | | |
T3 | | | +S(A) | +X(B) | -X(B) | | | |
T4 | | | | | | +S(B) | | |
T5 | | | | | | | +S(B) | |
Question 3
Locking
Locking
(a) What is printed, assuming we initially have B = 3 and F = 300?
T1 | T2 |
Lock X(B) | |
Read B | |
B := B * 10 | |
Write B | |
Lock X(F) | |
Unlock B | |
| Lock S(F) |
F := B * 100 | |
Write F | |
Commit | |
Unlock F | |
| Read F |
| Unlock F |
| Lock S(B) |
| Read B |
| Print F+B |
| Commit |
| Unlock B |
Locking
(a) What is printed, assuming we initially have B = 3 and F = 300?
3030
T1: Lock X(B) → Read B → B = 3*10 = 30 → Write B → Lock X(F) → Unlock B
T2: Lock S(F) (waiting for T1 to release X Lock on F)
T1: F = 30*100 = 3000 → Write F → Commit → Unlock F (S Lock on F is granted to T2)
T2: Read F → Unlock F → Lock S(B) → Read B → Compute and print F + B = 3000 + 30 = 3030 → Commit → Unlock B
T1 | T2 |
Lock X(B) | |
Read B | |
B := B * 10 | |
Write B | |
Lock X(F) | |
Unlock B | |
| Lock S(F) |
F := B * 100 | |
Write F | |
Commit | |
Unlock F | |
| Read F |
| Unlock F |
| Lock S(B) |
| Read B |
| Print F+B |
| Commit |
| Unlock B |
Locking
(b) Does the execution use 2PL, strict 2PL, or neither?
T1 | T2 |
Lock X(B) | |
Read B | |
B := B * 10 | |
Write B | |
Lock X(F) | |
Unlock B | |
| Lock S(F) |
F := B * 100 | |
Write F | |
Commit | |
Unlock F | |
| Read F |
| Unlock F |
| Lock S(B) |
| Read B |
| Print F+B |
| Commit |
| Unlock B |
Locking
(b) Does the execution use 2PL, strict 2PL, or neither?
Neither - S(F) unlocked before T2 acquires S(B)
T1 | T2 |
Lock X(B) | |
Read B | |
B := B * 10 | |
Write B | |
Lock X(F) | |
Unlock B | |
| Lock S(F) |
F := B * 100 | |
Write F | |
Commit | |
Unlock F | |
| Read F |
| Unlock F |
| Lock S(B) |
| Read B |
| Print F+B |
| Commit |
| Unlock B |
Locking
(c) Would moving Unlock F in T2 to any point after Lock S(B) change this (or keep it) in 2PL?
T1 | T2 |
Lock X(B) | |
Read B | |
B := B * 10 | |
Write B | |
Lock X(F) | |
Unlock B | |
| Lock S(F) |
F := B * 100 | |
Write F | |
Commit | |
Unlock F | |
| Read F |
| Unlock F |
| Lock S(B) |
| Read B |
| Print F+B |
| Commit |
| Unlock B |
Locking
(c) Would moving Unlock F in T2 to any point after Lock S(B) change this (or keep it) in 2PL?
Yes - all locks would be acquired (for T2) before any are released.
T1 | T2 |
Lock X(B) | |
Read B | |
B := B * 10 | |
Write B | |
Lock X(F) | |
Unlock B | |
| Lock S(F) |
F := B * 100 | |
Write F | |
Commit | |
Unlock F | |
| Read F |
| Unlock F |
| Lock S(B) |
| Read B |
| Print F+B |
| Commit |
| Unlock B |
Locking
(d) Would moving Unlock F in T1 and Unlock F in T2 to the end of their respective transactions change this (or keep it) in strict 2PL?
T1 | T2 |
Lock X(B) | |
Read B | |
B := B * 10 | |
Write B | |
Lock X(F) | |
Unlock B | |
| Lock S(F) |
F := B * 100 | |
Write F | |
Commit | |
Unlock F | |
| Read F |
| Unlock F |
| Lock S(B) |
| Read B |
| Print F+B |
| Commit |
| Unlock B |
Locking
(d) Would moving Unlock F in T1 and Unlock F in T2 to the end of their respective transactions change this (or keep it) in strict 2PL?
No - T1 still unlocks B before the end of the transaction
T1 | T2 |
Lock X(B) | |
Read B | |
B := B * 10 | |
Write B | |
Lock X(F) | |
Unlock B | |
| Lock S(F) |
F := B * 100 | |
Write F | |
Commit | |
Unlock F | |
| Read F |
| Unlock F |
| Lock S(B) |
| Read B |
| Print F+B |
| Commit |
| Unlock B |
Locking
(e) Would moving Unlock B in T1 and Unlock F in T2 to the end of their respective transactions change this (or keep it) in strict 2PL?
T1 | T2 |
Lock X(B) | |
Read B | |
B := B * 10 | |
Write B | |
Lock X(F) | |
Unlock B | |
| Lock S(F) |
F := B * 100 | |
Write F | |
Commit | |
Unlock F | |
| Read F |
| Unlock F |
| Lock S(B) |
| Read B |
| Print F+B |
| Commit |
| Unlock B |
Locking
(e) Would moving Unlock B in T1 and Unlock F in T2 to the end of their respective transactions change this (or keep it) in strict 2PL?
Yes - all unlocks would only happen when the respective transactions end
T1 | T2 |
Lock X(B) | |
Read B | |
B := B * 10 | |
Write B | |
Lock X(F) | |
Unlock B | |
| Lock S(F) |
F := B * 100 | |
Write F | |
Commit | |
Unlock F | |
| Read F |
| Unlock F |
| Lock S(B) |
| Read B |
| Print F+B |
| Commit |
| Unlock B |
Multi-granularity Locking
Multi-granularity Locking
Multi-granularity Locking
Multi-granularity Locking
Multi-granularity Locking
Multi-granularity Locking
Multi-granularity Locking
Multi-granularity Locking
Multi-granularity Locking
Parent Lock | Possible Children Locks |
NL | Nothing |
S | Nothing |
X | Nothing |
IS | IS, S |
IX | IX, X, IS, S |
SIX | IX, X |
Question 4
Multi-granularity Locking
Multi-granularity Locking
(a) Suppose a transaction T1 wants to scan a table R and update a few of its tuples. What kinds of locks should T1 have on R, the pages of R, and the updated tuples?
Multi-granularity Locking
(a) Suppose a transaction T1 wants to scan a table R and update a few of its tuples. What kinds of locks should T1 have on R, the pages of R, and the updated tuples?
SIX on R
IX on page (no need for SIX since already have S on R)
X on tuples being modified
Multi-granularity Locking
(b) Is an S lock compatible with an IX lock?
Multi-granularity Locking
(b) Is an S lock compatible with an IX lock?
Suppose T1 wants S(A) and T2 wants IX(A).
S(A) implies T1 will read all of A.
IX(A) implies T2 will write to some of A.
We can’t have T1 read all of A while T2 is writing to some of it, so S and IX are incompatible.
Multi-granularity Locking
(c) Consider a table which contains two pages with three tuples each, with Page 1 containing Tuples 1, 2, and 3, and Page 2 containing Tuples 4, 5, and 6.
Given that a transaction T1 has IX(table), IX(Page 1), X(Tuple 1), which locks can be granted to a second transaction T2 on Tuple 2?
Page 2
Lock:
Table
Lock: IX
Page 1
Lock: IX
Tuple 2
Lock: ?
Tuple 1
Lock: X
Tuple 3
Lock:
Tuple 5
Lock:
Tuple 4
Lock:
Tuple 6
Lock:
T1: Red
T2: Blue
Multi-granularity Locking
(c) Consider a table which contains two pages with three tuples each, with Page 1 containing Tuples 1, 2, and 3, and Page 2 containing Tuples 4, 5, and 6.
Given that a transaction T1 has IX(table), IX(Page 1), X(Tuple 1), which locks can be granted to a second transaction T2 on Tuple 2?
X, S
Multi-granularity Locking
(c) Consider a table which contains two pages with three tuples each, with Page 1 containing Tuples 1, 2, and 3, and Page 2 containing Tuples 4, 5, and 6.
Given that a transaction T1 has IS(table), S(Page 1), which locks can be granted to a second transaction T2 on Page 1?
Page 2
Lock:
Table
Lock: IS
Page 1
Lock: S, ?
Tuple 2
Lock:
Tuple 1
Lock:
Tuple 3
Lock:
Tuple 5
Lock:
Tuple 4
Lock:
Tuple 6
Lock:
T1: Red
T2: Blue
Multi-granularity Locking
(c) Consider a table which contains two pages with three tuples each, with Page 1 containing Tuples 1, 2, and 3, and Page 2 containing Tuples 4, 5, and 6.
Given that a transaction T1 has IS(table), S(Page 1), which locks can be granted to a second transaction T2 on Page 1?
S, IS
Attendance Link