Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

A @Transactional outer method calls inner() on another bean, also @Transactional (REQUIRED). inner() throws a RuntimeException, which the outer method catches and logs. At commit the outer method fails with UnexpectedRollbackException: Transaction silently rolled back because it has been marked as rollback-only. Explain.

Because inner() uses REQUIRED, it joins the outer method's transaction, so there is only one physical transaction. When the RuntimeException passes through inner()'s proxy, Spring marks that shared transaction rollback-only; catching the exception in outer() does not clear the mark, so when outer() tries to commit, Spring rolls back instead and throws UnexpectedRollbackException to tell us the commit we asked for did not happen. The fix depends on intent: REQUIRES_NEW if inner()'s work is independent, no exception or noRollbackFor on inner() if the failure is expected, or let the exception propagate and fail the whole operation.

Why this question is asked

This error confuses people because the code looks correct: the exception is caught, logged and handled, and yet the method fails at the very end with an exception that was never thrown by our code. Interviewers like it because the explanation requires understanding that two @Transactional methods can share one transaction, and that an inner method has no way to roll back only its own part. In this answer, we will reproduce the problem, follow what happens inside Spring step by step, see why the exception is thrown at commit, and then go through the fixes and when each one is right.

1Reproducing the Problem

Here are the two beans. outer() saves an order and calls inner() on another bean, which fails. outer() catches the exception because it considers the loyalty points optional.

Java
@Service
class OrderService {
private final LoyaltyService loyalty;
@Transactional
public void outer(Order o) {
orders.save(o);
try {
loyalty.inner(o); // throws IllegalStateException
} catch (IllegalStateException e) {
log.warn("points not added", e); // handled, we think
}
} // commit attempted here
}
@Service
class LoyaltyService {
@Transactional // REQUIRED: joins outer's transaction
public void inner(Order o) {
points.save(new Points(o.customerId(), 10));
throw new IllegalStateException("customer blocked");
}
}
// org.springframework.transaction.UnexpectedRollbackException:
// Transaction silently rolled back because it has been marked as rollback-only
// The order is NOT saved.

Notice that the order is lost too, even though outer() clearly wanted to keep it. That is the important consequence, more than the exception itself.

2Both Methods Share One Physical Transaction

With the default propagation REQUIRED, when inner() is called and a transaction is already active on the thread, Spring does not start a new one; inner() joins it. So there is one physical transaction, one database connection and one commit, and two logical scopes, one per method.

Only the outermost scope, the one that started the transaction, can actually commit or roll it back. The inner scope can only take part: when it finishes normally, it does nothing, and when it fails, it can only signal its failure to the transaction they share.

3Why the Transaction Is Marked Rollback-Only

When inner() throws the IllegalStateException, the exception travels out through inner()'s proxy. The TransactionInterceptor there sees a RuntimeException, which matches the default rollback rules, so it asks the transaction manager to roll back. But inner() is only a participant. The manager cannot roll back the physical transaction from here, because the outer method is still running, so it does the only thing it can: it sets a rollback-only flag on the shared transaction. (This behaviour is controlled by the manager's globalRollbackOnParticipationFailure setting, which is true by default.)

Then the exception continues to outer(), which catches it. Catching an exception is plain Java; it does not reach into Spring's transaction state, so the flag stays set.

When outer() returns normally, its interceptor calls commit(). The manager sees the rollback-only flag, so it rolls back instead. Because the caller asked for a commit and got a rollback without any exception from its own code, Spring throws UnexpectedRollbackException rather than pretending the commit succeeded. Silently losing the order would be far worse than this error.

Step throughHow the commit turns into a rollback
outer() proxyOrderServiceinner() proxyLoyaltyServiceTx manager
begin transaction
outer(o)
inner(o)
join existing transaction
inner(o)
throws IllegalStateException
participant failed: set rollback-only
rethrows; outer catches it
returns normally
commit()
rolled back: UnexpectedRollbackException

11 messages in order. Play them, or step through them one at a time.

The inner proxy cannot roll back alone, so it marks the shared transaction.

4Fix 1: REQUIRES_NEW When inner() Is Independent

If adding loyalty points really is a separate unit of work whose failure should not affect the order, give it its own transaction. With REQUIRES_NEW, Spring suspends outer()'s transaction and runs inner() in a new one. When inner() fails, only its own transaction rolls back, and outer()'s transaction is untouched.

Java
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void inner(Order o) { ... }
// outer(): order COMMITTED, points rolled back, warning logged, no exception

The cost is a second connection while inner() runs, and the opposite consistency trade-off: if inner() succeeds and outer() later fails, the points stay committed. That is only right if they are truly independent.

5Fix 2: Do Not Treat an Expected Outcome as a Failure

If 'customer blocked' is a normal business outcome, it should not be a rollback-causing exception at all. inner() can return a result instead, or the check can happen before any write. If we want to keep the exception, noRollbackFor on inner() tells its interceptor not to mark the transaction.

Java
@Transactional(noRollbackFor = CustomerBlockedException.class)
public void inner(Order o) { ... } // the exception passes through without the rollback-only mark
// Better still, check first and avoid the exception:
if (loyalty.canEarnPoints(o.customerId())) loyalty.inner(o);

Be careful with noRollbackFor: any points row inner() saved before throwing would now be committed with the order. Checking first is usually cleaner.

6Fix 3: Let It Fail

Sometimes the right answer is that the order should not be saved if inner() fails. Then the try/catch is the bug: remove it and let the exception propagate, so outer() rolls back with the real exception visible to the caller instead of an UnexpectedRollbackException. It is worth asking this question first, because the catch block was often added to hide an error rather than to handle it.

Step throughChoosing the fix
  1. Should the order survive inner()'s failure?
    YesGo onNoRemove the catch and let it propagate
  2. Is inner()'s work independent of the order?
    YesREQUIRES_NEW on inner()NoGo on
  3. Is the failure an expected business case?
    YesCheck first, return a result, or noRollbackForNoRethink the boundary

3 steps in order. Play them, or step through them one at a time.

7Follow-up questions

Why does Spring not just commit what is left?

Because a participant reported a failure, and Spring cannot know which part of the shared work is inconsistent. Committing could save half of inner()'s changes. Rolling back everything and telling the caller is the only safe choice.

Would the same happen if inner() were called on this?

No. A self-call skips inner()'s proxy, so no interceptor sees the exception and nothing marks the transaction. outer() would catch it and commit, including any rows inner() wrote before failing, which is a different bug.

Can we turn off the marking?

AbstractPlatformTransactionManager has setGlobalRollbackOnParticipationFailure(false), which leaves the decision to the outer method. It changes behaviour for every participant, so I would only use it with care and good tests; fixing the boundary is usually better.

8Conclusion

In this answer, we saw that with REQUIRED the inner method joins the outer transaction, so its exception, seen by its proxy, can only mark the shared transaction rollback-only. Catching the exception does not clear the mark, so the outer commit becomes a rollback and Spring throws UnexpectedRollbackException rather than hide the lost work. We fix it with REQUIRES_NEW for independent work, with no exception or noRollbackFor for expected outcomes, or by letting the failure propagate.

What to say in the interviewAbout 80 seconds aloud

Open with

The key is that both methods share one physical transaction. inner uses REQUIRED, so when outer calls it, inner joins outer's transaction instead of starting its own. When inner throws a RuntimeException, the exception goes out through inner's proxy, and its transaction interceptor wants to roll back.

Then, if they want more

But inner is only a participant, it can't end the transaction, so the transaction manager marks the shared transaction as rollback-only. Then outer catches the exception, but catching it in Java doesn't touch that flag.

When outer returns and its proxy calls commit, the manager sees rollback-only, rolls back everything, including the order outer wanted to keep, and throws UnexpectedRollbackException, because the caller asked for a commit that didn't happen. How I fix it depends on what we want.

If inner's work is independent, like optional loyalty points, I'd make it REQUIRES_NEW so it has its own transaction and its failure doesn't affect outer, accepting the extra connection. If the failure is an expected business case, I'd avoid throwing, for example check first or return a result, or use noRollbackFor for that exception on inner.

And if the order shouldn't be saved when inner fails, the catch is the real bug, so I'd remove it and let the exception propagate.

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.
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 @Transactional outer method calls inner() on another bean, also @Transactional...": https://scrivio-interview-library.netlify.app/databases/transactions/bd15671d68/