Summary: MySQL transactions let you group database operations so they all succeed or all fail. Use BEGIN; to start, run your queries, then COMMIT; to save or ROLLBACK; to undo. This keeps your data consistent. InnoDB is the default engine that supports transactions.
What’s Happening
A MySQL transaction isn’t some mysterious process—it’s just a way to bundle multiple database operations together. Think of it like a financial transfer: you wouldn’t want $100 to leave your account without landing in someone else’s. That’s exactly what transactions prevent. They follow the ACID rules—atomicity, consistency, isolation, and durability—which means either all changes are saved together or none are. As of 2026, MySQL’s default storage engine, InnoDB, fully supports ACID transactions, unlike the older MyISAM engine which doesn’t MySQL InnoDB Storage Engine.
How do I start a transaction?
BEGIN; or START TRANSACTION; in your MySQL client.No magic incantations here—just run BEGIN; or START TRANSACTION; in your MySQL client. This temporarily turns off autocommit, so your changes won’t stick until you explicitly say so with COMMIT; or ROLLBACK;. It’s like putting your database in “hold on” mode while you work.
What happens during a transaction?
Here’s the thing: your queries run exactly as they would outside a transaction. The difference is that nothing becomes permanent until you call COMMIT;. Until then, other connections can’t see your changes (depending on isolation settings). It’s like editing a document in a shared folder—others can’t see your edits until you save and share them.
How do I complete a transaction?
COMMIT; to save changes or ROLLBACK; to undo them.Once you’re satisfied everything worked as expected, run COMMIT; to make those changes permanent. All locks get released, and other sessions can finally see the new data. But if something went wrong—maybe a calculation error or a failed query—just run ROLLBACK; instead. It’s like hitting “undo” on your entire set of changes.
Can I see an example of a real transaction?
Let’s say you need to move $100 from account 1 to account 2. Without a transaction, a server crash halfway through could leave account 1 $100 poorer and account 2 unchanged. Nasty, right?
- Start the transaction:
BEGIN; - Withdraw $100 from account 1:
Command Description UPDATE accounts SET balance = balance – 100 WHERE id = 1;Withdraw $100 - Deposit $100 into account 2:
Command Description UPDATE accounts SET balance = balance + 100 WHERE id = 2;Deposit $100 - Check the balances look correct
- Commit the changes:
COMMIT;
Now both accounts update together—or not at all if something fails.
What if my transaction doesn’t work?
First, don’t panic. Transactions fail for predictable reasons, and most fixes are simple. Start by checking autocommit—if it’s turned on, each statement commits immediately, which defeats the whole purpose. Run SELECT @@autocommit;; if it returns 1, disable it with SET autocommit = 0;.
Next, confirm your table actually uses InnoDB. Run SELECT TABLE_NAME, ENGINE FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'your_db';. If you see MyISAM anywhere, switch it: ALTER TABLE your_table ENGINE=InnoDB; MySQL ALTER TABLE.
Finally, locking issues or strange read results? Check your isolation level with SELECT @@transaction_isolation;. You can tweak it with SET SESSION transaction_isolation = 'REPEATABLE-READ'; MySQL Isolation Levels.
How do I check if autocommit is enabled?
SELECT @@autocommit;—a result of 1 means it’s on; 0 means it’s off.It’s one of those simple checks that saves hours of frustration. Just execute SELECT @@autocommit; in your MySQL client. If it returns 1, autocommit is active, which means every single statement commits immediately. That’s usually not what you want for multi-step operations. Turn it off with SET autocommit = 0; and you’ll be back in control.
How do I confirm my table uses InnoDB?
SELECT TABLE_NAME, ENGINE FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'your_db'; to check your table engine.This is one of those “better safe than sorry” checks. Run that query, and you’ll get a list of all your tables along with their storage engines. If you spot any MyISAM entries, it’s time for an upgrade. Switch them to InnoDB with ALTER TABLE your_table ENGINE=InnoDB; MySQL ALTER TABLE. Honestly, this is the best approach for any modern application.
How do I change the isolation level?
SELECT @@transaction_isolation; and adjust with SET SESSION transaction_isolation = 'level_name';Isolation levels control how much your transaction can “see” of other transactions’ changes. To check your current setting, run SELECT @@transaction_isolation;. If you need to change it, use SET SESSION transaction_isolation = 'REPEATABLE-READ'; or another level like READ COMMITTED or SERIALIZABLE MySQL Isolation Levels.
Just remember: higher isolation means more locking, which can slow things down. Lower isolation means you might see inconsistent reads. Pick what fits your needs.
What are the best practices for using transactions?
First rule: always use InnoDB. It’s been the default since MySQL 5.5 (released in 2010), and it’s the only engine that reliably supports transactions MySQL Storage Engines. Next, wrap your multi-step operations in explicit BEGIN; and COMMIT; so a failure doesn’t leave partial data in the database.
Keep transactions short—if one runs longer than 5 seconds, review its logic. Long transactions hog locks and can cause deadlocks. And if you’re just running reports? Use read-only transactions. Add READ ONLY after START TRANSACTION READ ONLY; to minimize overhead.
Why should I use InnoDB for transactions?
Look, the older MyISAM engine doesn’t support transactions at all. That means no atomicity, no rollbacks, no safety net. InnoDB, on the other hand, handles all the ACID properties reliably. It’s been the default since MySQL 5.5, so there’s really no excuse not to use it. If you’re doing anything serious with transactions, InnoDB is your only real option MySQL Storage Engines.
How long should a transaction last?
Here’s the thing: the longer a transaction runs, the more it locks resources. Other connections have to wait, which can slow down your entire application. If you find yourself with a transaction running longer than 5 seconds, it’s time to optimize. Break it into smaller chunks or review your logic. Short transactions are happy transactions.
What’s a read-only transaction?
If you’re running reports or analytics and don’t need to change anything, use a read-only transaction. Just add READ ONLY after START TRANSACTION READ ONLY;. This tells MySQL you won’t be making changes, so it can skip some of the locking overhead. It’s a small tweak that can make a noticeable difference in performance.
How do I handle errors in transactions?
ROLLBACK; to undo all changes if any step fails.Errors happen. Maybe a constraint fails, maybe a connection drops. Whatever the reason, if something goes wrong during your transaction, don’t try to fix it mid-transaction. Just run ROLLBACK; and start over. It’s like hitting “cancel” on a form you’ve been filling out—everything resets, and you can try again with a clean slate.
