A database transaction is a group of operations treated as a single unit of work. Either every step is committed and becomes permanent, or the whole group is rolled back as if nothing happened.
Why it matters
Transactions keep data consistent when multiple related changes must stay in step, such as creating an order and reducing stock. They also control what concurrent users can see through isolation levels. Long-running transactions hold locks and block others, so keeping them short is a key operational habit.
Example
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
If the second update fails, ROLLBACK restores the first account to its original balance.
Common mistakes
- Leaving a transaction open while waiting on a user or a slow external API call, which holds locks.
- Forgetting error handling, so a failed statement leaves the connection in an aborted transaction.
- Assuming auto-commit is off. Many clients commit each statement immediately by default.
Related
Get notified when this term updates
Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.
This page is general information written by our editorial team and updated when documentation changes. Servers and providers differ, so test changes safely first. See our editorial policy and disclaimer.