Quick revision
The short answer
I prefer TransactionTemplate when the transaction boundary must sit inside a method rather than around it: one transaction per chunk in a batch loop, a fresh transaction for each retry of an optimistic lock or serialization failure, or a short write between two remote calls. It is also clearer when I want the boundary visible in code without relying on proxies and the self-invocation rule, and it works in code that is not a Spring bean. In reactive code, TransactionalOperator plays the same role.
What to remember
In this answer, we saw that TransactionTemplate is the better choice when the transaction boundary must be smaller than a method or repeated inside it: one transaction per chunk in a batch, a new transaction per retry, short writes between remote calls in a loop, and code that is not a Spring bean or is reached from many paths. It also removes any dependence on proxies and self-invocation, and in reactive code TransactionalOperator does the same job.
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.
I prefer TransactionTemplate when the transaction boundary must sit inside a method rather than around it: one transaction per chunk in a batch loop, a fresh transaction for each retry of an optimistic lock or serialization failure, or a short write between two remote calls. It is also clearer when I want the boundary visible in code without relying on proxies and the self-invocation rule, and it works in code that is not a Spring bean. In reactive code, TransactionalOperator plays the same role.
Why this question is asked
Most Spring code uses @Transactional, and for a typical service method that is the right choice. But declarative transactions have two limits: the boundary is always a whole method, and it only works when the call passes through a Spring proxy. Interviewers ask this question to see whether we know those limits from experience. In this answer, we will recap how each approach works, then walk through the batch, retry and loop cases with code, and finish with a short guide for choosing.
1A Quick Recap of the Two Styles
@Transactional is declarative: we describe what we want and Spring's proxy, a wrapper object around our bean, starts and ends the transaction around each call to the annotated method. TransactionTemplate is programmatic: we call tx.execute(callback) and the transaction lasts exactly as long as the callback runs. Both use the same PlatformTransactionManager underneath, so they share propagation, isolation and rollback behaviour and can be mixed in one application.
| Aspect | @Transactional | TransactionTemplate |
|---|---|---|
| Boundary | Whole method | Any block of code |
| Needs a Spring proxy | Yes, self-calls skip it | No |
| Works outside Spring beans | No | Yes, given a transaction manager |
| Visible in the code | As an annotation | As an explicit call |
| Typical use | Ordinary service methods | Loops, retries, mixed remote and DB work |
2Batch Jobs: One Transaction per Chunk
Suppose a job imports 200,000 rows. With @Transactional on the job method, everything is one transaction: locks and memory grow for the whole run, and one bad row at the end rolls back all of it. We want a transaction per chunk of, say, 1,000 rows, so the boundary has to sit inside the loop.
With @Transactional we would need a separate bean with a saveChunk() method, because calling a method on this skips the proxy. The template keeps the loop and its boundary in one place.
3Retries: A Fresh Transaction per Attempt
When a transaction fails with an optimistic lock or serialization error, the retry must be a new transaction that reads fresh data. A loop around tx.execute() expresses exactly that, and makes it obvious to the reader that each attempt commits or fails on its own.
A declarative retry annotation can do the same, but only if its interceptor runs outside the transaction interceptor. With the template there is no ordering question at all.
4Loops and Mixed Work: Keeping Remote Calls Out
Many methods mix remote calls with database writes. A loop that processes 100 webhook events, each needing an HTTP call to enrich it and then an insert, should not hold one transaction while 100 HTTP calls run. With the template we make the HTTP call first, then open a short transaction for the insert, inside the same loop iteration. The method reads top to bottom in the order things happen, and the transaction covers only the lines that need it.
5Code Outside Spring and Reactive Code
Sometimes the code is not a Spring bean: a utility called from many paths, a class created with new in a library, or a test helper. There is no proxy to apply @Transactional, but anything that can be given a PlatformTransactionManager can build a TransactionTemplate. The same holds for code called from many entry points whose callers we do not control: an explicit template makes the transaction behaviour independent of how the caller got there.
In reactive code with Spring WebFlux and R2DBC, the transaction is tied to the reactive pipeline rather than to a thread, and the programmatic equivalent is TransactionalOperator. It wraps a Mono or Flux so the work inside it runs in one transaction.
6A Simple Way to Choose
My default is @Transactional on public service methods, because it is short and everyone recognises it. I switch to TransactionTemplate when one of these is true: the boundary is smaller than the method, there is a loop with one transaction per item or chunk, the whole transaction must be retried, the calling code is not a proxied bean, or the transaction would otherwise depend on self-invocation tricks. In a code review, the question I ask is simple: does the transaction start and end where the work that must be atomic starts and ends?
7Follow-up questions
Is TransactionTemplate slower than @Transactional?
No measurable difference. Both go through the same transaction manager; the annotation only adds a proxy call and an attribute lookup. The cost of a transaction is in the database round trips, not in how it was started.
Can I use both in the same call chain?
Yes. A tx.execute() inside a @Transactional method joins the outer transaction with the default REQUIRED propagation, and a @Transactional method called from inside a template callback joins the template's transaction. To get a separate transaction, set REQUIRES_NEW on the inner one.
Does Spring Batch replace this for batch jobs?
For large or restartable jobs, yes. Spring Batch manages one transaction per chunk, records progress, and restarts from the last committed chunk. TransactionTemplate is the lighter choice for small jobs that do not need that machinery.
8Conclusion
In this answer, we saw that TransactionTemplate is the better choice when the transaction boundary must be smaller than a method or repeated inside it: one transaction per chunk in a batch, a new transaction per retry, short writes between remote calls in a loop, and code that is not a Spring bean or is reached from many paths. It also removes any dependence on proxies and self-invocation, and in reactive code TransactionalOperator does the same job.
Open with
I default to @Transactional for ordinary service methods, and I reach for TransactionTemplate when the boundary needs to be inside the method rather than around it. The clearest example is a batch loop: if I import 200,000 rows I want one transaction per chunk of a thousand, so a bad chunk rolls back alone and locks stay short.
Then, if they want more
With the annotation I would need a separate bean just to get through the proxy; with the template the boundary sits right in the loop. The second case is retries: after an optimistic lock or serialization failure, each retry must be a fresh transaction, and a loop around transactionTemplate.execute makes that explicit with no interceptor ordering to worry about.
Third, loops that mix remote calls and inserts, where I want the HTTP call outside and a short transaction just for the write. It also helps when the code is not a Spring bean or is called from many paths, because it does not depend on proxies or the self-invocation rule. For reactive code the equivalent is TransactionalOperator.
My review question is always whether the transaction starts and ends exactly where the atomic work does.
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 "When would you prefer TransactionTemplate over @Transactional? Give examples from batch...": https://scrivio-interview-library.netlify.app/databases/transactions/0d4a9ac77e/