Sub topic 7
Setting the Isolation Level in Practice
This chapter assumes that you know basic SELECT and UPDATE statements, can use BEGIN, COMMIT, and ROLLBACK, and understand that several clients can use the same database at once.
An isolation level controls what a transaction can observe while other transactions run. A stronger level can prevent more concurrency anomalies, but it can also increase waiting, conflicts, or retries. Choose the level for the transaction's correctness needs, not as a global setting by habit.
Choose the scope first
You can set an isolation level for one transaction, for a session, or sometimes for the whole database. A session is one connection between an application and the database. A transaction is the group of statements between BEGIN and COMMIT or ROLLBACK.
Prefer the smallest scope that expresses your requirement. A report may need a stable snapshot, while a simple log insert may work with the default level.
flowchart TD
A[Application opens a database session] --> B[Choose isolation scope]
B --> C[One transaction]
B --> D[Session]
B --> E[Database default]
C --> F[Run statements]
D --> G[Future transactions in this session]
E --> H[Connections inherit the default]
F --> I[COMMIT or ROLLBACK]The setting is usually not a substitute for a business constraint. A unique constraint, a check constraint, or an atomic UPDATE can often solve one specific race more cheaply than raising the isolation level for every statement.
PostgreSQL
PostgreSQL lets you choose the isolation level when starting a transaction. The setting applies to that transaction only.
BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
SELECT balance
FROM accounts
WHERE id = 1;
-- More statements that should use the same snapshot
COMMIT;You can also set the level after BEGIN, before the transaction has performed a statement:
BEGIN;
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
UPDATE accounts
SET balance = balance - 10
WHERE id = 1;
COMMIT;PostgreSQL's READ COMMITTED is the default. Each statement gets a snapshot of data committed before that statement began. REPEATABLE READ keeps a transaction-wide snapshot. SERIALIZABLE adds checks that can reject transactions whose combined behavior would be impossible in a serial order.
MySQL InnoDB
For MySQL, the following command sets the level for the next transaction in the current session:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
START TRANSACTION;
SELECT balance
FROM accounts
WHERE id = 1;
COMMIT;To set the default for future transactions in the current session, use SESSION:
SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;A connection pool can reuse sessions. Therefore, an isolation setting left by one request may affect a later request that receives the same connection. Reset session settings when returning connections to the pool, or set the required level explicitly at transaction start.
SQL Server
SQL Server uses a statement that changes the isolation level for the current session. The setting remains until the session changes it again.
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
BEGIN TRANSACTION;
SELECT balance
FROM accounts
WHERE id = 1;
COMMIT TRANSACTION;SQL Server also supports row-versioning options such as READ_COMMITTED_SNAPSHOT and SNAPSHOT. These are database configuration choices, not just transaction-level syntax. They can change how READ COMMITTED behaves, so check the database configuration before assuming that readers always take shared locks.
A practical selection process
- Start with the database default.
- Identify the invariant that concurrent work must preserve.
- Try an atomic statement, constraint, or targeted lock first.
- Raise the isolation level only when the whole transaction needs stronger guarantees.
- Add retry handling for serialization failures and deadlocks.
- Test with two or more concurrent sessions, not only with sequential tests.
Exercise
A PostgreSQL transaction must read several rows consistently while another transaction may update them. Which level should you consider first, and where should you set it?
Show answer
Consider REPEATABLE READ and set it at transaction start with BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ. Use SERIALIZABLE instead when the transaction must also prevent serialization anomalies such as write skew.