What Is a Database Transaction?
A database transaction groups multiple operations into a single all-or-nothing unit — either every operation succeeds and is committed, or if any fails, all...
Published November 19, 2026
A database transaction groups several operations into one unit that either all succeed (commit) or all fail (rollback). It keeps data consistent, such as moving money between accounts.
The concept
BEGIN starts a transaction, COMMIT makes the changes permanent and ROLLBACK undoes everything since BEGIN. If anything fails midway, nothing is half-applied.
Transactions provide ACID guarantees and isolate concurrent users from seeing each other's unfinished changes.
Transfer money
Practical examples
SQL
START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;Laravel
DB::transaction(function () {
Account::find(1)->decrement('balance', 100);
Account::find(2)->increment('balance', 100);
});Node with rollback
const c = await pool.getConnection()
try { await c.beginTransaction(); await c.query(a); await c.query(b); await c.commit() }
catch (e) { await c.rollback(); throw e } finally { c.release() }Common mistakes
- Long transactions holding locks.
- Forgetting to rollback on errors.
- Mixing non-transactional tables (MyISAM).
Key takeaways
- All or nothing.
- BEGIN / COMMIT / ROLLBACK.
- Keep them short.
FAQ
Do reads need transactions?
Only when you need a consistent view across several reads.
What is auto-commit?
Each statement is its own transaction unless you BEGIN.