Scrivio Interview library
LeadershipAdvanced7 min read · updated 12 November 2026

Self-invocation bugs with @Transactional keep reaching production across teams. What would you put in place?

I would attack it from several sides at once: teach the proxy model in onboarding with concrete examples of the failure, add static checks such as IDE inspections and custom rules that flag calls on this to annotated methods, and require integration tests that verify transaction behaviour, for example that a failure really rolls back. I would also encourage explicit transaction boundaries at service entry points, so fewer internal methods carry the annotation, and consider AspectJ mode only if the platform team can support it everywhere.

Why this question is asked

When the same class of bug keeps reaching production from different teams, the problem is no longer any one developer's knowledge; it is that our system makes the bug easy to write and hard to see. Self-invocation with @Transactional is a perfect example: the code looks right, unit tests with mocks pass, and the failure shows up as data inconsistency weeks later. Interviewers ask this leadership question to see whether we can design a layered response that teams will actually adopt. In this answer, we will recap the bug briefly, then go through teaching, static checks, integration tests, design conventions and the AspectJ option, with what each one catches and costs.

1A Quick Recap of the Bug

Spring applies @Transactional through a proxy around the bean. Calls from other beans go through the proxy and get a transaction. A call from one method of the bean to another, on this, goes straight to the real object and skips the proxy, so the annotated method runs without the transaction it declares. The usual symptom is partial data: an operation that should roll back as a whole leaves half its changes in the database after a failure.

Java
@Service
class PaymentService {
public void settle(List<Payment> batch) {
batch.forEach(this::settleOne); // self-call: @Transactional ignored
}
@Transactional
public void settleOne(Payment p) {
ledger.debit(p);
ledger.credit(p); // throws: the debit stays committed
}
}

Because the code compiles, starts and passes mock-based tests, the bug reaches production unless something in our process is specifically looking for it.

2Teach the Proxy Model With Concrete Examples

Most engineers who write this bug know that @Transactional exists but have never seen how it is applied. A short onboarding module fixes that: thirty to sixty minutes showing that Spring injects a proxy, a live demo printing TransactionSynchronizationManager.isActualTransactionActive() from a self-call and from an external call, and two or three examples taken from our own incidents. Seeing false printed from a method marked @Transactional is memorable in a way a rule in a wiki is not.

The material should also list the relatives of the bug, @Async, @Cacheable, @Retryable and @PreAuthorize on self-calls, private methods and final methods, so engineers recognise the whole family. Teaching is cheap, but on its own it fades, so it is the first layer, not the only one.

3Add Static Checks That Find the Pattern

The pattern is mechanical enough to detect. IntelliJ IDEA already has inspections for @Transactional self-invocation and for annotations on private methods; we can make them part of the shared inspection profile and run them in CI with the IDE's command-line inspector or a Qodana run. For teams on other tools, a custom rule in Error Prone, PMD or an ArchUnit test can flag a method call whose receiver is this (explicit or implicit) to a method carrying @Transactional, @Async or @Cacheable in the same class.

We ship the rules in the shared build configuration, start them in warning mode so teams can see existing findings, and switch them to failing the build once the backlog is cleared. A known exception can be suppressed with a short comment explaining why, which keeps the exception visible in review.

4Test Transaction Behaviour, Not Just Results

Unit tests with mocks never create a proxy, so they cannot see this bug. Integration tests with a real Spring context and a real database, for example using Testcontainers with PostgreSQL, can. The useful tests do not just check the happy path; they force a failure in the middle of a transactional operation and assert the rollback.

Java
@SpringBootTest
class PaymentRollbackTest {
@Autowired PaymentService payments;
@MockitoBean LedgerCreditClient credit; // the client settleOne() uses for the credit; Spring Boot 3.4+, earlier versions use @MockBean
@Autowired LedgerRepository ledger;
@Test
void failedCreditRollsBackDebit() {
doThrow(new IllegalStateException("down")).when(credit).credit(any());
assertThrows(IllegalStateException.class, () -> payments.settle(List.of(payment(42))));
assertThat(ledger.findByPaymentId(42)).isEmpty(); // fails while the self-call bug exists
}
}
Step throughLayers that catch a self-invocation bug
  1. Engineer writes a self-call to @Transactional
  2. Did onboarding cover it?proxy model demo and incident examples
    YesEngineer avoids the patternNoNext layer
  3. Does the static check flag it?IDE inspection or custom rule in CI
    YesBuild fails or warns in the pull requestNoNext layer
  4. Does a rollback test fail?real context, forced failure
    YesBug caught before releaseNoReaches production

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

