OCR H446, section 1.3.2 (f)
Transactions
A transaction is a set of database operations that must be treated as a single unit of work: either all of it happens or none of it does.
Example: transferring £100 from account A to account B.
- Subtract £100 from A.
- Add £100 to B.
If the system crashed after step 1 but before step 2, the £100 would vanish. So the two steps are a single transaction. If the transaction can't finish, everything is rolled back to how it was. When it succeeds, it is committed, which makes the changes permanent.
Transaction processing is the management of such transactions, so that the database stays correct, even when things go wrong or many users work at once. It is used in banking, ticket booking, online shopping and stock control.
ACID
A reliable transaction system provides four properties, remembered as ACID.
| Property | Meaning | Banking example |
|---|---|---|
| Atomicity | A transaction is "all or nothing". It is treated as one indivisible unit | Both the subtraction and the addition happen, or neither does |
| Consistency | A transaction takes the database from one valid state to another. All rules (such as keys and validation) are kept | The total money in the two accounts is the same before and after |
| Isolation | Transactions running at the same time don't interfere. Each acts as if it were the only one | Two transfers at the same moment don't see each other's half-finished work |
| Durability | Once committed, the changes are permanent, even after a crash or power failure | After "transfer complete", the new balances are safe on disk |
The problem: concurrent access
Many users may use the database at once. Without control, their changes can clash.
The lost update
An account has £100. Two transactions run together.
- Transaction A adds £50. Transaction B subtracts £30.
- The correct result is .
| Step | Transaction A | Transaction B | Balance |
|---|---|---|---|
| 1 | Reads balance (100) | 100 | |
| 2 | Reads balance (100) | 100 | |
| 3 | Calculates 150, writes 150 | 150 | |
| 4 | Calculates 70, writes 70 | 70 |
The final balance is £70, which is wrong. B started from the old balance, so A's update was lost. This breaks isolation.
Record locking
Record locking prevents the lost update by stopping other transactions from using a record while one is working on it.
- A transaction locks the record it wants to change.
- Other transactions that need that record must wait.
- When the first transaction commits or rolls back, the lock is released.
Applied to the example: A locks the balance, reads 100, writes 150, and unlocks. Then B reads 150 and writes 120. The result is correct.
The transactions give the same result as if they had run one after another, which is what isolation means.
Problems with locking
- Waiting: transactions are delayed, which slows the system down.
- Deadlock: two transactions each hold a lock that the other needs, so both wait forever. The system must detect this and cancel one of the transactions.
- Locking a whole table is simple but slow. Locking individual records allows more users at once, but is more complex to manage.
Redundancy
In transaction processing, redundancy means deliberately keeping extra copies of data or hardware so the system keeps working, and no data is lost, if something fails.
(This is different from the unwanted data redundancy that normalisation removes, 1.3.2 (c).)
Forms of deliberate redundancy:
- Backups: copies of the data kept elsewhere.
- Mirroring and RAID: data is written to two or more disks at once, so if one fails, the other still has it.
- Replication: the database is copied on to several servers, which stay in step.
- Duplicate hardware: spare servers and power supplies, to take over if one fails (failover).
- Transaction logs: a record of every change, so the database can be rebuilt after a crash. This supports durability.
Worked example
Task: two customers try to book the last ticket for a concert at the same moment. Explain the problem, and how locking and ACID help.
- The problem: both read "1 ticket available". Both book it, and both write "0 left". Two bookings now exist for one ticket, because one update overwrote the other's check.
- Locking: when the first customer starts to book, the ticket record is locked, and the second customer waits. The first writes "0 left" and the lock is released. The second now reads 0 tickets and is told that it's sold out. This keeps isolation.
- Atomicity: booking the ticket and taking payment are one transaction. If the payment fails, the ticket is released again (rolled back), instead of being lost.
- Durability: once the booking is confirmed, it is recorded safely, so it survives a crash.
Exam tip: learn ACID with a one-line meaning for each letter, and an example. Describe record locking as stopping others using a record while a transaction updates it, to prevent lost updates, and mention deadlock as a risk. Don't confuse deliberate redundancy (backups) with unwanted data duplication.
Check yourself
What is a transaction?
A set of database operations that is treated as a single unit of work, so that either all of it is completed or none of it is.
What do the letters of ACID stand for?
Atomicity, consistency, isolation and durability.
How does record locking prevent a lost update?
It stops other transactions from using a record while one is changing it, so the updates happen one after another and none is overwritten.
What is deadlock?
When two transactions each hold a lock that the other needs, so both wait forever.
Give an example of redundancy in a database system
Any one of: regular backups; mirroring or RAID; replicating the database across several servers; a spare server for failover.