Why Transactions?
Moving 30 rupees from one account to another is two updates. If the server crashes between them, money disappears. A transaction groups statements so they either all happen or none happen. InnoDB (the default MySQL engine) supports transactions; MyISAM does not.
| ACID | Meaning |
|---|---|
| Atomicity | All or nothing |
| Consistency | Constraints are never broken by a committed transaction |
| Isolation | Concurrent transactions do not see each other's half-done work |
| Durability | A committed change survives a crash (redo log) |
Autocommit
By default MySQL runs with autocommit = 1: each statement is its own transaction and is committed immediately. START TRANSACTION (or BEGIN) switches to manual mode until COMMIT or ROLLBACK.
Result:
| autocommit | isolation |
|---|---|
| 1 | REPEATABLE-READ |
COMMIT and ROLLBACK
Result:
| id | owner | balance |
|---|---|---|
| 1 | Asha | 70 |
| 2 | Ben | 130 |
Result:
| asha_inside_transaction |
|---|
| 70 |
Result:
| asha_after_rollback |
|---|
| 100 |
SAVEPOINT: Undo Part of a Transaction
Result:
| msg |
|---|
| step 1 |
| step 3 |
Statements That Commit Automatically
Most DDL statements (CREATE, ALTER, DROP, TRUNCATE, RENAME) cause an implicit commit in MySQL. You cannot roll them back:
Result:
| rows_kept_after_rollback |
|---|
| 1 |
Errors Do Not Roll Back Everything
In MySQL a failing statement undoes only that statement. The transaction stays open and earlier statements stay pending. You decide to continue, ROLLBACK or COMMIT:
This is the real output of one connection running those statements:
The failed insert was undone alone. Row 1 and row 2 were both committed.
Isolation Levels
| Level | What a transaction sees |
|---|---|
READ UNCOMMITTED | Even uncommitted changes of others (dirty reads), rarely used |
READ COMMITTED | Each statement sees the latest committed data |
REPEATABLE READ (default) | One snapshot for the whole transaction |
SERIALIZABLE | Reads take shared locks, most safe and slowest |
MVCC in Action: a Real Two-Session Test
InnoDB keeps old versions of rows (MVCC), so readers do not block writers. Session B reads Asha's balance, session A changes and commits it, then B reads again. The two sessions were real connections, run by a script, and this is their output. First under the default REPEATABLE READ:
B kept seeing 100 inside its transaction (a stable snapshot) and saw 70 only after it committed. Now the same steps under READ COMMITTED:
B saw the new value on the second read: a non-repeatable read. Many applications and databases (PostgreSQL's default) accept this for lower locking.
Result:
| now |
|---|
| REPEATABLE-READ |
Durability Setting
innodb_flush_log_at_trx_commit = 1 (default) flushes the redo log at every commit: safest. Value 2 flushes once per second and can lose about one second of commits on an operating system crash, in exchange for speed.
Result:
| flush_setting |
|---|
| 1 |
Common Beginner Mistakes
| Mistake | What happens | Fix |
|---|---|---|
Leaving a transaction open (forgot COMMIT) | Locks and old row versions pile up | Always end it; keep it short |
| Mixing DDL into transactions | Silent commit | Run DDL outside |
| Using MyISAM tables | No transactions | InnoDB |
| Thinking an error rolls back the whole transaction | Partial data committed | Handle errors and ROLLBACK |
| Waiting for user input inside a transaction | Other sessions blocked | Open the transaction after the input |
Practice
- Transfer money between two accounts inside a transaction and verify the total stays the same.
- Roll back a transaction and confirm nothing changed.
- Use a
SAVEPOINTto undo the last of three inserts. - Open two
mysqlwindows and repeat the isolation test.
Conclusion
Transactions give all-or-nothing changes. Use START TRANSACTION, COMMIT, ROLLBACK and SAVEPOINT, remember DDL commits implicitly, and know that InnoDB uses MVCC with REPEATABLE READ as the default.

