Spring / Spring7 Intermediate to Advanced Interview questions
When would you choose NESTED over REQUIRES_NEW transaction propagation?
Choose NESTED when an inner operation should be able to fail and roll back on its own, without ending or suspending the outer transaction around it - useful for something like a batch import where one bad record should be skipped without discarding everything already processed successfully.
@Transactional public void importBatch(List<Record> records) { for (Record r : records) { try { recordService.saveNested(r); // NESTED - savepoint per record } catch (ValidationException e) { log.warn("Skipping invalid record {}", r.getId()); } } } @Transactional(propagation = Propagation.NESTED) public void saveNested(Record r) { /* ... */ }
NESTED works by creating a JDBC savepoint within the same physical transaction and connection; if the nested unit fails, only that savepoint rolls back, and the outer transaction can catch the exception and continue using the same connection. This requires a PlatformTransactionManager that supports savepoints, such as DataSourceTransactionManager - it isn't universally available across every transaction manager.
Choose REQUIRES_NEW instead when the inner operation genuinely needs to be independent - committed or rolled back on its own regardless of what happens afterward in the outer transaction, such as writing an audit log entry that must persist even if the outer business operation later fails. REQUIRES_NEW physically suspends the outer transaction and starts a completely separate one on its own connection, rather than a savepoint sharing the same transaction - a heavier operation, but a stronger independence guarantee than a savepoint provides.
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...
