Sub topic 1
What a Transaction Is and How It Moves Through Its States
Prerequisites
This chapter assumes you know basic relational database concepts (tables, rows, queries), simple SQL statements such as SELECT and UPDATE, and basic graph ideas such as directed edges and cycles.
Transactions and their states
A transaction is a group of read and write operations that the database treats as one unit of work. It either completes as a whole or leaves no trace. A transaction moves through these states:
- Active: its operations are running.
- Partially committed: its last operation has run, but the result is not yet permanent.
- Committed: its changes are permanent.
- Failed: normal execution cannot continue.
- Aborted: it is rolled back, and the database returns to the state it had before the transaction began.
The ACID properties
ACID is a set of four guarantees for transactions.
- Atomicity: all operations happen or none do. A transfer that debits account A and then crashes before crediting account B must have its debit undone.
- Consistency: a transaction moves the database from one valid state to another and respects integrity constraints.
- Isolation: concurrent transactions do not see each other's unfinished work in a way that corrupts results.
- Durability: once committed, changes survive crashes.
Serial and concurrent schedules
A schedule is the order in which the operations of several transactions run. A serial schedule runs each transaction to completion before the next one starts. A concurrent schedule interleaves operations from different transactions. The following Python function checks whether a schedule is serial.
def is_serial(schedule):
# schedule: list of (txn, op, item) tuples
seen = []
for txn, _, _ in schedule:
if not seen or seen[-1] != txn:
if txn in seen:
return False
seen.append(txn)
return True
s1 = [('T1', 'R', 'A'), ('T1', 'W', 'A'), ('T2', 'R', 'A')]
s2 = [('T1', 'R', 'A'), ('T2', 'R', 'A'), ('T1', 'W', 'A')]
print(is_serial(s1)) # True
print(is_serial(s2)) # FalseWhat goes wrong without control
Interleaving can break correctness. A lost update happens when two transactions read the same balance of 100, both write a new balance, and one write overwrites the other. A dirty read happens when a transaction reads a value written by another transaction that later aborts.
Conflicting operations and conflict equivalence
Two operations conflict when they come from different transactions, touch the same item, and at least one of them is a write. There are three kinds: read-write, write-read and write-write. Two reads never conflict, and operations on different items never conflict.
If two adjacent operations do not conflict, swapping them does not change the result. Two schedules are conflict equivalent when one can be turned into the other by such swaps. A schedule that is conflict equivalent to some serial schedule is conflict serializable.
Exercise
Is the schedule R1(A), R2(A), W1(A) conflict equivalent to a serial schedule? Explain briefly.
Show answer
Yes. R1(A) and R2(A) do not conflict, so swap them to get R2(A), R1(A), W1(A). T2 then runs completely before T1 starts only if T2 has no further operations, and here it does not. Read carefully: the swap gives R2(A) R1(A) W1(A), which is serial only if T1 completes before T2 begins, and it does not. Therefore the schedule is not conflict equivalent to a serial schedule. The only conflict is between R2(A) and W1(A), which orders T2 before T1, and no swap can remove it.