Ch-5 Transactions
Ch-5 Transactions
Ch-5 Transactions
Concurrent DB access
concurrent with …
concurrent with …
concurrent with …
System-Failure Goal
Guarantee all-or-nothing execution, regardless of
failures.
Transactions
Solution for both concurrency and failures
SQL standard:
Transaction begins automatically on first SQL statement
On “commit” transaction ends and new one begins
Types of Failures
Computer Failure
A hardware or software error in the computer during
transaction execution.
Transaction Error
Failure caused by an operation in the transaction
Types of Failures cont…
Local Errors
Conditions that cause the cancellation of transaction.
(eg: data needed for transaction not found.)
Advantages are:
increased processor and disk utilization, leading to better
transaction throughput
one transaction can be using the CPU while another is reading from or
writing to the disk
throughput: # of transactions executed in a given amount of time.
reduced average response time for transactions
short transactions need not wait behind long ones.
Unrepeatable Read:
A transaction T1 may read a given value. If another
transaction later updates that value and T1 reads that value
again, then T1 will see a different value.
The Lost Update Problem
This occurs when two transactions that access the same
database items have their operations interleaved in a way
that makes the value of some database item incorrect.
The Temporary Update Problem (Dirty Read)
This occurs when one transaction updates a database item
and then the transaction fails for some reason.
The updated item is accessed by another transaction before it is
changed back to its original value.
Incorrect Summary Problem
If one transaction is calculating an aggregate summary
function on a number of records while other
transactions are updating some of these records, the
aggregate function may calculate some values before
they are updated and others after they are updated.
Incorrect Summary Problem
Transaction and System Concepts
A transaction is an atomic unit of work that is either completed in its entirety or not done at all.
For recovery purposes, the system needs to keep track of when the transaction starts, terminates,
and commits or aborts.
A transaction must be in one of the following states:
Active:
the initial state; the transaction stays in this state while it is executing
Partially committed:
after the final statement has been executed.
Failed:
after the discovery that normal execution can no longer proceed.
Committed:
after successful completion.
Aborted:
after the transaction has been rolled back and the database restored to its state prior to the
start of the transaction. Two options after it has been aborted:
restart the transaction (only if no internal logical error)
Example: Given
T1 = R1(Q) W1(Q) & T2 = R2(Q) W2(Q)
It is not strict
It permits T2 to write X even though T1 that last
wrote X had not yet committed (or aborted).
Serial Schedules
A schedule S is serial,
if for every transaction T participating in the
schedule, all the operations of T are executed
consecutively
if operations from different transactions are not
interleaved.
T2 = R2(X) W2(X)
T2 = R2(X) W2(X)
read(Q)
write(Q)
write(Q)
T1 T2 T3
read(x)
write(x)
write(x)
write(x)
Summary: Schedules
Schedules must be conflict or view
serializable, and recoverable - for the sake
of database consistency, and preferably
cascadeless.
Exercise Question1
Which of the following schedules is (conflict) serializable?
For each serializable schedule, determine the equivalent
serial schedules.
(a) This schedule is not serializable because T1 reads X (r1(X)) before T3 but T3 reads X (r3(X)) before T1 writes
X (w1(X)), where X is a common data item. The operation r2(X) of T2 does not affect the schedule at all so
its position in the schedule is irrelevant. In a serial schedule T1, T2, and T3, the operation w1(X) comes after
r3(X), which does not happen in the question.
(b) This schedule is not serializable because T1 reads X ( r1(X)) before T3 but T3 writes X (w3(X)) before T1
writes X (w1(X)). The operation r2(X) of T2 does not affect the schedule at all so its position in the schedule
is irrelevant. In a serial schedule T1, T3, and T2, r3(X) and w3(X) must come after w1(X), which does not
happen in the question.
(c) This schedule is serializable because all conflicting operations of T3 happens before all conflicting operation
of T1. T2 has only one operation, which is a read on X (r2(X)), which does not conflict with any other
operation. Thus this serializable schedule is equivalent to r2(X); r3(X); w3(X); r1(X); w1(X) (e.g., T2 T3
T1) serial schedule.
(d) This is not a serializable schedule because T3 reads X (r3(X)) before T1 reads X (r1(X)) but r1(X) happens
before T3 writes X (w3(X)). In a serial schedule T3, T2, and T1, r1(X) will happen after w3(X), which does not
happen in the question.
Exercise Question2
Consider the three transactions T1, T2, and T3, and the
schedules S1 and S2 given below. Draw the serializibility
(precedence) graphs for S1 and S2 and state whether each
schedule is serializable or not. If a schedule is serializable, write
down the equivalent serial schedule(s).
S3: r1(x); r2(z); r1(z); r3(x); r3(y); w1(x); c1; w3(y); c3;
r2(y); w2(z); w2(y);c2
S4: r1(x); r2(z); r1(z); r3(x); r3(y); w1(x); w3(y); r2(y);
w2(z); w2(y); c1; c2; c3;
S5: r1(x); r2(z); r3(x); r1(z); r2(y); r3(y); w1(x); w2(z);
w3(y); w2(y); c3; c2;
Resources
Chapter 21 of Fundamentals of Database Systems (FODS),
6th Edition.
References books
Internet Surf