Quick revision
The short answer
@Transactional belongs on application service methods, where one method represents one use case and therefore one unit of work. Controllers should not define transactions, and repositories and domain services should rely on the caller's transaction rather than start their own. One use case should be one transaction where possible, REQUIRES_NEW should be kept for clearly justified cases such as audit logs, and remote calls and messaging should stay outside the transaction or go through an outbox. We enforce the rules with ArchUnit tests and code review.
What to remember
In this answer, we set rules for transaction boundaries in a layered service. The application service method for one use case owns the transaction, controllers do not define one, and domain services and repositories join the caller's transaction. One use case is one transaction, REQUIRES_NEW is kept for documented cases such as audit records, and remote calls and messages stay outside or go through an outbox. ArchUnit tests and a review checklist keep the rules in place.
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.
@Transactional belongs on application service methods, where one method represents one use case and therefore one unit of work. Controllers should not define transactions, and repositories and domain services should rely on the caller's transaction rather than start their own. One use case should be one transaction where possible, REQUIRES_NEW should be kept for clearly justified cases such as audit logs, and remote calls and messaging should stay outside the transaction or go through an outbox. We enforce the rules with ArchUnit tests and code review.
Why this question is asked
Without a rule, @Transactional ends up everywhere: on controllers because a lazy-loading error went away, on repositories because someone wanted to be safe, and on domain methods with REQUIRES_NEW because a record was not being saved. Each choice made sense locally, and together they create transactions that are too long, too short or impossible to reason about. Interviewers ask this design question to see whether we can turn experience into a simple standard that a whole team can follow. In this answer, we will recap the layers, decide where transactions belong and why, look at what goes wrong in each other layer, handle remote calls, and finish with automated enforcement.
1A Quick Recap of the Layers
In a typical layered Spring service, a controller receives the HTTP request and translates it into a call. An application service carries out one use case, such as 'place order' or 'cancel subscription': it loads what it needs, asks the domain objects to apply business rules and saves the result. Domain services hold business logic that does not belong to a single entity, such as a pricing calculation across several products. Repositories load and save aggregates.
A transaction should match a unit of work that must succeed or fail as a whole. The question is which of these layers knows where that unit begins and ends.
- ControllerHTTP in and out; no
@Transactional - Application serviceOne public method per use case;
@Transactionalhere - Domain services and entitiesBusiness rules; run inside the caller's transaction
- RepositoriesLoad and save; join the caller's transaction
- DatabaseOne physical transaction per use case
5 layers, from the top down. Step through them one at a time.
Only the application service knows the whole use case, so only it opens the transaction.
2Why the Application Service Owns the Transaction
The application service is the only layer that sees the whole use case. If 'transfer money' debits one account and credits another, only the service method that does both knows that the two updates must commit together. Put the boundary lower and each update commits alone; put it higher and the transaction includes HTTP concerns it does not need.
A useful side effect is that the transaction's name, the service method, now describes the business operation. When we read a slow-transaction log or a lock wait report, we see TransferService.transfer, not a list of repository calls.
3Why Not in Controllers or Repositories
A transaction on a controller method stays open while the request is parsed, while the response is serialised to JSON, and while lazy relations are loaded during serialisation. The connection is held longer than necessary, and the boundary hides the real problem, which is usually that the service returned entities instead of a view of the data. It also means another entry point, such as a message listener calling the same logic, gets no transaction.
On repositories, Spring Data already makes each built-in method transactional on its own: SimpleJpaRepository is annotated @Transactional(readOnly = true) at class level and @Transactional on the write methods. With the default REQUIRED propagation, those simply join the service's transaction. Adding our own transactions at repository level, especially REQUIRES_NEW, would split a use case into several commits. Domain services follow the same rule as repositories: they are called by an application service and run inside its transaction.
4One Use Case, One Transaction
The default rule should be one transaction per use case. It is the easiest model to reason about: either the whole operation happened or none of it did. REQUIRES_NEW, which suspends the caller's transaction and commits a separate one, breaks that model and also needs a second connection from the pool for the duration. It is allowed only for documented reasons where the inner write must survive the outer rollback, such as an audit log entry or an idempotency record.
Long use cases need care too. A batch import of 100,000 rows as one transaction holds locks for minutes, so a batch job is an exception where we deliberately use one transaction per chunk, written explicitly with TransactionTemplate or Spring Batch, and we document that partial progress is expected.
5Keeping Remote Calls and Messages Out
Any call to another system inside the transaction holds the connection and locks for the duration of the network call and creates a consistency gap, because the remote system does not roll back with us. A Kafka message sent before commit may describe an order that then rolls back.
So the rule is that application services do remote calls before the transaction or after it commits. When an action must happen if and only if the transaction commits, we use the outbox pattern: insert an outbox row in the same transaction and let a separate process publish it. For in-process reactions, @TransactionalEventListener with phase AFTER_COMMIT runs code only once the commit has succeeded.
6Enforcing the Rules
Rules in a wiki are forgotten. Architecture tests make them part of the build. ArchUnit, a library that checks dependencies and annotations in compiled classes, can express each rule in a few lines.
Some things cannot be checked mechanically, such as whether a REQUIRES_NEW has a real reason or whether a method makes a remote call inside the transaction. Those belong in the code review checklist, with the documented exceptions listed so reviewers can point to them.
7Follow-up questions
What about read-only use cases?
The same rule applies: the application service method for the query gets @Transactional(readOnly = true), so all reads see a consistent connection and Hibernate can skip dirty checking. Many teams put readOnly = true on the service class and override it on write methods.
Should domain entities ever know about transactions?
No. Entities and domain services should be plain business logic that can be tested without Spring. Keeping transactions in the application layer is what makes that possible.
How do you migrate an existing codebase?
Start with ArchUnit rules in a frozen mode that records existing violations and fails only on new ones, then remove old violations service by service, with tests that check the database state after failures so behaviour changes are visible.
8Conclusion
In this answer, we set rules for transaction boundaries in a layered service. The application service method for one use case owns the transaction, controllers do not define one, and domain services and repositories join the caller's transaction. One use case is one transaction, REQUIRES_NEW is kept for documented cases such as audit records, and remote calls and messages stay outside or go through an outbox. ArchUnit tests and a review checklist keep the rules in place.
Open with
My rule is that the transaction boundary belongs to the application service, one public method per use case, because that's the only layer that knows which changes must succeed or fail together.
Then, if they want more
If transfer debits one account and credits another, the service method that does both is where the @Transactional goes. Controllers don't get transactions: they would hold the connection during request parsing and JSON serialisation, they hide lazy-loading problems that should be fixed by returning views, and other entry points like message listeners wouldn't get the same boundary.
Repositories and domain services don't start their own transactions; Spring Data's built-in methods are already transactional with REQUIRED, so they just join the service's transaction. I'd default to one use case being one transaction, and allow REQUIRES_NEW only for documented reasons like an audit or idempotency record, because it splits the commit and needs a second connection.
Remote calls and Kafka messages stay outside the transaction, or go through an outbox table written in the same transaction, or an after-commit event listener. Batch jobs are a deliberate exception with chunked transactions.
And I'd enforce it with ArchUnit rules, no @Transactional in controllers, only in the application package, plus a review checklist for the things a tool can't see.
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 "Set rules for where @Transactional belongs in a layered service architecture...": https://scrivio-interview-library.netlify.app/databases/transactions/5bd118baf9/