16 Transaction, CC, Recovery
16 Transaction, CC, Recovery
Piyush Ojha
School of Computing and Mathematics
University of Ulster
[email protected]
23 November 2005
Abstract
Database transactions are required to be atomic, consistent, isolated and
durable. It is the programmers responsibility to define the transactions
appropriately and ensure that they are (individually) consistent. Their atomicity
must be ensured by the Database Management System (by rolling back failed
transactions). Their concurrent scheduling so that transactions remain e ectively
isolated and their durability after commit is also the responsibility of the DBMS.
We discuss serialisable, recoverable and cascadeless schedules and concurrency
control protocols for generating such schedules. Techniques for recovering from
failure are also discussed.
1 Sources
2 Transactions
Data integrity requires that the database management system maintains the
following
properties of transactions (collectively known as ACID properties):
Transaction T1 Transaction T2
read(A);
A:=A+50.00
read(A);
A:=A-100.00;
write(A);
write(A);
read(B);
B:= B+100.00;
write(B);
In this schedule the particular interleaving of database operations the
update of A byT2 is lost. The outcome is not as if
transaction T1 andwereT2isolated from each other.
Clearly, this is undesirable because the database is not consistent.
The concurrency-control component of the database management system
ensures isolation.
Durability: Once the money transfer has been completed and the account
balances
updated, the changes to the database must persist (even if there is a
subsequent system
failure).
A committed transaction can not be undone by aborting it. Its e ects can only
be
by undone
executing another conpensating transaction.
The recovery-management component of the DBMS also ensures durability
of transactions.
We will examine some of the techniques used by DBMSs to ensure atomicity,
isolation
durabilityand
of transactions.
Partially
Committed Committed
Active
Failed Aborted
Transaction T1 Transaction T2
read(A);
A:=A-100.00;
write(A);
read(B);
B:= B+100.00;
write(B);
read(A);
temp=0.2*A;
A:=A-temp;
write(A);
read(B);
B:= B+temp;
write(B);
Transaction T1 Transaction T2
read(A);
temp=0.2*A;
A:=A-temp;
write(A);
read(B);
B:= B+temp;
write(B);
read(A);
A:=A-100.00;
write(A);
read(B);
B:= B+100.00;
Table write(B);
executed
database
Transactions/
2: Schedule
is
before 2: AnT1
in aConcurrency/
consistent
alternative
state.
. AtRecovery
the end A =where
serial schedule 700.00 , B = 1300.00 and
T2the
is7
Piyush Ojha 23 November 2005
Transaction T1 Transaction T2
read(A);
A:=A-100.00;
write(A);
read(A);
temp=0.2*A;
A:=A-temp;
write(A);
read(B);
B:= B+100.00;
write(B);
read(B);
B:= B+temp;
write(B);
Transaction T1 Transaction T2
read(A);
A:=A-100.00;
read(A);
temp=0.2*A;
A:=A-temp;
write(A);
read(B);
write(A);
read(B);
B:= B+100.00;
write(B);
B:= B+temp;
write(B);
Thus the relative order of Ii and Ij matters if they operate on the same data item and at
least one operation is a write. In this case, Ii and Ij are said to con ict.
IfTransactions/
operations,
Cona schedule SConcurrency/
SandcanSbe are
ict Serialisability
Equivalence transformed
Recovery
said to beinto
con anther
ict equivalent.
schedule by Sswapping non-con icting9
Piyush Ojha 23 November 2005
Examples
Example 1
Transaction T1 Transaction T2
read(A);
write(A);
read(B);
write(B);
read(A);
write(A);
read(B);
write(B);
Transaction T1 Transaction T2
read(A);
write(A);
read(A);
write(A);
read(B);
write(B);
read(B);
write(B);
Transaction T3 Transaction T4
read(A);
write(A);
write(A);
Transaction T1 Transaction T5
read(A);
A:=A-100.00;
write(A);
read(B);
B:=B-50.0;
write(B);
read(B);
B:= B+100.00;
write(B);
read(A);
A:= A+50.0;
write(A);
Schedules S and S comprising the same set of transactions are said to be view equivalent
if the following conditions are satisfied:
1. For each data item Q , if transaction Ti reads the initial value ofin theQschedule
S , it must also read the initial value of Q in S .
2. For every data item Q , if the value of Q read by Ti was S
in previously written by
Tj , the value of Q read by Ti in S must also have been previously written byTj.
3. For every data item Q , the final value must be written to the database by the same
transaction in both schedules.
Conditions 1 and 2 ensure that every transaction reads the same data in the
two schedules.
Likewise, the third condition ensures that both schedules make the same
updates
database.to the
A concurrent schedule is view serialisable if it is view equivalent to a serial
schedule.
Example
T T
1 2
T T
1 2
Transaction T1 Transaction T2
read(A);
write(A);
read(A);
write(A);
commit;
read(B);
write(B);
Cascadeless Schedule
A concurrent schedule is cascadeless if, for every pair of transactions Ti and Tj
such that
Tj reads a data item previously written by Ti , commits
Ti before the read operation of
Tj .
Every cascadeless schedule is also recoverable.
3 Concurrency Control
Lo ck-based protocols
Timestamp-based protocols
Shared: A transaction Ti holding a shared lock on a data item can read Q it but not
write it. Other transactions can simultaneously hold a shared lock on the data
item.
Exclusive: A transaction holding an exclusive lock on a data item can read as
well
writeasit. Only one transaction can hold an exclusive lock on a data ite at a
time.
A transaction
unlock(B);
print(A+B);
T1 transfers 100.00 from the savings account (A) to the current account; printsT2
the
value of A + B .
Notice that both transactions release a lo ck as soon as they have finished
using
item. a data
This is not a good idea because it allows concurrent schedules that are not
serialisable. For 1 below.
example, Schedule
lock X(A);
read(A);
A:=A-100.00;
write(A);
lock X(B);
unlock(A);
read(B);
B:= B+100.00;
write(B);
unlock(B);
Transaction T4
lock S(A);
read(A);
lock S(B);
unlock(A);
read(B);
unlock(B);
print(A+B);
Deadlock
Lo
Transactions/
ck-based protocols
Concurrency/
can lead
Recovery
to a deadlock. 20
Piyush Ojha 23 November 2005
Every transaction requests locks and releases locks in two phases, a growing
phase and phase.
shrinking a
In the growing phase, a transaction may request lo cks but may not release
any lo cks.
In the shrinking phase, a transaction may release lo cks but may not request
any new lo cks.
Transactions T3 and T4 in the previous example conform to the two-phase locking protocol.
The two-phase locking protocol has the following properties:
It ensures serialisability.
The equivalent serial order is the order in which the transactions make their
last
request for
Transactions/
It
example
Cascading
does not a lock (i.e.
conform
Concurrency/
rollbacks
guarantee
to thethe
may orderfrom
freedom
Recovery
two-phase
occur. in which their
locking
deadlock. growing
protocol, yetphase
Transactions ends).
end up T3 cked.
deadlo andtheT5
in previous
21
Piyush Ojha 23 November 2005
The timestamp-ordering proto col ensures that read and write operations are
executed
timestampinorder.
The rules of this proto col are as follows: