Quick revision
The short answer
The transaction interceptor is running outside the retry advice, so one transaction surrounds every attempt: the first failure marks it rollback-only and each retry runs inside that doomed transaction. The fix is to give the retry advice higher precedence, a lower order value, than the transaction advisor, for example by setting @EnableTransactionManagement(order = ...) to a larger number or the retry aspect's @Order to a smaller one. Then we prove it with a test in which the second attempt starts a new transaction and commits.
What to remember
In this answer, we saw why retries reused a rollback-only transaction: the transaction interceptor was the outer layer, so every attempt ran inside the transaction started before the retry loop. Giving the retry advice higher precedence, with a lower @Order value or a larger order on @EnableTransactionManagement, puts the transaction inside the loop so each attempt gets a new one. We also noted that the retry must be the outermost transactional boundary, and we added a test that records how each attempt's transaction ends to keep the fix in place.
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 transaction interceptor is running outside the retry advice, so one transaction surrounds every attempt: the first failure marks it rollback-only and each retry runs inside that doomed transaction. The fix is to give the retry advice higher precedence, a lower order value, than the transaction advisor, for example by setting @EnableTransactionManagement(order = ...) to a larger number or the retry aspect's @Order to a smaller one. Then we prove it with a test in which the second attempt starts a new transaction and commits.
Why this question is asked
Retrying a database operation after a deadlock or an optimistic locking failure is a common and sensible pattern. It only works if each attempt starts from a clean transaction, and that depends entirely on the order in which two proxies' advice wraps the method. When the order is wrong, the symptom is confusing: the retry appears to run, the second attempt seems to succeed, and then the commit fails with an UnexpectedRollbackException. Interviewers ask this to see whether we can reason about the interceptor chain rather than adding more retries. In this answer, we will recap rollback-only, reproduce the failure, see why the order causes it, fix it, and write a test that proves each attempt gets its own transaction.
1A Quick Recap of Rollback-Only
With @Transactional, Spring's transaction interceptor starts a transaction before the method and commits or rolls back after it. When an exception that triggers rollback, by default any RuntimeException, passes through a transactional boundary, Spring marks the transaction rollback-only. That flag means: whatever happens next, this transaction can no longer commit.
This matters when transactions nest with the default propagation, REQUIRED, which joins an existing transaction instead of starting a new one. If an inner participant fails, it cannot roll back alone, so it marks the shared transaction rollback-only. When the outer code later tries to commit, Spring finds the flag, rolls back, and throws UnexpectedRollbackException to tell the caller that its commit did not happen.
2Reproducing the Problem
Here is a service whose method is both retried and transactional. The retry comes from a custom aspect, but the same happens with any retry interceptor. Both advisors use the default order, so their relative order is not what we need.
The exact exception text can differ between versions, but the shape is always the same: the retry loop swallowed the first failure, yet the transaction around the loop remembers it.
3Why the Order Makes Every Attempt Share One Transaction
Both the retry aspect and the transaction interceptor sit in the same proxy chain around reserve(). Whichever has higher precedence is the outer layer. In our failing case the transaction interceptor ended up outside, so the chain looks like this: start a transaction, then enter the retry loop, then call the method up to three times, then commit. Every attempt runs inside the one transaction that was started before the loop, and the first failure already poisoned it.
It is worth asking why the transaction is rollback-only at all, since the retry loop catches the exception before the outer transaction interceptor sees it. The reason is that the exception passed through something transactional on its way out: a Spring Data repository method, or any inner @Transactional bean, joins the transaction and marks it rollback-only when an exception leaves it. JPA providers also mark the transaction for rollback themselves on most persistence exceptions, including an optimistic lock failure. So by the time the retry catches the exception, the shared transaction is already doomed.
What we want is the opposite nesting: the retry loop outside, and the transaction inside it. Then each call to proceed() enters the transaction interceptor afresh, which starts a new transaction for that attempt, and a failure rolls back only that attempt's work.
| Outer layer | Inner layer | What each retry gets |
|---|---|---|
| Transaction interceptor | Retry advice | The same transaction, already rollback-only |
| Retry advice | Transaction interceptor | A brand new transaction per attempt |
4Fixing the Order
The transaction advisor's order is set on @EnableTransactionManagement. Its default is Ordered.LOWEST_PRECEDENCE. Spring Boot enables transaction management automatically, but if we declare @EnableTransactionManagement ourselves on a configuration class, Boot backs off and uses ours. We then give our retry aspect a lower order value so it becomes the outer layer.
Either side can move. If we would rather not touch the transaction configuration, giving the retry aspect an order such as Ordered.LOWEST_PRECEDENCE - 1 also puts it outside, because the transaction advisor stays at the lowest precedence. What matters is the relation between the two values, and writing both down as named constants avoids someone changing one of them later without noticing.
If the retry comes from a library instead of our own aspect, the library usually exposes its own order setting. Spring Retry's @EnableRetry has an order attribute, and Spring Framework 7 adds its own @Retryable support; their defaults differ between versions, so check your version and set the value explicitly rather than rely on it.
There is one more trap. Even with the right order, retries do not help if reserve() is called from a method that already runs in a transaction, because REQUIRED makes each attempt join the caller's transaction again. The retry boundary must be the outermost transactional boundary, or the inner method must use REQUIRES_NEW deliberately.
5Proving That Each Attempt Gets a New Transaction
A fix to ordering is easy to undo by accident, so we lock it in with a test. The idea is to make the first attempt fail and record how each attempt's transaction ends. Counting calls to the transaction manager is tempting but misleading, because repository methods that join the transaction call it too. A TransactionSynchronization is more precise: it is a callback registered on the current transaction, and its afterCompletion method runs once, when the real physical transaction commits or rolls back.
With the wrong order, both callbacks belong to the same transaction, so they report the same status twice, and the test fails with a clear message. The test also documents the intended nesting for the next reader, which is worth as much as the check itself.
6Follow-up questions
Why not just catch the exception inside the transactional method and retry there?
Because the transaction is already marked rollback-only once the exception passes through any transactional boundary, and even if it did not, the persistence context still holds the stale entity that caused the conflict. A retry inside the same transaction would re-read cached state and fail again. A clean retry needs a new transaction and a new persistence context, which is exactly what the outer retry gives us.
Which exceptions should a database retry catch?
Only transient ones that a fresh attempt can succeed at, such as optimistic locking failures, deadlocks and serialization failures, which Spring translates into exceptions like OptimisticLockingFailureException and other subclasses of TransientDataAccessException or PessimisticLockingFailureException. Retrying a constraint violation or a validation error just repeats the failure three times.
How can I see the actual order of the interceptors on a bean?
In a test or debugger we can cast the bean to Advised and call getAdvisors(), which returns the advisors in chain order, outermost first. Logging that list once at startup in a test makes ordering problems obvious.
7Conclusion
In this answer, we saw why retries reused a rollback-only transaction: the transaction interceptor was the outer layer, so every attempt ran inside the transaction started before the retry loop. Giving the retry advice higher precedence, with a lower @Order value or a larger order on @EnableTransactionManagement, puts the transaction inside the loop so each attempt gets a new one. We also noted that the retry must be the outermost transactional boundary, and we added a test that records how each attempt's transaction ends to keep the fix in place.
Open with
The symptom tells me the nesting is wrong. The transaction interceptor is running outside the retry advice, so a single transaction starts before the retry loop. The first attempt throws, say an optimistic locking failure, and Spring marks that transaction rollback-only.
Then, if they want more
The retry then calls the method again inside the same transaction, the attempt succeeds, but the commit fails with an UnexpectedRollbackException. Both are advisors in the same proxy chain, and the one with the lower order value is the outer layer. So the fix is to give the retry advice higher precedence than the transaction advisor.
The transaction advisor defaults to the lowest precedence; I can declare EnableTransactionManagement with an explicit order like 200 and put Order 100 on the retry aspect, or if the retry comes from a library I set its order attribute explicitly, because defaults vary by version.
Then every proceed call goes through the transaction interceptor again and each attempt gets a fresh transaction and persistence context. I would also check that the method is not called from an outer transaction, because with REQUIRED the attempts would just join that one again.
Finally I would write a test that fails the first attempt and records how each attempt's transaction ends, expecting one rollback followed by one commit, so a later reordering breaks the build instead of production.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Framework 7
- 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
- Spring Framework reference, Core technologies
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 "A retry aspect and @Transactional apply to the same method, but retries reuse a...": https://scrivio-interview-library.netlify.app/spring-boot/aop/25e0f137e9/