Transactions & Concurrency: START TRANSACTION, COMMIT, ROLLBACK & ACID in MySQL
Overview & Learning Objectives
In enterprise production systems, multiple users, payment gateways, and automated ETL pipelines access the database concurrently. Without transactional controls, system crashes mid-operation can deduct inventory without recording payment, creating catastrophic inconsistencies. MySQL's InnoDB storage engine provides full ACID guarantees, controlled via `START TRANSACTION`, `COMMIT`, `ROLLBACK`, and four standardized isolation levels.
- Execute multi-statement atomic transactions using `START TRANSACTION`, `COMMIT`, and `ROLLBACK`.
- Understand the four tenets of ACID compliance in MySQL InnoDB.
- Differentiate the 4 SQL Transaction Isolation Levels (READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, SERIALIZABLE).
- Prevent concurrency anomalies (Dirty Reads, Non-Repeatable Reads, Phantom Reads).
Industry Standard
Standard: Financial ledgers at Stripe and Paytm execute all balance transfers within explicit database transaction blocks set to REPEATABLE READ or SERIALIZABLE to ensure zero balance drift.
Real-World Example: ScoopCraft Dark-Store Inventory & Payment Settlement
ScoopCraft deducts 2 ice cream tubs from store inventory and records the customer payment. If the payment gateway fails, inventory deduction is safely rolled back.
| Step | SQL Statement | Transaction State | Crash Impact |
|---|---|---|---|
| 1. Begin Block | START TRANSACTION; | Active Sandbox | No changes visible to other users |
| 2. Deduct Stock | UPDATE inventory SET stock = stock - 2 WHERE ... | Uncommitted | Held in InnoDB undo log |
| 3. Record Sale | INSERT INTO payments VALUES (...) | Uncommitted | Payment logged |
| 4. Finalize | COMMIT; | Permanent Durability | Written to redo log disk (Changes permanent) |
Syntax & Core SQL Implementation
-- Atomic Bank Transfer / Inventory Deduction Transaction in MySQL
START TRANSACTION;
-- Step 1: Deduct inventory stock
UPDATE inventory
SET stock_units = stock_units - 2
WHERE item_id = 101 AND stock_units >= 2;
-- Step 2: Record sales transaction
INSERT INTO orders (customer_id, order_total, payment_status)
VALUES (42, 700.00, 'Settled');
-- Step 3: Commit permanently if all succeeded
COMMIT;
-- In case of failure or exception:
-- ROLLBACK;
Parameters & Arguments Breakdown
START TRANSACTION: Begins a new atomic transaction block, turning off autocommit for the session.COMMIT: Persists all modifications made during the transaction permanently to disk.ROLLBACK: Cancels all modifications made during the transaction, restoring original state.REPEATABLE READ: MySQL InnoDB default isolation level preventing Dirty Reads and Non-Repeatable Reads.
Sample Walkthrough (Q&A)
Question: What happens if the database server crashes or loses power after Step 2 but before COMMIT?
SQL Pattern: Server Crash before COMMIT
Result: On restart, InnoDB crash recovery automatically executes a ROLLBACK using the undo log
Explanation: Atomicity guarantees that partially executed transactions are never left stranded in the database.
Common Mistakes & Limitations
- Assuming DDL statements like `CREATE TABLE` or `TRUNCATE TABLE` can be rolled back (in MySQL, DDL causes an implicit COMMIT, ending transactions immediately!).
- Leaving transactions open indefinitely (`START TRANSACTION` without `COMMIT`), which holds InnoDB locks and freezes other users.
Keyboard Shortcuts (MySQL Workbench)
| Action | Windows | Mac |
|---|---|---|
| Check Autocommit Status | SELECT @@autocommit; | SELECT @@autocommit; |
Pro Tips & Modern Best Practices
- MySQL runs in `autocommit = 1` by default, meaning every standalone statement commits immediately unless preceded by `START TRANSACTION`.
- MySQL InnoDB's default isolation level is `REPEATABLE READ`, which uses Multi-Version Concurrency Control (MVCC) to provide consistent snapshot reads without locking tables.