Skip to the page

Spring in production · Scenario

A @Transactional method that doesn't roll back

The interviewer asks

Finance reports refunds marked 'completed' in our database, but the money never left. The refund code is @Transactional, and the logs show the bank call failed. Why didn't it roll back?

Then

Follow-ups the interviewer asks next

  1. Which exceptions roll back by default?
  2. How do you confirm the cause from the code and the logs?
  3. What do you do about the refunds already marked completed?
  4. What's the proper fix in the code?
  5. Another team's similar code throws UnexpectedRollbackException instead. What's happening there?
  6. How do you test rollback behaviour?

The answer

What a strong answer covers

  • Rollback rules: by default only unchecked exceptions (RuntimeException) and Errors roll back. BankDeclinedException extends Exception is checked, so Spring commits the status change. Quick fix: @Transactional(rollbackFor = Exception.class), or the Framework 6.2+ global switch @EnableTransactionManagement(rollbackOn = RollbackOn.ALL_EXCEPTIONS).
  • Other causes to rule out: the exception is caught and logged inside the method; self-invocation (no proxy); a private or final method; the wrong transaction manager (two DataSources); MySQL MyISAM tables (no transactions at all).
  • The real design flaw: the status shouldn't depend on a rollback. Use states: PENDING, then call the bank outside the transaction with an idempotency key, then COMPLETED or FAILED in a short transaction; plus a reconciliation job against the bank's report.
  • Already affected: find refunds marked completed with no bank reference, compare with the bank statement, retry with the same idempotency key or mark failed; tell finance and support.
  • UnexpectedRollbackException: Transaction silently rolled back because it has been marked as rollback-only: an inner REQUIRED @Transactional method threw a RuntimeException, which marked the shared transaction rollback-only; the outer method caught it and tried to commit.
  • Test: an integration test (Testcontainers) where the bank client throws, then check the row is unchanged. A @Transactional test method rolls back on its own and can hide this, so let this test commit for real.

The red pen

Common traps

Avoid:"add a try/catch"; "Spring rolls back on every exception"; fixing the annotation but keeping the design.

Practise this out loud

Say your answer out loud to an AI interviewer that follows up, and get a score for it.

Practise by voice

Practise questions like this out loud

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview