VillageSQL is a drop-in replacement for MySQL with extensions.
All examples in this guide work on VillageSQL. Install Now →
The Basics: BEGIN, COMMIT, ROLLBACK
ROLLBACK undoes everything back to the START TRANSACTION. The database is left as it was before the transaction began.
BEGIN and START TRANSACTION are equivalent. Both start a new transaction.
Autocommit
By default, MySQL runs in autocommit mode — each statement is its own transaction, committed immediately. That’s whyDELETE FROM orders WHERE id = 5 takes effect right away without an explicit COMMIT.
Calling START TRANSACTION temporarily suspends autocommit for the duration of that transaction.
COMMIT or ROLLBACK after every operation. If you disconnect without committing, MySQL rolls back any open transaction.
Isolation Levels
When multiple transactions run at the same time, isolation levels control what each transaction can see from the others. MySQL’s default isREPEATABLE READ.
| Level | Dirty Reads | Non-repeatable Reads | Phantom Reads |
|---|---|---|---|
READ UNCOMMITTED | Possible | Possible | Possible |
READ COMMITTED | Prevented | Possible | Possible |
REPEATABLE READ | Prevented | Prevented | Prevented* |
SERIALIZABLE | Prevented | Prevented | Prevented |
Savepoints
Savepoints let you roll back part of a transaction without abandoning the whole thing:What Transactions Don’t Cover
Not all MySQL statements can be rolled back. DDL statements (CREATE TABLE, ALTER TABLE, DROP TABLE) cause an implicit commit — MySQL commits any open transaction before executing them, and the DDL itself cannot be rolled back.
Frequently Asked Questions
What happens if a transaction is never committed?
If you close the connection without committing, MySQL automatically rolls back the open transaction. If the server crashes, InnoDB’s redo log recovers committed transactions and rolls back uncommitted ones on startup.Can I use transactions with MyISAM?
No. Transactions require a transactional storage engine. InnoDB (MySQL’s default engine since 5.5) fully supports transactions. MyISAM does not — statements against MyISAM tables take effect immediately and can’t be rolled back.How long can a transaction stay open?
Technically as long as needed, but long-running transactions hold locks and consume undo log space, which causes performance problems for other transactions. Theinnodb_lock_wait_timeout variable (default: 50 seconds) controls how long a transaction waits for a lock before returning an error. Keep transactions short and commit as soon as the work is done.
Troubleshooting
| Problem | Solution |
|---|---|
| Changes aren’t persisting | Check if autocommit is off — you may need to call COMMIT explicitly |
ROLLBACK has no effect | A DDL statement (CREATE, ALTER, DROP) caused an implicit commit mid-transaction |
| Transaction is blocking other queries | A long-running transaction is holding locks — commit or roll back promptly; check SHOW ENGINE INNODB STATUS for lock info |
ERROR 1205: Lock wait timeout exceeded | Another transaction holds a conflicting lock; retry after the blocking transaction completes |

