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.
Keep transactions short, lock as few rows as possible, always handle rollback in application code, and never call external services inside a transaction.
-- 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);
Long-held locks = reduced concurrency. Commit as soon as the DB work is done.