Quick revision
The short answer
The error is not a regression; it is snapshot isolation doing its job, because the job tried to update rows that another transaction changed and committed after the job's snapshot was taken. I would not go back to READ COMMITTED for everything. Either split the job so the report runs as a read-only REPEATABLE READ transaction and the status updates run in small READ COMMITTED transactions with conditional updates, or keep REPEATABLE READ and retry each short transaction from the start.
What to remember
In this answer, we saw that the could not serialize access error is snapshot isolation refusing to update a row changed after the job's snapshot, which is likely in a long nightly job. The best fix is usually to split the job: the report as a read-only REPEATABLE READ transaction and the updates as small READ COMMITTED transactions with conditional updates. Keeping REPEATABLE READ with retries also works if transactions are short. Going back to READ COMMITTED everywhere gives up the consistent report, and in Spring the retry must wrap the whole @Transactional call from outside.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The error is not a regression; it is snapshot isolation doing its job, because the job tried to update rows that another transaction changed and committed after the job's snapshot was taken. I would not go back to READ COMMITTED for everything. Either split the job so the report runs as a read-only REPEATABLE READ transaction and the status updates run in small READ COMMITTED transactions with conditional updates, or keep REPEATABLE READ and retry each short transaction from the start.
Why this question is asked
Teams often move to a stronger isolation level for one reason and then meet a side effect they did not expect. Here the reason was a consistent report, and the side effect is serialization failures on writes. The tempting reaction is to undo the change, which removes the error by removing the protection that caused it. Interviewers ask this to see whether we understand why the error happens and can design the job so both needs are met. In this answer, we will explain the error, look at two good options, discuss what going back would cost, and see how to write the retry correctly in Spring.
1Why the Error Appears
At REPEATABLE READ, PostgreSQL gives the transaction one snapshot, taken at its first statement, and every read uses it. That is exactly what made the report consistent. But when the transaction later tries to UPDATE a row, PostgreSQL checks whether a transaction that committed after the snapshot already changed that row. If so, it cannot apply our update without either ignoring that change or acting on data our snapshot does not show, so it aborts with ERROR: could not serialize access due to concurrent update, SQLSTATE 40001.
A nightly job is the worst case for this. Its transaction runs long, so its snapshot gets old, and during that time the online application keeps updating the same orders. The longer the job runs before it writes, the more rows have changed under it, which is why the failure is intermittent: it depends on whether a customer touched one of those orders during the job.
4 messages in order. Play them, or step through them one at a time.
2Option 1: Split the Job by What Each Part Needs
The report and the updates want different things. The report needs one consistent picture and never writes. The updates need fresh data and safety against overwriting the online application's changes. So we give each its own transactions.
A read-only transaction never writes, so it can never get this error, and its snapshot gives the report its consistency. The updates run in batches of, say, 500 rows, each in its own short transaction. The condition in the WHERE clause, status = 'SHIPPED', is the important part: at READ COMMITTED, PostgreSQL re-checks it against the latest committed row, so if the online application already moved an order to CANCELLED, our update simply skips it instead of overwriting it. The affected row count tells us how many we changed. This is usually the best design for a batch job.
3Option 2: Keep REPEATABLE READ and Retry
If the updates really must be based on the same snapshot as the report, we keep REPEATABLE READ and treat 40001 as a normal outcome. The failed transaction is rolled back and run again from the start; its new snapshot shows the online change, and its logic decides again. For this to be practical the transactions must be short: retrying a 40-minute transaction is not a plan, but retrying a two-second batch is. So we still split the work into many small transactions and retry each one a bounded number of times.
4What Going Back to READ COMMITTED Costs
Going back for everything removes the error, but it also removes the consistent report the team moved for in the first place: each query would see a slightly different database. And it can reintroduce lost updates if the job reads a status in Java and then writes an absolute value. The error was the database telling us that the job was about to act on stale data. Silencing it might be acceptable, but it should be a conscious decision recorded in the job's design, not a reflex.
5Where the Retry Belongs in Spring
In Spring, SQLSTATE 40001 from PostgreSQL is translated into a ConcurrencyFailureException subtype, typically CannotAcquireLockException or PessimisticLockingFailureException depending on your version, so we catch ConcurrencyFailureException. The retry must wrap the whole @Transactional call from outside, through the proxy.
Notice that the loop lives in a different bean from the @Transactional method. If the loop were inside the transactional method, every attempt would run in the same, already failed transaction.
6Follow-up questions
Why must the retry not happen inside the same transaction?
After an error, PostgreSQL marks the transaction as aborted: every further statement fails with current transaction is aborted, commands ignored until end of transaction block. Even if it did not, the transaction would still have the same old snapshot, so the retry would see the same stale data and fail the same way. Only a new transaction gets a new snapshot. In Spring, that means the loop must call the @Transactional method through its proxy, from another bean or with TransactionTemplate, so each attempt starts, and rolls back, a transaction of its own.
Would SERIALIZABLE help here?
Not with this error. SERIALIZABLE includes the same write-conflict check, so it would fail the same way, plus aborts for read-write dependencies. It helps when the problem is write skew, not here.
Does a long read-only report transaction have any cost?
It cannot hit write conflicts, but it holds its snapshot for as long as it runs, and VACUUM cannot remove row versions that snapshot might still need. A report of a few minutes is harmless; one that runs for hours on a busy primary can cause bloat, so very long reports are better run on a replica or from an exported snapshot.
7Conclusion
In this answer, we saw that the could not serialize access error is snapshot isolation refusing to update a row changed after the job's snapshot, which is likely in a long nightly job. The best fix is usually to split the job: the report as a read-only REPEATABLE READ transaction and the updates as small READ COMMITTED transactions with conditional updates. Keeping REPEATABLE READ with retries also works if transactions are short. Going back to READ COMMITTED everywhere gives up the consistent report, and in Spring the retry must wrap the whole @Transactional call from outside.
Open with
I'd push back on going back to READ COMMITTED. The error is expected snapshot isolation behaviour: the job's transaction took its snapshot at the start, the online app changed and committed some of the same orders during the run, and when the job tried to update those rows PostgreSQL refused, because applying the update would mean acting on data the snapshot doesn't show.
Then, if they want more
It's intermittent because it depends on whether a customer touched those orders during the job. My first recommendation is to split the job. The report runs in one REPEATABLE READ, read-only transaction, which can never get this error and stays consistent.
The status updates run in small READ COMMITTED batches with conditional updates, like where status = 'SHIPPED', so PostgreSQL re-checks the latest row and we skip anything the app already changed, and the row count tells us what happened. The alternative is to keep REPEATABLE READ and retry each short transaction from the start.
Going back to READ COMMITTED everywhere loses the consistent report and can bring back lost updates, so that should be a deliberate choice. In Spring the error arrives as a ConcurrencyFailureException subtype, and the retry has to wrap the whole transactional call from another bean, never run inside it.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "After moving a batch job's transactions to PostgreSQL REPEATABLE READ to get consistent...": https://scrivio-interview-library.netlify.app/databases/acid-and-isolation/f695f1e920/