Double-spend prevention in wallet
SQL
Hard
4 views
Problem Description
Wallet(user_id, balance). Two payments at same time should not allow negative balance. Write a safe transaction with check.
Sample Test Case
Output:
No negative balance
Constraints
Lock row before update
Official Solution
BEGIN; SELECT balance FROM Wallet WHERE user_id=101 FOR UPDATE; UPDATE Wallet SET balance = balance - 800 WHERE user_id=101 AND balance >= 800; COMMIT;
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!