TransactionTemplate is Spring's callback-based helper for programmatic transaction management. You pass it a lambda, and it begins a transaction, runs your code, commits on success and rolls back if the code throws or calls setRollbackOnly(). It gives you the control of PlatformTransactionManager without the begin/commit/rollback boilerplate.
Use it when @Transactional is too coarse, for example when only part of a method should be transactional, when the transaction settings are decided at runtime, or when you need to avoid proxy limitations such as self-invocation.
TransactionTemplate lives in org.springframework.transaction.support and implements TransactionOperations. It wraps a PlatformTransactionManager and follows the same template pattern as JdbcTemplate: you supply the business logic, the template supplies the lifecycle.
Behaviour on completion:
status.setRollbackOnly(): the transaction rolls back and no exception is thrown.A TransactionTemplate instance is thread-safe once configured, so you create it once and reuse it.
Spring Boot auto-configures a TransactionTemplate bean when a single PlatformTransactionManager is present, so you can inject it directly. Outside Boot, or when you want custom defaults, build it yourself:
The template exposes the same attributes as @Transactional: propagation behaviour, isolation level, timeout and read-only flag.
For code that returns a value, use execute. For code that returns nothing, use executeWithoutResult, available since Spring Framework 5.2.
The status argument is a TransactionStatus. Use it to mark the transaction rollback-only or to check whether it is a new transaction.
The callback is not allowed to throw checked exceptions, because TransactionCallback.doInTransaction does not declare any. If you must signal a checked failure, wrap it in an unchecked exception or call setRollbackOnly() and return a result describing the failure. Unlike @Transactional, there is no rollbackFor attribute: any RuntimeException or Error rolls back.
| Aspect | TransactionTemplate | @Transactional |
|---|---|---|
| Style | Programmatic, explicit scope | Declarative, annotation-driven |
| Granularity | Any block of code | Whole method |
| Proxy required | No | Yes |
| Self-invocation works | Yes | No (bypasses the proxy) |
| Settings decided at runtime | Easy | Awkward |
| Boilerplate | Slightly more | Minimal |
Prefer @Transactional for the common case because it keeps business code free of infrastructure. Reach for TransactionTemplate when you need a transaction around only part of a method, for example to avoid holding a database connection during a slow HTTP call, or to run a retry loop where each attempt gets a fresh transaction.
Each iteration opens a new transaction, which is not possible when the whole method is annotated.
TransactionTemplate is for imperative, thread-bound transactions. In reactive applications use TransactionalOperator from spring-tx, which offers the same idea for Mono and Flux. Also remember that a transaction is bound to the current thread, so work handed to another thread or executor inside the callback does not take part in it.
Yes, after you finish configuring it. Share one instance, or inject the auto-configured bean, and call execute from many threads.
Call setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW) on a template, or create a second template bean with that setting for the cases that need it.
The callback cannot throw checked exceptions. Any unchecked exception rolls back, and you can call status.setRollbackOnly() for controlled rollbacks.
Yes. Inside an existing transaction the template joins it under the default PROPAGATION_REQUIRED, which is rarely what you want to do deliberately.
TransactionTemplate gives you explicit, proxy-free transaction boundaries with a small callback API. Use execute or executeWithoutResult, call setRollbackOnly() for controlled rollbacks, and configure isolation, timeout and propagation on the template. Keep @Transactional for ordinary service methods and use the template where you need finer control.