sql / intermediate
Snippet
Enforcing Serializable Isolation Level for Concurrent Transactions
The SERIALIZABLE transaction isolation level is the strictest isolation level defined by ANSI SQL. It guarantees complete transaction isolation by preventing phantom reads, non-repeatable reads, and dirty reads, ensuring operations execute as if they occurred sequentially.
snippet.sql
sql
1
2
3
4
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ WRITE;START TRANSACTION;UPDATE accounts SET balance = balance - 100 WHERE account_id = 42;COMMIT;
Breakdown
1
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ WRITE;
Configures the upcoming transaction isolation level to SERIALIZABLE in read-write mode.
2
START TRANSACTION;
Initiates an explicit transaction block.
3
UPDATE accounts SET balance = balance - 100 WHERE account_id = 42;
Modifies the target account balance within the protected transaction context.
4
COMMIT;
Persists all changes made within the transaction to the database.