Transactions
Transactions
Outline
Transaction Concept
1. read(A)
2. A := A – 50
3. write(A)
4. read(B)
5. B := B + 50
6. write(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
• 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.
Consistency requirement in above example:
• Explicitly specified integrity constraints such as primary keys and foreign keys
e.g., sum of balances of all accounts, minus sum of loan amounts must equal
value of cash-in-hand
ACID Properties
Transaction State
Active – the initial state; the transaction stays in this state while it is executing
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:
Transaction State
Concurrent Executions
Advantages are:
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.
• That is, to control the interaction among the concurrent transactions in order to prevent them from destroying the
consistency of the database
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
Schedule 1
Schedule 2
Schedule 3
Schedule 4
Serializability
A (possibly concurrent) schedule is serializable if it is equivalent to a serial schedule. Different forms of schedule
equivalence give rise to the notions of:
1. conflict serializability
2. view serializability
We assume that transactions may perform arbitrary computations on data in local buffers in between reads and
writes.
Conflicting Instructions
Conflict Serializability
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.
Conflict Serializability
Conflict Serializability
View Serializability
View Serializability
The precedence graph test for conflict serializability cannot be used directly to test for view serializability.
• Extension to test for view serializability has cost exponential in the size of the precedence graph.
The problem of checking if a schedule is view serializable falls in the class of NP-complete problems.
However practical algorithms that just check some sufficient conditions for view serializability can still be used.
Recoverable Schedules
Cascading Rollbacks
Cascadeless Schedules
Concurrency Control
A database must provide a mechanism that will ensure that all possible schedules are
A policy in which only one transaction can execute at a time generates serial schedules, but provides a poor degree
of concurrency
Testing a schedule for serializability after it has executed is a little too late!
Schedules must be conflict or view serializable, and recoverable, for the sake of database consistency, 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.
Concurrency-control schemes tradeoff between the amount of concurrency they allow and the amount of
overhead that they incur.
Some schemes allow only conflict-serializable schedules to be generated, while others allow view-serializable
schedules that are not conflict-serializable.
Concurrency-control protocols allow concurrent schedules, but ensure that the schedules are conflict/view
serializable, and are recoverable and cascadeless .
Concurrency control protocols (generally) do not examine the precedence graph as it is being created
Different concurrency control protocols provide different tradeoffs between the amount of concurrency they allow
and the amount of overhead that they incur.
Tests for serializability help us understand why a concurrency control protocol is correct.
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?)
Serializable — default
Levels of Consistency
Lower degrees of consistency useful for gathering approximate information about the database
Overview
Locking
Timestamps
Read timestamp
Write timestamp