For Spring Data JPA repositories, @Transactional defines the transaction boundary around repository calls. You usually do not have to add it yourself for the built-in methods: SimpleJpaRepository, the class behind JpaRepository, is already @Transactional(readOnly = true) at class level, and its write methods such as save and delete override that with a read-write transaction. You add the annotation yourself on custom @Modifying queries and, more importantly, on the service method that combines several repository calls.
Getting this wrong shows up as TransactionRequiredException, changes that are never saved, or half-completed operations. The sections below show what Spring gives you by default and where you need to take over.
Every repository interface that extends JpaRepository is backed at runtime by SimpleJpaRepository. That class carries @Transactional(readOnly = true) on the type and re-declares @Transactional on save, saveAll, delete, deleteById and similar write methods. Two consequences follow:
findByEmail run in a read-only transaction automatically.save directly from a controller works, because save opens its own read-write transaction when none exists.Each call is its own transaction when no outer one exists. Two separate repository calls in a row are therefore two transactions, which is exactly the situation the service layer annotation fixes.
Business operations usually touch more than one repository. If every repository call commits independently, a failure in the second call leaves the first one already committed.
Both repository calls join the transaction opened by raiseSalary, because REQUIRED propagation joins an existing one. If logs.save fails, the salary change rolls back with it. The explicit employees.save is unnecessary here: the entity is managed inside the transaction, and Hibernate detects the change through dirty checking and writes it at flush time.
Query methods annotated with @Query that run UPDATE or DELETE statements need @Modifying, and they need a transaction. Because the class-level default is read-only, add @Transactional on the method, or call it from a transactional service.
Bulk statements bypass the persistence context, so entities already loaded in the same transaction can be stale. clearAutomatically = true clears the context after the update; flushAutomatically = true flushes pending changes before it runs. Without a transaction you get TransactionRequiredException: Executing an update/delete query.
| Situation | What happens |
|---|---|
| Runtime exception in a repository call inside a service transaction | The shared transaction is marked rollback-only |
| Checked exception thrown from the service method | Commits, unless rollbackFor is set |
| Repository call from a method with no transaction | Each call runs in its own transaction |
Write call inside @Transactional(readOnly = true) | Hibernate does not flush, so changes are usually not persisted |
If a repository method throws inside a transaction and your service catches the exception and carries on, the transaction is already marked rollback-only. The outer commit then fails with UnexpectedRollbackException. Catch the exception outside the transactional boundary, or use REQUIRES_NEW on the part that is allowed to fail independently.
A read-only service method that calls save is a common bug. Either the write is silently dropped or the database rejects it, depending on the driver and settings. Use a read-write transaction for anything that changes data.
Lazy associations can only be loaded while the persistence context is open. Inside a @Transactional service method, employee.getProjects() works. After the method returns, the context closes and touching an uninitialised collection throws LazyInitializationException. Spring Boot enables spring.jpa.open-in-view by default, which keeps the session open for the web request, but many teams turn it off and instead fetch what they need inside the service, for example with JOIN FETCH or an entity graph, and return DTOs.
Usually not. The inherited CRUD methods are already transactional. Add it to custom @Modifying queries, or better, to the service method that orchestrates several repository calls.
A modifying query needs an active read-write transaction. The default repository transaction is read-only, so annotate the repository method or the calling service method with @Transactional.
No. Inside a transaction a managed entity is flushed through dirty checking on commit. save is only needed for new entities or detached ones.
Yes, for classes annotated with @Repository. The proxy rules are the same as for services: public methods called from outside the bean.
Spring Data JPA repositories are transactional by default, read-only for queries and read-write for the built-in write methods. Put your own @Transactional on service methods so several repository calls commit or roll back together, and on custom @Modifying queries. Keep lazy loading inside the transaction, avoid writes in read-only transactions, and do not swallow exceptions that have already marked the transaction rollback-only.