Ch14-Transaction Management
Ch14-Transaction Management
Ch14-Transaction Management
1
Chapter 14: Transactions
Transaction Concept
Transaction State
Concurrent Executions
Serializability
Recoverability
Implementation of Isolation
Transaction Definition in SQL
Testing for Serializability.
2
Transactions
3
Transaction Concept
A transaction is a unit of program execution that accesses
and possibly updates various data items.
E.g. transaction to transfer $50 from account A to account B:
1. read(A)
2. A := A 50
3. write(A)
4. read(B)
5. B := B + 50
6. write(B)
Two main issues to deal with:
Failures of various kinds, such as hardware failures and
system crashes
Concurrent execution of multiple transactions
4
Example of Fund Transfer
Transaction to transfer $50 from account A to account B:
1. read(A)
2. A := A 50
3. write(A)
4. read(B)
5. B := B + 50
6. write(B)
Atomicity requirement
If the transaction fails after step 3 and before step 6, money will be lost leading to an
inconsistent database state
Failure could be due to software or hardware
The system should ensure that updates of a partially executed transaction are not
reflected in the database
Durability requirement
Once the user has been notified that the transaction has completed (i.e., the transfer of
the $50 has taken place), the updates to the database by the transaction must persist
even if there are software or hardware failures.
5
Example of Fund Transfer (Cont.)
Transaction to transfer $50 from account A to account B:
1. read(A)
2. A := A 50
3. write(A)
4. read(B)
5. B := B + 50
6. write(B)
Consistency requirement in above example:
the sum of A and B is unchanged by the execution of the transaction
In general, consistency requirements include
Explicitly specified integrity constraints such as primary keys and foreign keys
Implicit integrity constraints
e.g. sum of balances of all accounts, minus sum of loan amounts must equal
value of cash-in-hand
A transaction must see a consistent database.
During transaction execution the database may be temporarily inconsistent.
When the transaction completes successfully the database must be
consistent
Erroneous transaction logic can lead to inconsistency
6
Example of Fund Transfer (Cont.)
Isolation requirement if between steps 3 and 6, another
transaction T2 is allowed to access the partially updated database, it
will see an inconsistent database (the sum A + B will be less than it
should be).
T1 T2
1. read(A)
2. A := A 50
3. write(A)
read(A), read(B), print(A+B)
4. read(B)
5. B := B + 50
6. write(B
Isolation can be ensured trivially by running transactions serially
that is, one after the other.
However, executing multiple transactions concurrently has significant
benefits, as we will see later.
7
ACID Properties
To preserve the integrity of data the database system must
ensure:
Atomicity
Either all operations of the transaction are properly reflected in the database
or none are.
Consistency
Execution of a transaction in isolation preserves the consistency of the
database.
Isolation
Although multiple transactions may execute concurrently, each transaction
must be unaware of other concurrently executing transactions
i.e. for every pair of transactions Ti and Tj, it appears to Ti that either Tj, finished execution
before Ti started, or Tj started execution after Ti finished.
Durability
After a transaction completes successfully, the changes it has made to the
database persist, even if there are system failures.
8
Transaction State
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.
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
can be done only if no internal logical error
kill the transaction
Committed after successful completion.
9
Transaction State (Cont.)
10
Concurrent Executions
Multiple transactions are allowed to run concurrently in the system.
Advantages are:
increased processor and disk utilization, leading to better
transaction throughput
E.g. one transaction can be using the CPU while another is
reading from or writing to the disk
reduced average response time for transactions: short
transactions need not wait behind long ones.
Concurrency control schemes mechanisms to achieve isolation
that is, to control the interaction among the concurrent
transactions in order to prevent them from destroying the
consistency of the database
Will study in Chapter 16, after studying notion of correctness
of concurrent executions.
11
Schedules
Schedule a sequences of instructions that specify the chronological
order in which instructions of concurrent transactions are executed
A schedule for a set of transactions must consist of all instructions
of those transactions
Must preserve the order in which the instructions appear in each
individual transaction.
A transaction that successfully completes its execution will have a
commit instructions as the last statement
By default transaction assumed to execute commit instruction as its
last step
A transaction that fails to successfully complete its execution will have
an abort instruction as the last statement
12
Schedule 1
Let T1 transfer $50 from A to B, and T2 transfer 10% of the
balance from A to B.
A serial schedule in which T1 is followed by T2 :
13
Schedule 2
A serial schedule where T2 is followed by T1
14
Schedule 3
Let T1 and T2 be the transactions defined previously. The following schedule is not a serial (concurrent) schedule, but it is
equivalent to Schedule 1
17
Simplified view of transactions
18
Conflicting Instructions
Let us consider a schedule S in which there are two consecutive
instructions, I and J , of transactions Ti and Tj , respectively (i = j).
If I and J refer to different data items, then we can swap I and J
without affecting the results of any instruction in the schedule.
However, if I and J refer to the same data item Q, then the order
of the two steps may matter. Since we are dealing with only read
and write instructions, there are four cases that we need to
consider:
1. I = read(Q), J = read(Q).
The order of I and J does not matter, since the same value of Q is read by
Ti and Tj , regardless of the order.
2. I = read(Q), J = write(Q).
If I comes before J , then Ti does not read the value of Q that is written by
Tj in instruction J . If J comes before I, then Ti reads the value of Q that is
written by Tj. Thus, the order of I and J matters.
19
Conflict Serializability
3. I = write(Q), J = read(Q). The order of I and J matters for
reasons similar to those of the previous case.
4. I = write(Q), J = write(Q). Since both instructions are write
operations, the order of these instructions does not affect either
Ti or Tj .
However, the value obtained by the next read(Q) instruction of
S is affected, since the result of only the latter of the two write
instructions is preserved in the database
If there is no other write(Q) instruction after I and J in S, then
the order of I and J directly affects the final value of Q in the
database state that results from schedule S.
NOTE: We say that I and J conflict if they are operations by
different transactions on the same data item, and at least one
of these instructions is a write operation.
20
Conflict Serializability (Cont.)
If a schedule S can be transformed into a schedule S by a series of
swaps of non-conflicting instructions, we say that S and S are conflict
equivalent.
We say that a schedule S is conflict serializable if it is conflict
equivalent to a serial schedule
21
Conflict Serializability (Cont.)
Schedule 3 can be transformed into Schedule 6, a serial schedule
where T2 follows T1, by series of swaps of non-conflicting
instructions. Therefore Schedule 3 is conflict serializable.
Schedule 3 Schedule 6
1. Swap the read(B) instruction of T1 with the write(A) instruction of T2.
2. Swap the read(B) instruction of T1 with the read(A) instruction of T2.
3. Swap the write(B) instruction of T1 with the write(A) instruction of T2.
4. Swap the write(B) instruction of T1 with the read(A) instruction of T2.
22
Conflict Serializability (Cont.)
23
Testing for Serializability
Method for determining conflict serializability of a schedule
Consider a schedule S
We construct a directed graph, called a precedence graph, from S
This graph consists of a pair G = (V, E), where V is a set of vertices and
E is a set of edges
The set of vertices consists of all the transactions participating in the
schedule. The set of edges consists of all edges Ti Tj for which one of
three conditions holds:
1. Ti executes write(Q) before Tj executes read(Q).
2. Ti executes read(Q) before Tj executes write(Q).
3. Ti executes write(Q) before Tj executes write(Q).
24
If the precedence graph for S has a cycle,
then schedule S is not conflict serializable.
If the graph contains no cycles, then the
schedule S is conflict serializable
The precedence graph for schedule 4
appears in the figure
It contains the edge T1 T2, because T1
executes read(A) before T2 executes
write(A)
It also contains the edge T2T1, because
T2 executes read(B) before T1 executes
write(B).
Thus, to test for conflict serializability, we
need to construct the precedence graph
and to invoke a cycle-detection algorithm.
25
The drawing sequence for the precedence graph:-
1.For each transaction Ti participating in schedule S, create a node labelled Ti in the precedence
graph. So the precedence graph contains T1, T2, T3
2.For each case in S where Ti executes a write_item(X) then Tj executes a read_item(X), create an
edge (Ti --> Tj) in the precedence graph. This occurs nowhere in the above example, as there is no
read after write.
3.For each case in S where Ti executes a read_item(X) then Tj executes a write_item(X), create an
edge (Ti --> Tj) in the precedence graph. This will bring to front a directed graph from T1 to T2.
4.For each case in S where Ti executes a write_item(X) then Tj executes a write_item(X), create an
edge (Ti --> Tj) in the precedence graph. It creates a directed graph from T2 to T1, T1 to T3, and T2 to
T3.
5.The schedule S is serializable if the precedence graph has no cycles. As T1 and T2 constitute a
cycle, then we cannot declare S as serializable or not and serializability has to be checked using
other methods.
26
Database System Concepts - 6th Edition 14.26 Silberschatz, Korth and Sudarshan
27
Database System Concepts - 6th Edition 14.27 Silberschatz, Korth and Sudarshan
Transaction Isolation and Atomicity
28
Recoverable Schedules
Need to address the effect of transaction failures on concurrently
running transactions.
Recoverable schedule if a transaction Tj reads a data item previously
written by a transaction Ti , then the commit operation of Ti appears before the
commit operation of Tj.
The following schedule is not recoverable if T9 commits immediately after the
read
T8 is called a partial schedule
because commit is missing
If T8 should abort, T9 would have read (and possibly shown to the user) an
inconsistent database state. Hence, database must ensure that schedules are
recoverable
To make it recoverable, T9 would have to delay committing until after T8
commits.
29
Cascading Rollbacks
Cascading rollback a single transaction failure leads to a series of
transaction rollbacks. Consider the following schedule where none of the
transactions has yet committed (so the schedule is recoverable)
30
Cascadeless Schedules
Cascadeless schedules cascading rollbacks
cannot occur; for each pair of transactions Ti and Tj
such that Tj reads a data item previously written by
Ti, the commit operation of Ti appears before the
read operation of Tj.
Every cascadeless schedule is also recoverable
It is desirable to restrict the schedules to those that
are cascadeless
31
Concurrency Control
A database must provide a mechanism that will ensure that all possible
schedules are
either conflict or view serializable, and
are recoverable and preferably cascadeless
A policy in which only one transaction can execute at a time generates
serial schedules, but provides a poor degree of concurrency
Are serial schedules recoverable/cascadeless?
Testing a schedule for serializability after it has executed is a little too
late!
Goal to develop concurrency control protocols that will assure
serializability.
32
Concurrency Control vs. Serializability Tests
33
Weak Levels of Consistency
Some applications are willing to live with weak levels of consistency,
allowing schedules that are not serializable
E.g. a read-only transaction that wants to get an approximate total
balance of all accounts
E.g. database statistics computed for query optimization can be
approximate (why?)
Such transactions need not be serializable with respect to other
transactions
Tradeoff accuracy for performance
34
End of Chapter 14
35