Bird
0
0

You have a banking app using PostgreSQL with Serializable isolation. You want to transfer money between accounts safely. Which approach best handles serialization failures?

hard📝 Application Q15 of 15
PostgreSQL - Transactions and Concurrency

You have a banking app using PostgreSQL with Serializable isolation. You want to transfer money between accounts safely. Which approach best handles serialization failures?

-- Pseudocode
BEGIN;
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
-- debit from source account
-- credit to target account
COMMIT;

What is the best way to ensure the transfer completes reliably?

ASet isolation level to Read Committed to avoid errors.
BWrap the transaction in a retry loop that restarts on serialization failure.
CUse explicit table locks to prevent conflicts.
DIgnore serialization errors and log them only.
Step-by-Step Solution
Solution:
  1. Step 1: Recognize Need for Reliable Transaction Completion

    Serializable isolation can cause serialization failures; to handle this, retrying the transaction is necessary.
  2. Step 2: Evaluate Options for Handling Failures

    Lowering isolation risks data anomalies; explicit locks add complexity; ignoring errors risks data loss. Retrying is best practice.
  3. Final Answer:

    Wrap the transaction in a retry loop that restarts on serialization failure. -> Option B
  4. Quick Check:

    Retry loop ensures reliable serializable transactions [OK]
Quick Trick: Use retry loops to handle serialization failures safely [OK]
Common Mistakes:
  • Switching to lower isolation unsafely
  • Relying on manual locks instead of retries
  • Ignoring errors risking inconsistent data

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes