MySQL Transactions: COMMIT, ROLLBACK, SAVEPOINT and Isolation Levels

MySQL 70 min min read Updated: Nov 27, 2026 Intermediate
MySQL Transactions: COMMIT, ROLLBACK, SAVEPOINT and Isolation Levels
Intermediate Topic 1 of 3

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.

ACIDMeaning
AtomicityAll or nothing
ConsistencyConstraints are never broken by a committed transaction
IsolationConcurrent transactions do not see each other's half-done work
DurabilityA 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.

sql SELECT @@autocommit AS autocommit, @@transaction_isolation AS isolation;

Result:

autocommitisolation
1REPEATABLE-READ

COMMIT and ROLLBACK

sql CREATE TABLE accounts (id INT PRIMARY KEY, owner VARCHAR(10), balance INT NOT NULL) ENGINE=InnoDB; INSERT INTO accounts VALUES (1, 'Asha', 100), (2, 'Ben', 100); START TRANSACTION; UPDATE accounts SET balance = balance - 30 WHERE id = 1; UPDATE accounts SET balance = balance + 30 WHERE id = 2; COMMIT; SELECT id, owner, balance FROM accounts ORDER BY id;

Result:

idownerbalance
1Asha70
2Ben130
sql CREATE TABLE accounts (id INT PRIMARY KEY, owner VARCHAR(10), balance INT NOT NULL) ENGINE=InnoDB; INSERT INTO accounts VALUES (1, 'Asha', 100), (2, 'Ben', 100); START TRANSACTION; UPDATE accounts SET balance = balance - 30 WHERE id = 1; SELECT balance AS asha_inside_transaction FROM accounts WHERE id = 1; ROLLBACK; SELECT balance AS asha_after_rollback FROM accounts WHERE id = 1;

Result:

asha_inside_transaction
70

Result:

asha_after_rollback
100

SAVEPOINT: Undo Part of a Transaction

sql CREATE TABLE log (msg VARCHAR(20)) ENGINE=InnoDB; START TRANSACTION; INSERT INTO log VALUES ('step 1'); SAVEPOINT after_step1; INSERT INTO log VALUES ('step 2'); ROLLBACK TO SAVEPOINT after_step1; INSERT INTO log VALUES ('step 3'); COMMIT; SELECT msg FROM log ORDER BY msg;

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:

sql CREATE TABLE t (id INT) ENGINE=InnoDB; START TRANSACTION; INSERT INTO t VALUES (1); CREATE TABLE other (x INT); ROLLBACK; SELECT COUNT(*) AS rows_kept_after_rollback FROM t;

Result:

rows_kept_after_rollback
1
MySQL is different from PostgreSQL here In PostgreSQL, DDL is transactional. In MySQL, a DDL statement commits whatever the transaction did before it. Keep DDL out of business transactions.

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:

text START TRANSACTION -> OK INSERT INTO t VALUES (1) -> OK INSERT INTO t VALUES (1) -> ERROR 1062 Duplicate entry '1' for key 't.PRIMARY' INSERT INTO t VALUES (2) -> OK COMMIT -> OK SELECT id FROM t ORDER BY id -> 1, 2

The failed insert was undone alone. Row 1 and row 2 were both committed.

Isolation Levels

LevelWhat a transaction sees
READ UNCOMMITTEDEven uncommitted changes of others (dirty reads), rarely used
READ COMMITTEDEach statement sees the latest committed data
REPEATABLE READ (default)One snapshot for the whole transaction
SERIALIZABLEReads 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:

text B START TRANSACTION B SELECT balance FROM accounts WHERE id = 1 -> [[100]] A UPDATE accounts SET balance = 70 WHERE id = 1 B SELECT balance FROM accounts WHERE id = 1 -> [[100]] B COMMIT B SELECT balance FROM accounts WHERE id = 1 -> [[70]]

B kept seeing 100 inside its transaction (a stable snapshot) and saw 70 only after it committed. Now the same steps under READ COMMITTED:

text B START TRANSACTION B SELECT balance FROM accounts WHERE id = 1 -> [[100]] A UPDATE accounts SET balance = 70 WHERE id = 1 B SELECT balance FROM accounts WHERE id = 1 -> [[70]] B COMMIT B SELECT balance FROM accounts WHERE id = 1 -> [[70]]

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.

sql SELECT @@transaction_isolation AS now;

Result:

now
REPEATABLE-READ
sql SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; -- this connection only SET GLOBAL TRANSACTION ISOLATION LEVEL READ COMMITTED; -- new connections (needs privilege)

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.

sql SELECT @@innodb_flush_log_at_trx_commit AS flush_setting;

Result:

flush_setting
1

Common Beginner Mistakes

MistakeWhat happensFix
Leaving a transaction open (forgot COMMIT)Locks and old row versions pile upAlways end it; keep it short
Mixing DDL into transactionsSilent commitRun DDL outside
Using MyISAM tablesNo transactionsInnoDB
Thinking an error rolls back the whole transactionPartial data committedHandle errors and ROLLBACK
Waiting for user input inside a transactionOther sessions blockedOpen the transaction after the input

Practice

  1. Transfer money between two accounts inside a transaction and verify the total stays the same.
  2. Roll back a transaction and confirm nothing changed.
  3. Use a SAVEPOINT to undo the last of three inserts.
  4. Open two mysql windows 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.

Quick Summary: autocommit, COMMIT, ROLLBACK, SAVEPOINT, ACID, MVCC, isolation levels.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators