Database Concurrency & Scaling interview question · Most asked
A Spring @Transactional method threw an exception but the data was still saved. What are the likely causes?
Summary
Either the method wasn't actually transactional (self-invocation bypasses the proxy, or the method isn't public on a Spring bean) or the exception was a checked exception, which commits by default. Also check for swallowed exceptions and REQUIRES_NEW calls.
Longer Answer
- Self-invocation:
this.placeOrder()from the same class doesn't go through the proxy — no transaction at all. - Checked exceptions: Spring rolls back only on
RuntimeException/ErrorunlessrollbackForis set. - Swallowed exceptions: catching the exception inside the transactional method means the interceptor never sees it.
Propagation.REQUIRES_NEWinner calls commit independently of the outer transaction.- Different transaction managers / datasources: the write went through a connection not bound to the transaction.
Fix by moving the method to another bean (or calling through the proxy), using rollbackFor = Exception.class where appropriate, rethrowing, and testing the failure path.
Key Points
- Proxy-based: self-calls skip it
- Checked exceptions commit by default
- Don't swallow exceptions
- Beware REQUIRES_NEW
Deep dive: Framework Transaction Pitfalls
More most-asked Database Concurrency & Scaling questions
- Two customers buy the last item at the same moment and both orders are confirmed. What happened, and how do you prevent it?
- After a deploy, logs show hundreds of 'deadlock detected' errors per hour on checkout. Walk through how you'd investigate.
- Your service throws 'connection pool exhausted' errors but database CPU is only 20%. What's going on?
- Why shouldn't you call a payment API inside a database transaction?
- What's the difference between Read Committed and Repeatable Read in practice?
- Database CPU is at 85%. How do you decide what to optimise before buying a bigger server?
- A user saves their profile and the next page shows the old data. Reads go to replicas. How do you fix it?
- How do you choose a shard key for a multi-tenant SaaS?