Nested Transactions
MySQL does not support true nested transactions. Use savepoints to create sub-transaction checkpoints within a transaction.
MySQL does not support true nested transactions. Use savepoints to create sub-transaction checkpoints within a transaction.
START TRANSACTION;
INSERT INTO orders (user_id, total) VALUES (1, 99.99);
SAVEPOINT order_created;
-- Try to reserve inventory
INSERT INTO reservations (product_id, qty) VALUES (5, 2);
-- If reservation fails:
ROLLBACK TO SAVEPOINT order_created;
-- Mark order as pending instead
UPDATE orders SET status = "pending_inventory" WHERE id = LAST_INSERT_ID();
COMMIT;
SAVEPOINT names are scoped to the current transaction — release them to free resources: RELEASE SAVEPOINT name.