BackendBench
Coffee
Spot the Bug #91

The transaction that is never started

A medium Java puzzle in the spring pitfalls family. One line below contains a real bug. Try to spot it first, then read why it fails and how to fix it.

mediumSpring pitfallsbuggy line: 7

Highlighted: line 7. Play this one as a puzzle without the answer.

What is wrong

process calls save on this, which bypasses the Spring proxy that implements @Transactional. No transaction is started, so a failure in the middle of save leaves partial changes. The annotation looks effective but is ignored.

The fix

Move save to another bean and inject it, or annotate process with @Transactional.

How to avoid it next time

Calls inside the same class skip proxies. Put transactional methods on a different bean or annotate the entry point.

Want the background? Read Spring proxies (how annotations work) in the Java concepts library.

About spring pitfalls

Spring features such as @Transactional, @Async and @Cacheable work through proxies. Calls that bypass the proxy, wrong bean scopes and misplaced annotations make behavior silently disappear.

In code review: For every annotation that adds behavior, check that the call crosses a Spring-managed bean boundary, that the method is public, and that the bean is a singleton only when it is stateless.

More puzzles in this family

๐Ÿž Play today's puzzleAll 132 bugsโ† #90#92 โ†’

Frequently asked questions

What is the bug in "The transaction that is never started"?

process calls save on this, which bypasses the Spring proxy that implements @Transactional. No transaction is started, so a failure in the middle of save leaves partial changes. The annotation looks effective but is ignored. The faulty line is line 7: save(orderId);

How do I fix it?

Replace it with: Move save to another bean and inject it, or annotate process with @Transactional.. Calls inside the same class skip proxies. Put transactional methods on a different bean or annotate the entry point.

How do I catch spring pitfalls in code review?

For every annotation that adds behavior, check that the call crosses a Spring-managed bean boundary, that the method is public, and that the bean is a singleton only when it is stateless.

Is there a new puzzle every day?

Yes. The daily Spot the Bug puzzle picks one of these snippets for each UTC day, with the difficulty set by the weekday. Your streak is stored only in your browser.

More tools you might need