Docsity

Transaction Processing for E-Commerce Assignment 3: Two-Phase Locking and Deadlocks, Exercises of Transaction Processing for E Commerce

Toggle sidebar
bg1
CSEP 545 Transaction Processing for E-Commerce 1/25/2012
Assignment 3 1
Assignment 3 - Solution
Problem 1
H1: r1[y] r1[x] r2[x] w1[y] c1 w2[y] c2
H1 is normally-strict two-phase locked:
rl1[y] r1[y] rl1[x] r1[x] rl2[x] r2[x] wl1[y] w1[y] c1 ru1[x] wu1[y] wl2[y] w2[y] c2 ru2[x]
wu2[y]
Note that ru1[y] isn’t needed, since ru1[y] was converted into wu1[y], i.e., T1 holds only
one lock on y.
H2: r1[y] r1[x] r2[x] w2[x] w1[y] c1 w2[y] c2
H2 is two-phase locked, but not strict two-phase locked. To run w2[x], T1 must have
released its read lock on x before w2[x], which means it cannot be strict two-phase
locked. Moreover, to be two-phase locked, it must have gotten its write lock on y before
it released its read lock on x. Thus, we have the following:
rl1[y] r1[y] rl1[x] r1[x] rl2[x] r2[x] wl1[y] ru1[x] wl2[x] w2[x] w1[y] c1 wu1[y] wl2[y] w2[y]
c2 ru2[x] wu2[y]
H3: r1[y] r1[x] r2[x] w1[y] w2[y] c2 c1
H3 is two-phase locked, but not strict two-phase locked because T1 must have released its
write lock before w2[y] executed.
rl1[y] r1[y] rl1[x] r1[x] rl2[x] r2[x] wl1[y] w1[y] wu1[y] wl2[y] w2[y] c2 ru2[x] wu2[y] c1
ru1[x]
H4: r1[y] r1[x] r2[x] w2[x] r3[y] w1[y] c1 w3[z] c3 w2[y] c2
H4 is not two-phase locked. To see why, consider the following prefix of the history:
rl1[y] r1[y] rl1[x] r1[x] rl2[x] r2[x]
The next operation is w2[x]. So as in H2, T1 must have released its read lock on x before
w2[x], so again the next few operations must have been wl1[y] ru1[x] wl2[x] w2[x], as in
the following expanded prefix.
docsity.com
pf3

Partial preview of the text

Download Transaction Processing for E-Commerce Assignment 3: Two-Phase Locking and Deadlocks and more Exercises Transaction Processing for E Commerce in PDF only on Docsity!

Assignment 3 1

Assignment 3 - Solution

Problem 1

H 1 : r 1 [y] r 1 [x] r 2 [x] w 1 [y] c 1 w 2 [y] c 2

H 1 is normally-strict two-phase locked:

rl 1 [y] r 1 [y] rl 1 [x] r 1 [x] rl 2 [x] r 2 [x] wl 1 [y] w 1 [y] c 1 ru 1 [x] wu 1 [y] wl 2 [y] w 2 [y] c 2 ru 2 [x] wu 2 [y]

Note that ru 1 [y] isn’t needed, since ru 1 [y] was converted into wu 1 [y], i.e., T 1 holds only one lock on y.

H 2 : r 1 [y] r 1 [x] r 2 [x] w 2 [x] w 1 [y] c 1 w 2 [y] c 2

H 2 is two-phase locked, but not strict two-phase locked. To run w 2 [x] , T 1 must have released its read lock on x before w 2 [x], which means it cannot be strict two-phase locked. Moreover, to be two-phase locked, it must have gotten its write lock on y before it released its read lock on x. Thus, we have the following:

rl 1 [y] r 1 [y] rl 1 [x] r 1 [x] rl 2 [x] r 2 [x] wl 1 [y] ru 1 [x] wl 2 [x ] w 2 [x] w 1 [y] c 1 wu 1 [y] wl 2 [y] w 2 [y] c 2 ru 2 [x] wu 2 [y]

H 3 : r 1 [y] r 1 [x] r 2 [x] w 1 [y] w 2 [y] c 2 c 1

H 3 is two-phase locked, but not strict two-phase locked because T 1 must have released its write lock before w 2 [y] executed.

rl 1 [y] r 1 [y] rl 1 [x] r 1 [x] rl 2 [x] r 2 [x] wl 1 [y] w 1 [y] wu 1 [y] wl 2 [y] w 2 [y] c 2 ru 2 [x] wu 2 [y] c 1 ru 1 [x]

H 4 : r 1 [y] r 1 [x] r 2 [x] w 2 [x] r 3 [y] w 1 [y] c 1 w 3 [z] c 3 w 2 [y] c 2

H 4 is not two-phase locked. To see why, consider the following prefix of the history:

rl 1 [y] r 1 [y] rl 1 [x] r 1 [x] rl 2 [x] r 2 [x]

The next operation is w 2 [x]. So as in H 2 , T 1 must have released its read lock on x before w 2 [x], so again the next few operations must have been wl 1 [y] ru 1 [x] wl 2 [x] w 2 [x], as in the following expanded prefix.

Assignment 3 2

rl 1 [y] r 1 [y] rl 1 [x] r 1 [x] rl 2 [x] r 2 [x] wl 1 [y] ru 1 [x] wl 2 [x] w 2 [x]

The next operation is r 3 [y]. To have executed here, T 3 would have to obtain its lock on y, which requires that T 1 had already released its lock on y, which it could not have done at this point because it hasn’t yet executed w 1 [y].

Nevertheless, this history is SR. We have only the following SG edges: T 1  T 2 because (r 1 [x],w 2 [x]) and (w 1 [y], w 2 [y]) T 3  T 1 because (r 3 [y], w 1 [y]) There’s no cycle in the SG, so the history is serializable as T 3 T 1 T 2. Note that there are no transaction handshakes in the input, so there are none to preserve.

Extra credit: Is it possible for a history to be strict two-phase locked but not normally- strict two phase locked? No. To prove it, let H be a strict 2PL history that has been augmented with lock and unlock operations to demonstrate that it’s strict 2PL. We can transform H into a history each of whose lock operations immediately precedes the operation it’s synchronizing, as follows.  Suppose that for some operation oi[x] in H, the corresponding lock request oli[x] does not immediately precede oi[x].  The only constraint that prevents moving oli[x] to the right in H so that it immediately precedes oi[x] is an unlock operation by Ti, since that would break 2PL.  However, since H is strict 2PL, all of Ti’s unlock operations follow ci.  Therefore, it’s possible to move oli[x] to the right in H so that it immediately precedes oi[x].  This can be done for all offending lock operations in H, thereby transforming it into a demonstration that H is normally-strict 2PL-ed.

Problem 2

Consider the following three transactions, each of which is a sequential program: T 1 : r 1 [x] r 1 [y] T 2 : r 2 [x] r 2 [y] T 3 : w 3 [y] w 3 [x] Suppose they start executing as follows: H 1 : r 1 [x] r 2 [x] w 3 [y] So far, T 1 and T 2 each have a read lock on x, and T 3 has a write lock on y. Next, each transaction tries to set a lock for its second operation: r 1 [y], r 2 [y], and w 3 [x]. However, no matter which order the three lock requests are made, none of those lock requests can be granted, because another transaction already owns a conflicting lock. In terms of the waits-for graph, we have: T 1  T 3 because T 1 requests a read lock on y and T 3 owns a write lock on y T 2  T 3 for the same reason as above T 3  T 1 because T 3 requests a write lock on x and T 1 owns a read lock on x T 3  T 2 for the same reason as above.