DEV Community

ToolStack
ToolStack

Posted on

Spring Boot @Transactional Didn't Roll Back: The Self-Invocation Trap

The refund crashed halfway. The money still left our account. And @Transactional was right there.

This is INCIDENT #003 from Production Incident Room, our series of 30-second production bugs. This is the long version, with the why, the fix and a test that proves it.

The incident

ShopForge, our fictional online store, has a refund flow. A refund does two things: it sends money back through the payment provider and it marks the order as refunded. Both should happen or neither should. That's what a transaction is for.

One night a refund crashed halfway through. The payment had already been sent, the database write after it failed, and nothing rolled back. The code looked fine:

@Service
public class OrderService {

    public void refund(Order order) {
        processRefund(order);          // looks transactional...
    }

    @Transactional
    public void processRefund(Order order) {
        ledger.debit(order);           // write 1
        orders.markRefunded(order);    // write 2: throws -> should roll back write 1
    }
}
Enter fullscreen mode Exit fullscreen mode

processRefund is annotated. So why was there no transaction?

Why it happened: Spring transactions live in a proxy

In Spring's default mode, @Transactional doesn't change your class. Spring wraps your bean in a proxy. When another bean calls orderService.processRefund(...), the call goes through the proxy, the proxy starts a transaction, calls your method, then commits or rolls back.

Controller ──► [ Proxy: begin tx ] ──► OrderService.processRefund() ──► [ commit / rollback ]
Enter fullscreen mode Exit fullscreen mode

But refund() calls processRefund() on this, from inside the same object. That call never leaves the object, so it never passes through the proxy:

Controller ──► [ Proxy ] ──► OrderService.refund() ──► this.processRefund()   ← no proxy, no transaction
Enter fullscreen mode Exit fullscreen mode

No transaction was started, so there was nothing to roll back. Each write committed on its own. The Spring reference documentation calls this out directly: in proxy mode, only external method calls coming in through the proxy are intercepted.

This is one of the most common Spring interview questions, because it surprises almost everyone the first time.

The fix

Option 1 (cleanest): move the transactional method into its own bean.

@Service
public class RefundProcessor {
    @Transactional
    public void process(Order order) {
        ledger.debit(order);
        orders.markRefunded(order);
    }
}

@Service
public class OrderService {
    private final RefundProcessor refunds;
    public OrderService(RefundProcessor refunds) { this.refunds = refunds; }

    public void refund(Order order) {
        refunds.process(order);        // goes through the proxy -> transaction starts
    }
}
Enter fullscreen mode Exit fullscreen mode

Option 2: put @Transactional on the entry method (refund()), if the whole flow should be one transaction anyway.

Option 3: use TransactionTemplate when you want explicit, programmatic boundaries:

transactionTemplate.executeWithoutResult(status -> {
    ledger.debit(order);
    orders.markRefunded(order);
});
Enter fullscreen mode Exit fullscreen mode

(Injecting a bean into itself, or switching to AspectJ weaving, also works. Both are usually more surprising for the next developer than a small extra bean.)

Prove it with a test

Don't trust the annotation. Write a test that forces the second write to fail and checks that the first write is gone:

@SpringBootTest
class RefundRollbackTest {

    @Autowired OrderService orderService;
    @Autowired LedgerRepository ledger;
    @MockitoSpyBean OrderRepository orders;   // Spring Boot 3.4+; use @SpyBean on older versions

    @Test
    void refundRollsBackWhenSecondWriteFails() {
        doThrow(new RuntimeException("boom")).when(orders).markRefunded(any());

        assertThrows(RuntimeException.class, () -> orderService.refund(testOrder()));

        assertThat(ledger.count()).isZero();   // fails with the self-call bug, passes with the fix
    }
}
Enter fullscreen mode Exit fullscreen mode

Remember

  • Spring applies @Transactional through a proxy.
  • A call to this.someMethod() skips the proxy, so there's no transaction.
  • Fix it by calling through another bean, annotating the entry point, or using TransactionTemplate.
  • Test the rollback. An annotation is a promise, and a test is proof.

Run it yourself, then catch it in your code

  • Run this exact bug in your browser (free Codespace): production-incident-room/003. Run the test, watch the partial write survive, then apply the fix.
  • Find it in your own project: Incident Lint is a free, open-source checker that flags @Transactional self-calls and @Transactional on private, static or final methods. java -jar incident-lint.jar src/

ShopForge is our fictional store; the failure pattern is real.


Next in the series — INCIDENT #004: Java 21 virtual threads froze checkout instead of speeding it up.

Top comments (0)