What is a transaction, and what are the ACID properties?
EasyConceptTransactions & ACID
Answer
A transaction is a sequence of reads and writes that the database treats as one logical unit of work: either all of it takes effect or none of it does. ACID names the four guarantees a DBMS gives for every transaction.
- Atomicity: all or nothing. If a step fails, the earlier steps are undone using the undo log.
- Consistency: a transaction takes the database from one valid state to another, respecting every constraint.
- Isolation: concurrent transactions do not see each other's unfinished changes; the result is as if they ran one after another, to the degree set by the isolation level.
- Durability: once committed, changes survive crashes, because they are first written to a log on disk.
Example: Transfer 500 from A (balance 1000) to B (balance 200):
1. read A = 1000; write A = 500
2. read B = 200; write B = 700
3. COMMIT
If the system crashes after step 1, atomicity restores A to 1000. Consistency means the total, 1200, is the same before and after the transfer.