Each layer catches what the previous one missed.

We ask every service to have such tests for its key transactional operations, the ones where partial data would hurt. They are slower than unit tests, so a handful per service is the goal, chosen by the business impact of a partial write.

5Prefer Explicit Boundaries at Service Entry Points

Many self-invocation bugs come from annotating small internal methods. A convention that transaction boundaries sit on the public entry methods of service classes, the methods controllers, listeners and jobs call, removes most of the opportunities. Inside a service, helper methods then run in the transaction that the entry point already started, and self-calls between them do not matter.

When a loop needs one transaction per item, we recommend TransactionTemplate, which makes the boundary visible in the loop and does not depend on a proxy, or extracting the per-item work into its own bean. The architecture guidelines show both with code, so reviewers can point to a standard answer rather than inventing one each time.

6Consider AspectJ Mode Only With Platform Support

AspectJ mode, where the transaction advice is woven into the bytecode, makes self-calls transactional and removes the bug class entirely. It is tempting as a single fix. But it requires the AspectJ compiler in every build or a weaving agent in every runtime, consistent setup in tests, IDE support and engineers who can debug woven code. If a platform team can own that across all services, it is a real option; if not, a half-supported AspectJ setup creates new and stranger failures. I would only consider it after the other layers are in place and measured.

Finally, we measure progress: the number of findings from the static checks, the number of services with rollback tests on key operations, and the number of self-invocation incidents per quarter. If incidents do not fall within two or three quarters, we revisit which layer is failing.

7Follow-up questions

Would you make the static check block the build immediately?

No. On existing code it would produce a backlog that blocks every team at once. I would start in warning mode, publish the counts, give teams a deadline, and then make it blocking, possibly earlier for services that handle money or personal data.

How do you get teams to write rollback tests?

Provide a template and a shared test base with Testcontainers already configured, so writing one is a few minutes of work, and include one or two rollback tests in the definition of done for new transactional operations. Showing a real incident that a rollback test would have caught is usually the strongest argument.

Is moving all transactions to the entry points always right?

It is a good default, not a law. Long batch operations often need smaller transactions per item to avoid long locks and huge rollbacks. In those cases the boundary should be explicit, with TransactionTemplate or a separate bean, rather than an annotation on a self-called method.

8Conclusion

In this answer, we built a layered response to recurring self-invocation bugs. Onboarding teaches the proxy model with live examples and our own incidents. Static checks in the shared build flag self-calls to annotated methods. Integration tests with real databases force failures and assert rollbacks. A convention puts transaction boundaries at service entry points, with TransactionTemplate or a separate bean for per-item work. AspectJ mode is an option only with full platform support, and we measure incidents to see which layer needs work.

What to say in the interviewAbout 90 seconds aloud

Open with

When the same bug keeps reaching production across teams, I treat it as a system problem, not individual mistakes, and I would put several layers in place. First, teaching: an onboarding module on the proxy model with a live demo, printing whether a transaction is active from a self-call and from an external call, plus examples from our own incidents, and covering the relatives like @Async, @Cacheable and private or final methods.

Then, if they want more

Second, static checks: enable IntelliJ's self-invocation inspection in the shared profile and run it in CI, or a custom Error Prone, PMD or ArchUnit rule that flags calls on this to annotated methods, starting in warning mode and then blocking.

Third, integration tests with a real context and database, for example Testcontainers, that force a failure in the middle of key transactional operations and assert the rollback, because mock-based unit tests never create a proxy and cannot see the bug.

Fourth, a design convention: transaction boundaries sit on public service entry points, and per-item transactions use TransactionTemplate or a separate bean, so few internal methods carry the annotation at all. Fifth, AspectJ mode would remove the bug class, but only if the platform team can support weaving in every build, test and runtime; otherwise it creates new problems.

Then I would track static-check findings and incidents per quarter to see whether it works.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 3.4
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 "Self-invocation bugs with @Transactional keep reaching production across teams. What...": https://scrivio-interview-library.netlify.app/spring-boot/aop/ffc35a698e/