Você está na página 1de 15

Data management

Part 2
Exercises on transaction
management
Maurizio Lenzerini, Riccardo Rosati
Corso di laurea magistrale in Ingegneria Gestionale
Sapienza Università di Roma
Anno Accademico 2013/2014

http://www.dis.uniroma1.it/~rosati/gd/
Exercise 1

For each of the following schedules:

S1 : w0(x), r2(x), r1(x), w2(x), w2(z)


S2 : w0(x), r1(x), r2(x), w2(x), w2(z)
S3 : r1(x), r2(x), w2(x), w1(x)
S4 : r1(x), r2(x), w2(x), r1(x)

establish whether the schedule is view-serializable. For each of the


non-view-serializable schedules, explain which kind of anomaly it
presents.

Data management 2
Exercise 1 – solution (1)

• S2 is a serial schedule, so it is trivially view-serializable (of


course it is view-equivalent to itself).

• S1 is view-serializable since it is view-equivalent to S2 (which is


a serial schedule). In fact, S1 ed S2 have:
– the same FINAL-WRITE relation (w2(x) and w2(z));
– the same READS-FROM relation: {(r2(x), w0(x)),(r1(x),
w0(x))}.

Data management 3
Exercise 1 – solution (2)

We show that S3 is not view-serializable. In fact, suppose S3 is


view-serializable. Then, a serial schedule S’ exists that is view-
equivalent to S3. Since w1(x) is a final write for S3, T2 must be the
first transaction in S’, therefore S’ must be the following schedule:
S’: r2(x), w2(x), r1(x), w1(x)
But in this case, READS-FROM(S’) contains the pair (r1(x), w2(x)),
which is not in READS-FROM(S3). This contradicts the hypothesis
that S3 and S’ are view-equivalent. Therefore, there exists no serial
schedule that is view-equivalent to S3, which implies that S3 is not
view-serializable.

Data management 4
Exercise 1 – solution (3)

S3 presents an update loss anomaly, because T1 and T2 read the


same initial value for x and, after T2 writes the new value for x,
such a value is overwritten by T1, so the update of x done by T2 is
lost.

Data management 5
Exercise 1 – solution (4)
S4 is not view-serializable. If S4 were view-serializable, one of the
following serial schedules should be view-equivalent to S4 (where we
denote by r’1(x) the second read of x done by T1):
S’: r2(x), w2(x), r1(x), r’1(x)
→ READS-FROM(S’)={(r1(x), w2(x)), (r’1(x), w2(x))}

S’’: r1(x), r’1(x), r2(x), w2(x)


→ READS-FROM(S’’) ={}

Now, since:
READS-FROM(S4)={(r’1(x), w2(x))}
it follows that there exists no serial schedule which is view-equivalent to
S4!
S4 presents the unrepeatable read anomaly, because T1 reads two
different values for x (without making any update on x in the meantime).

Data management 6
Exercise 2

Given the following schedule S:

r1(x), w2(x), r3(x), r1(y), r4(z), w2(y), r1(v), w3(v), r4(v), w4(y), w5(y), w5(z)

Establish whether S is:


• View-serializable
• Conflict-serializable
• Executable under the 2PL protocol
Justify each answer.

Data management 7
Exercise 3

Given the following schedule S:

w3(Z) w1(Y) c3 r2(Y) w2(Z) w1(X) c1 c2

1. Establish (justifying your answer) whether S can be executed under


the 2PL protocol (using both shared and exclusive locks). If the
answer to the above question is positive, show the 2PL schedule
obtained by inserting the proper lock/unlock statements.
2. Establish whether S belongs to the class of strict 2PL schedules.
3. Establish whether S belongs to the class of ACR (Avoid Cascading
Rollback) schedules.

Data management 8
Exercise 4
Illustrate the actions of the universion timestamp-based scheduler
for the following input sequence:

r1(A), r2(B), r3(A), r2(A), w1(A), w3(A)

Then, establish whether the sequence of executed actions actually


executed by the scheduler is a schedule in the class of strict 2PL
schedules.

Data management 9
Exercise 4 – solution (1)
• Before the scheduler receives its input, the timestamps are as
follows:
wts(A) = wts(B) = rts(A) = rts(B) = 0, cb(A)=cb(B)=true
• Then, the scheduler reacts as follows (requests → answers →
updates):
– r1(A) → ok → ts(T1)=1, rts(A)=1, since ts(T1)>= wts(A) and
cb(A)=true
– r2(B) → ok → ts(T2)=2, rts(B)=2, since ts(T2)>= wts(B) and
cb(B)=true
– r3(A) → ok → ts(T3)=3, rts(A)=3, since ts(T3)>= wts(A) and
cb(A)=true
– r2(A) → ok → rts(A)=3, since ts(T2)>= wts(A), cb(A)=true
and max(ts(T2),rts(A))=3
– w1(A) → no: T1 is aborted, because ts(T1)<rts(A)=3
– w3(A) → ok → wts(A)=3 e cb(A)=false, because ts(T3)=
rts(A) and ts(T3)>=wts(A)
Data management 10
Exercise 4 – solution (2)

Since T1 is aborted, the sequence of actually executed actions is the


following:
r2(B), r3(A), r2(A), w3(A)

Is this a strict 2PL schedule? To answer this question, we have to


establish the position of the commit statements. We assume that the
commit statement of every transaction immediately follows the last
statement of the transaction in the schedule. We can conclude that the
schedule thus obtained is a 2PL strict schedule, in fact:

sl2(A), sl2(B), r2(B), sl3(A), r3(A), r2(A), c2, u2(A), xl3(A), w3(A), c3, u3(A), u3(B)

Data management 11
Exercise 5

Illustrate the actions of the universion timestamp-based scheduler


for the following input sequence:

r1(B), w1(A), w2(B), w1(B), r2(A)

Data management 12
Exercise 5 - solution
Before the scheduler receives its input, the timestamps are as follows:
wts(A) = wts(B) = rts(A) = rts(B) = 0
Then, the scheduler reacts as follows (requests → answers → updates):
– r1(B) → ok → ts(T1)=1, rts(B)=1, since ts(T1)>= wts(B) and
rts(B)=max(ts(T1),rts(B))
– w1(A) → ok → wts(A)=1 and cb(A)=false, since ts(T1)>= wts(A) and
ts(T1)>= rts(A)
– w2(B) → ok → ts(T2)=3, wts(B)=3 and cb(B)=false, since ts(T2)>=
wts(B) and ts(T2)>= rts(B)
– w1(B) → T1 waits for cb(B)=true (i.e., T1 waits for commit or rollback of
T2), since ts(T1) = rts(B) and ts(T1)<wts(B)
– r2(A) → T2 waits for cb(A)=true (i.e., T2 waits for commit or rollback of
T1)

DEADLOCK!

Data management 13
Exercise 6

Illustrate the actions of the multiversion timestamp-based scheduler


for the following input sequence:

r1(x), r2(y), w2(x), w1(y)

Data management 14
Exercise 7
Given the schedule
S = r1(A), r2(B), r3(B), w4(A), r2(A), w5(A), r4(A), r3(A), r5(A),
show the differences between the execution of S by the universion-
based timestamp scheduler and the execution of S by the
multiversion-based timestamp scheduler.

Data management 15

Você também pode gostar