Quick revision
The short answer
saveRow() runs without a transaction because Spring starts transactions inside a proxy that wraps the bean, and the call from importFile() to saveRow() is a call on this, which never goes through that proxy. The fix is to make the call go through a Spring proxy, and the cleanest way is to move saveRow() into its own bean.
What to remember
In this answer, we saw that Spring applies @Transactional through a proxy, and that only calls entering through the proxy get a transaction. A call from importFile() to saveRow() is a call on this, so it skips the proxy and runs without a transaction. Moving saveRow() into its own bean or using TransactionTemplate fixes it, and while fixing it we should decide on purpose whether each row or the whole file is one transaction.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
saveRow() runs without a transaction because Spring starts transactions inside a proxy that wraps the bean, and the call from importFile() to saveRow() is a call on this, which never goes through that proxy. The fix is to make the call go through a Spring proxy, and the cleanest way is to move saveRow() into its own bean.
Why this question is asked
This is one of the most common surprises in Spring applications: the annotation is on the method, the code compiles, and still nothing happens. Interviewers like it because the answer shows whether we understand how Spring adds behaviour to our beans, and the same rule applies to @Async, @Cacheable, @Retryable and the security annotations. In this answer, we will see how Spring applies @Transactional, reproduce the problem, understand why the call skips the transaction, and then look at the fixes and how to choose between them.
1How Spring Applies @Transactional
A transaction groups database changes so that they all succeed or all fail together. When we put @Transactional on a method, we are asking Spring to start a transaction before the method runs, commit it when the method returns, and roll it back if the method throws an exception.
Spring does not change our class to do this. When the application starts, it creates a proxy for ImportService: a wrapper object, generated at runtime, that looks exactly like ImportService from the outside. The proxy is what Spring injects into every other bean that depends on ImportService. When another bean calls a method, the call reaches the proxy first. The proxy checks whether that method has @Transactional, starts a transaction if it does, and then passes the call on to our real object.
2Reproducing the Problem
We can see the problem directly by logging whether a transaction is active inside saveRow(). Spring gives us a method for exactly this check.
Notice that the log prints false for every row. If we call importService.saveRow(row) directly from another bean, the same line prints true. So the annotation works; it just never gets a chance to run when saveRow() is called from inside the class.
3Why the Call Skips the Proxy
The controller calls importFile() on the proxy. importFile() has no @Transactional, so the proxy forwards the call to our real ImportService object without starting anything.
Now we are running inside the real object. The line saveRow(r) is really this.saveRow(r), and this is the real object, not the proxy. A plain Java method call on this goes straight to the method. The proxy, which is the only place that knows how to start a transaction, never sees it.
Spring chose this design because it can add behaviour from the outside, at runtime, without touching our compiled classes. The cost is that the behaviour only applies to calls that come in through the proxy. A call that stays inside the object, which we call self-invocation, never passes through it.
4 messages in order. Play them, or step through them one at a time.
Only calls that come in through the proxy can start a transaction.
4Fix 1: Move saveRow() Into Its Own Bean
The cleanest fix is to put saveRow() in a separate bean. The call then leaves ImportService and enters the new bean through its proxy, so the transaction starts.
6 messages in order. Play them, or step through them one at a time.
This also makes the design clearer: anyone reading the code can see that writing a row is a separate, transactional operation.
5Fix 2: Use TransactionTemplate
If we prefer to keep the code in one class, we can start the transaction ourselves with TransactionTemplate. It runs the code we give it inside a transaction.
This is explicit and easy to read when we want one transaction per row, and it lets us catch the failure of one row and carry on with the next.
6Other Options and When to Avoid Them
Two more options work, but they are rarely the best choice. We can inject the bean into itself (marked @Lazy) and call self.saveRow(r), which goes through the proxy; it works, but it hides the trick from the next person reading the code. Or we can switch Spring to AspectJ weaving, which changes the compiled bytecode so that even self-calls are intercepted; that also works, but it makes the build and the tooling more complicated.
7Choosing the Transaction Boundary
The fix is also a decision. Suppose the file has 1,000 rows and row 500 is invalid. With one transaction per row, rows 1 to 499 stay saved, and we can record row 500 as failed and continue. With one transaction for the whole file (@Transactional on importFile()), row 500 rolls back everything and nothing is saved. Both can be right. The bug made this choice by accident; the fix should make it on purpose, based on what the business needs.
8Follow-up questions
Does putting @Transactional on the class fix it?
No. The annotation then applies to every public method, but the self-call still never goes through the proxy, so saveRow() still runs without a transaction when called from importFile().
Does @Transactional work on a private method?
No. Spring's proxies can only intercept methods that can be overridden, so private and final methods are never intercepted, even when another bean calls them.
Which other annotations have the same limitation?
Every annotation applied through a Spring proxy: @Async, @Cacheable and the other cache annotations, @Retryable, @PreAuthorize and similar security annotations. A self-call skips all of them.
9Conclusion
In this answer, we saw that Spring applies @Transactional through a proxy, and that only calls entering through the proxy get a transaction. A call from importFile() to saveRow() is a call on this, so it skips the proxy and runs without a transaction. Moving saveRow() into its own bean or using TransactionTemplate fixes it, and while fixing it we should decide on purpose whether each row or the whole file is one transaction.
Open with
This is the self-invocation problem. Spring applies @Transactional through a proxy around the bean, and only calls that go through the proxy get the transaction. importFile calls saveRow on this, the real object, so the transaction interceptor never runs.
Then, if they want more
I can confirm it by logging TransactionSynchronizationManager.isActualTransactionActive inside saveRow. The cleanest fix is to move saveRow into its own bean and call it through injection; TransactionTemplate is a good alternative when I want the transaction visible in the loop. Self-injection and AspectJ weaving also work, but I would avoid them without a reason.
I would also confirm whether the business wants one transaction per row or one for the whole file, because that decides where the annotation belongs.
About this answer
- Last updated
- 12 November 2026
- 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.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
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 "In this class, saveRow() runs without a transaction when called from importFile(), even...": https://scrivio-interview-library.netlify.app/databases/transactions/9e0d07f76f/