Use savepoint in a business transaction
SQL
Medium
2 views
Problem Description
While placing an order, keep a savepoint after reserving stock. If payment fails, rollback to savepoint.
Sample Test Case
Input:
Inventory, Payments
Constraints
Use savepoint
Official Solution
BEGIN; UPDATE Inventory SET qty = qty - 2 WHERE product_id=10 AND qty >= 2; SAVEPOINT stock_reserved; INSERT INTO Payments(order_id, amount, paid_at) VALUES (7001, 1200, CURRENT_TIMESTAMP); ROLLBACK TO stock_reserved; COMMIT;
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!