School of Computer Science, McGill University COMP-421B Database Systems, Winter 2008 Written Assignment 4: Transactions & Indexing Sample Solution Exercise 1: a) T1: R(X), T2: R(X), T3: R(Y), T1: W(X), T2: R(Z), T2: R(Y), T2: W(Y), T1: W(Z) i) ii) T3 -----> T2 -----> T1 (No Cycle) The schedule is thus conflict-serializable. Equivalent to a serial schedule: T3, T2, T1 No protocol is followed for all locking and unlocking. iii) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 T1 Lock-S(X) Read(X) T2 T3 Lock-S(X) Read(X) Unlock(X) Lock-S(Y) Read(Y) Unlock(Y) Upgrade(X) Write(X) Unlock(X) Lock-S(Z) Read(Z) Unlock(Z) Lock-X(Y) Read(Y) Write(Y) Unlock(Y) Lock-X(Z) Write(Z) Unlock(Z) b) T1: R(X), T1: W(Y), T2: R(Y), T2: W(Z), T3: R(Z), T3: W(X) i) T1 -----> T2 -----> T3 (No Cycle) ii) The schedule is thus conflict-serializable. Equivalent to a serial schedule: T1, T2, T3 iii) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 T1 Lock-S(X) Read(X) Unlock(X) Lock-X(Y) Write(Y) Unlock(Y) T2 T3 Lock-S(Y) Read(Y) Unlock(Y) Lock-X(Z) Write(Z) Unlock(Z) Lock-S(Z) Read(Z) Unlock(Z) Lock-X(X) Write(X) Unlock(X) c) T3: W(X), T1: R(X), T1: W(Y), T2: R(Y), T2: W(Z), T3: R(Z) i) T3 -----> T1 -----> T2 -----> T3 (Cycle) ii) The schedule is not conflict-serializable. iii) T1 T2 T3 1 Lock-X(X) 2 Write(X) 3 Unlock(X) 4 Lock-S(X) 5 Read(X) 6 Unlock(X) 7 Lock-X(Y) 8 Write(Y) 9 Unlock(Y) 10 Lock-S(Y) 11 Read(Y) 12 Unlock(Y) 13 Lock-X(Z) 14 Write(Z) 15 Unlock(Z) 16 Lock-S(Z) 17 Read(Z) 18 Unlock(Z) d) T1: R(X), T2: R(X), T1: W(Y), T2: W(Y), T1: R(Y), T2: R(Y), T2: W(Z), T1: W(S) i) T1 -----> T2 -----> T1 (Cycle) ii) The schedule is not conflict-serializable. iii) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 T1 Lock-S(X) Read(X) Unlock(X) T2 Lock-S(X) Read(X) Unlock(X) Lock-X(Y) Write(Y) Unlock(Y) Lock-X(Y) Write(Y) Unlock(Y) Lock-S(Y) Read(Y) Unlock(Y) Lock-S(Y) Read(Y) Unlock(Y) Lock-X(Z) Write(Z) Unlock(Z) Lock-X(S) Write(S) Unlock(S) e) T1: R(X), T2: R(X), T1: R(Y), T2: R(Y), T3: R(X), T4: R(Y), T1: W(X), T2: W(Y) i) T3 -----> T1 -----> T2 -----> T1 T4 -----> T2 -----> T1 (Cycle) ii) The schedule is not conflict-serializable. iii) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 T1 Lock-S(X) Read(X) Unlock(X) T2 T3 T4 Lock-S(X) Read(X) Unlock(X) Lock-S(Y) Read(Y) Unlock(Y) Lock-S(Y) Read(Y) Unlock(Y) Lock-S(X) Read(X) Unlock(X) Lock-S(Y) Read(Y) Unlock(Y) Lock-X(X) Write(X) Unlock(X) Lock-X(Y) Write(Y) Unlock(Y) Exercise 2: Consider the following classes of schedules: serializable, recoverable, avoid cascading aborts, and strict. For each of the following schedules, state which of the above classes it belongs to. If you cannot decide whether a schedule belongs in a certain class based on the listed actions, explain why not. a) T1: R(X), T2: R(X), T1: W(X), T2: W(X) Not serializable, It is recoverable and avoid cascading aborts; not strict. b) T1: R(X), T2: W(X), T1: W(X), T2: Abort, T1: Commit It is serializable, It is recoverable and avoid cascading aborts; not strict. c) T2: R(X), T3: W(X), T3: Commit, T1: W(Y), T1: Commit, T2: R(Y), T2: W(Z), T2: Commit It is serializable, It is recoverable, avoid cascading aborts and strict. d) T1: R(X), T2: W(X), T2: Commit, T1: W(X), T1: Commit, T3: R(X), T3: Commit It is not serializable, It is recoverable, avoid cascading aborts and strict. e) T1: R(X), T2: W(X), T1: W(X), T3: R(X), T1: Commit, T2: Commit, T3: Commit It is not serializable, It is recoverable, but not avoid cascading aborts, not strict. Exercise 3: Consider the following schedule. Initial values for data items A and B are respectively 10 and 20. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 T1 Start Read(A) A=A+1 Write(A) T2 T3 Start Read(A) Start Write(A) A=A+1 Write(A) Read(B) B=B+1 Write(B) Commit Read(B) B=B+1 Write(B) Commit Read(B) B=B+1 Write(B) Commit a) Now assume that the system crashes after step 20, what recovery action do you suggest when the system is brought back up? Answer: Scanning the log will find T1 start and commit, T2 start and commit, T3 start but no commit. So, we undo T3 scanning the log backward, then Redo T1 and T2 scanning the log forward. b) Is the schedule possible under two-phase locking protocol? (explain why/why not) Answer: No, it is not possible under tree locking protocol because both T2 and T3 conflict on node A. At step 8, T3 need an exclusive lock on A which is not released by T2 because T2 still needs to write A in step 10. c) Is the schedule possible under tree locking protocol? (explain why/why not) Answer: No, it is not possible under tree locking protocol because both T2 and T3 conflict on node A. At step 8, T3 needs an exclusive lock on A which is not released by T2 because T2 still needs to write A in step 10. Exercise 4: a) Construct a B+-tree for the following set of key values: (2, 3, 5, 7, 11, 17, 19, 23, 29, 31) assuming that the tree is initially empty, values are added in ascending order, and the number of pointers will then fit in one node is 4. Answer: 19 5 2 3 29 11 5 7 11 17 19 23 b) For the B+-tree you constructed in (a), show the steps involved to find records with a search-key value of 11. Answer: a. Search the first level index; follow the first pointer. b. Search the second level; follow the third pointer. c. Search index level; follow first pointer to desired records. 29 31 c) For the B+-tree you constructed in (a), show the steps involved to find records with a search-key value between 7 and 17, inclusive. Answer: 1. 2. 3. 4. 5. 6. Search top index; take first pointer. Search next level; take second pointer. Search third level; follow second pointer to data. Take fourth pointer to new block. Take first pointer to data. Take second pointer to data.