Spring / Spring7 Intermediate to Advanced Interview questions
Why doesn't @Transactional work when called from within the same class (self-invocation)?
Spring's default @Transactional support is implemented with AOP proxies - a JDK dynamic proxy if the target implements an interface, or a CGLIB-generated subclass otherwise. The transactional behavior (starting, committing, rolling back) is advice woven in at the proxy layer, not inside the target class's own bytecode.
@Service public class OrderService { public void placeOrder(Order order) { // called via "this." - bypasses the proxy entirely this.saveOrder(order); } @Transactional public void saveOrder(Order order) { /* ... */ } }
When placeOrder calls this.saveOrder(order), that's a plain Java virtual method call on the raw target object - the call never goes back out through the proxy that Spring registered in the container, so none of the transactional interceptor logic runs. saveOrder executes with no transaction at all, silently, which is what makes this bug particularly hard to catch in testing.
Common fixes: split the transactional method into a separate collaborator bean and inject it, so the call genuinely goes through a proxy; inject the bean's own proxy back into itself (via @Lazy self-autowiring or ApplicationContext.getBean(...)) and call through that reference; or switch to AspectJ compile-time or load-time weaving, which instruments the actual bytecode rather than relying on a wrapper proxy, so self-invocation is advised correctly.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
