SyntaxStudy
Sign Up
MySQL Transaction Best Practices
MySQL Intermediate 4 min read

Transaction Best Practices

Best Practices

Keep transactions short, lock as few rows as possible, always handle rollback in application code, and never call external services inside a transaction.

Example
-- Bad: transaction holds locks while waiting for external API
START TRANSACTION;
UPDATE orders SET status = "processing" WHERE id = 101;
-- ← Never call Stripe/email/third-party API here!
COMMIT;

-- Good: update inside transaction, call API outside
START TRANSACTION;
UPDATE orders SET status = "processing" WHERE id = 101;
COMMIT;
-- Then call API after commit
callStripeAPI(101);
Pro Tip

Long-held locks = reduced concurrency. Commit as soon as the DB work is done.