Unit content
Database transactions
A transaction groups several database operations into one logical unit of work. Either the transaction's changes are committed together or they are rolled back instead of leaving a partial result.
Atomicity
Suppose transferring money requires both
subtract from account A
add to account B
If the second operation fails after the first succeeds, the database must not leave the transfer half-completed. A transaction makes the pair atomic.
COMMIT and ROLLBACK
A successful transaction ends with COMMIT, making its changes durable according to the database's guarantees.
ROLLBACK discards the transaction's uncommitted changes.
ACID properties
Transactions are commonly described through ACID:
- atomicity: the transaction is treated as one unit;
- consistency: committed states respect declared invariants;
- isolation: concurrent transactions are controlled so their interactions follow the chosen isolation model;
- durability: committed changes survive failures covered by the database's persistence guarantees.
Concurrency
Several transactions can run at the same time. Without sufficient isolation, one transaction may observe another's intermediate state or produce anomalies such as lost updates.
Stronger isolation prevents more anomalies but can require additional coordination.
Transactions therefore protect invariants across several statements and across concurrent activity; they are not merely a way to group SQL text cosmetically.