CHAPTER III - ppt7978863
CHAPTER III - ppt7978863
CONCURRENCY CONTROL
Concurrency Control
The management of simultaneous
execution of many different application
programs such that each transaction
does not interfere with another
transaction.
The concurrent execution of transactions
must be such that each transaction
appears to execute in isolation.
Recall ACID properties!!!
Conditions Necessary for Conflict
write_item(X);
read_item(Y);
T1: T3:
sum:= 0;
read_item(A);
sum:= sum +
A;
.
read_item(X); . T3 reads X after N
X:= X - N; . is subtracted and
write_item(X);
read_item(X); reads Y before N is
sum:= sum + added, so a wrong
X; summary is the
read_item(Y);
sum:= sum + result
Y;
read_item(Y);
Y:= Y + N;
write_item(Y);
Isolation
Repeatable read
Serializable
READ
UNCOMMITT Permitted Permitted Permitted
ED
READ
No Permitted Permitted
COMMITTED
REPEATABLE
No No Permitted
READ
SERIALIZABLE No No
No
Schedules
A schedule S of n transactions is a sequential
ordering of the operations of the n
transactions.
A schedule maintains the order of operations
within the individual transaction. It is subject to
the constraint that for each transaction T
participating in S, if operation i is performed in
T before operation j, then operation i will be
performed before operation j in S.
Serial, Non-serial and Serializable
Schedules
A schedule S is serial if, for every
transaction T participating in S all of T's
operations are executed consecutively in
the schedule; otherwise it is called non-
serial (interleaved).
A schedule S of n transactions is
serializable if it is equivalent to some
serial schedule of the same n
transactions.
Example of Serial Schedules
Schedule A
T1: T2:
read_item(x)
X:= X - N;
write_item(X);
read_item(Y);
time Y:=Y + N;
write_item(Y);
read_item(X);
X:= X +
M;
write_item(X);
Advantages & Disadvantages
T1: T2:
read_item(X);
X:= X - N;
read_item(X);
time
X:= X +
M;
write_item(X);
read_item(Y);
write_item(X);
Y:=Y + N;
write_item(Y);
Serializability
A serializable schedule S is a non-serial
schedule with n transactions T1,T2….Tn whose
execution gives the same result as a serial
execution of the same n transactions.
Basic Assumption:
Each transaction preserves database consistency.
Serial execution preserves consistency
A schedule is serializable if it is equivalent to a
serial schedule of the same n transactions.
When are schedules equivalent? Two
definitions:
Conflict Equivalence
View Equivalence
Conflict Equivalence
Read(A)
Write(A)
Read(B) Non-
Write(B) conflicting
Read(A) operations
Write(A) swapped
Serial
Read(B) schedule
Write(B) T1-T2
Example 2
Consider two schedules A & B:
T1 T2 T1 T2
Read(X); Read(X);
X=X-N; X=X-N;
Write(X); Write(X);
Read(Y);
Y=Y+N; Read(X);
write(Y); X=X+M;
Write(X);
Read(X);
X=X+M; Read(Y);
Write(X); Y=Y+N;
write(Y);
A B
Example 2
According to the definition, schedule B is
equivalent to serial schedule A, since in both
schedules read(X) of T2 reads the value of X
written by T1.
Also T1 is the last transaction to write Y and
T2 is the last transaction to write X in both
schedules.
Now notice that read(Y) and and write(Y) of T1
in schedule B doesn’t conflict with read(X) and
write(X) of T2 so we can swap. Hop its clear!!
Testing for conflict serializability.
Use a directed or precedence graph:
Create a node labeled Ti for each transaction
participating in a schedule.
If Tj executes a read-item(X) after Ti executes a
write-item(X), create an edge from Ti to Tj
If Tj executes write-item(X) after Ti executes a
read-item(X), create an edge Ti to Tj
If Tj executes write-item(X) after Ti executes a
write-item(X), create an edge Ti to Tj
S is serializable iff the graph has no cycles
Example
Schedule A
T1: T2:
read_item(X);
X:= X - N;
write_item(X);
time read_item(Y);
Y:=Y + N;
write_item(Y);
read_item(X);
X:= X + M;
write_item(X);
read_item(X);
X:= X + M;
time write_item(X);
read_item(Y);
write_item(X);
Y:=Y + N;
write_item(Y);
X
X
Example (Schedule A)
T1 T2 T3 T4 T5
read(X)
read(Y)
read(Z)
read(V)
read(W)
read(W)
read(Y)
write(Y)
write(Z)
read(U)
read(Y)
write(Y)
read(Z)
write(Z)
read(U)
write(U)
Precedence Graph for Schedule
A
T1 T2
T4
T3
Test for Conflict Serializability
A schedule is conflict serializable if and only if its
precedence graph is acyclic.
Cycle-detection algorithms exist which take order
n2 time, where n is the number of vertices in the
graph. (Better algorithms take order n + e where
e is the number of edges.)
If precedence graph is acyclic, the serializability
order can be obtained by a topological sorting of
the graph nodes. This is a linear order consistent
with the partial order of the graph.
For example, a serializability order for Schedule A
would be
T T T T T .
Consider the following schedule,
S:
T1 T2 T3
Read Y
Read Z
Read X
Write X
Write Y
Write Z
Read Z
Read Y
Write Y
Read Y
Write Y
Is S serializable?
Soln:
X,Y S is serializable.
T1 T2
Possible serial schedules are:
Y T1 T2 T3
Z,Y
T3 T1 T2
T3
T3 T2 T1
Excersises
Consider three transactions T1,T2,T3 and
schedules S1 and S2 below:
T1 : r1(x),r1(z),w1(x)
T2 : r2(z),r2(y),w2(z),w2(y)
T3 : r3(x),r3(y),w3(y)
S1:
r1(x),r2(z),r1(z),r3(x),r3(y),w1(x),w3(y),r2(y),
w2(z),w2(z)
S2:
r1(x),r2(z),r3(x),r1(z),r2(y),r3(y),w1(x),w2(z),
w3(y),w2(y)
Excersises
Required:
Draw the precedence graph for S1 and
S2 and state whether each schedule is
serializable or not. If yes write the
equivalent serial schedule.
View Serializability
Let S and S´ be two schedules with the same set
of transactions. S and S´ are view equivalent if
the following three conditions are met, where Q is
a data item and Ti is a transaction:
1. If Ti reads the initial value of Q in schedule S, then Ti
must, in schedule S´, also read the initial value of Q
2. If Ti executes read(Q) in schedule S, and that value was
produced by transaction Tj (if any), then transaction Ti
must in schedule S´ also read the value of Q that was
produced by transaction Tj
3. The transaction (if any) that performs the final write(Q)
operation in schedule S (for any data item Q) must
perform the final write(Q) operation in schedule S´
View Equivalence
The idea behind view equivalence is that
as long as each read operation of a
transaction reads the result of the same
write operation in both schedules(both
have same set of txns) then the write
operation of each txn must produce the
same results.
The read operations are hence said to
see the same view in both schedules.
View Serializability