On repository methods, @Transactional controls whether a query or write runs inside a transaction, whether that transaction is read-only, and how it combines with transactions opened by callers. In Spring Data, the repository base class already applies @Transactional(readOnly = true) to the whole type and a read-write @Transactional to save and delete methods. You override or add the annotation on your own repository methods when you need different behaviour, such as a modifying query, a custom timeout or a separate transaction.
Knowing the defaults prevents two common mistakes: adding the annotation everywhere out of habit, and forgetting it on custom write queries.
For an interface that extends JpaRepository or CrudRepository, the generated implementation is SimpleJpaRepository. It is annotated @Transactional(readOnly = true), and save, saveAll, delete, deleteAll and related methods carry their own @Transactional. So:
findById, findAll and derived queries run in a read-only transaction.save and delete run in a read-write transaction.If no outer transaction exists, each repository call is a separate unit of work. That is fine for a single operation and wrong for a multi-step one, which belongs in a service method.
You can place @Transactional on a method in your own repository interface. The method-level setting wins over the class-level default.
Without @Transactional here, calling adjustPrices outside an existing transaction fails with TransactionRequiredException, because the inherited default is read-only and @Modifying queries need a writable transaction.
The most common propagation modes in repository code are:
| Propagation | Behaviour | Typical use |
|---|---|---|
REQUIRED (default) | Join the current transaction or create one | Almost everything |
REQUIRES_NEW | Suspend the current transaction and start a new one | Audit rows that must commit even if the caller rolls back |
SUPPORTS | Use a transaction if one exists, otherwise run without | Pure reads that do not need one |
MANDATORY | Throw if no transaction exists | Methods that must only be called from a service transaction |
Here the audit entry commits independently, so a later rollback of the business transaction does not erase it. Remember that REQUIRES_NEW takes a second database connection while the first is still held.
The defaults apply as in any Spring bean: unchecked exceptions and errors cause rollback, checked exceptions do not. Repository methods mostly throw Spring's DataAccessException hierarchy, which is unchecked, so a constraint violation such as DataIntegrityViolationException rolls the surrounding transaction back.
Be aware that a failing repository call inside a larger transaction marks that whole transaction rollback-only, even if you catch the exception in the service. Continuing and returning normally then ends in UnexpectedRollbackException. If one step is allowed to fail on its own, give it REQUIRES_NEW.
A timeout bounds how long a transaction may run:
The timeout counts seconds and is enforced at the next database operation, so it is not a hard kill switch for a single long statement.
Treat repository transactions as the safety net, not the design. Put the main boundary on the service method, where one use case combines several repository calls and one failure rolls them all back. Use a repository-level annotation only for the exceptions: modifying queries, REQUIRES_NEW audit writes and custom timeouts. Avoid repeating @Transactional on derived queries, since it adds nothing to the inherited read-only default.
Yes. The base implementation is read-only transactional at class level, and its write methods are read-write transactional.
The default is read-only, and an UPDATE or DELETE query needs a writable transaction. Add @Transactional to the method or call it from a transactional service.
Yes, with the default REQUIRED propagation it joins the caller's transaction. Only REQUIRES_NEW and NOT_SUPPORTED break out of it.
Yes. Annotate the @Repository class or its public methods, and call it through the Spring bean so the proxy applies.
Repository methods in Spring Data are already transactional: read-only for queries, read-write for saves and deletes. Add your own @Transactional to custom modifying queries, to give an audit write its own transaction, or to set a timeout. Keep the main transaction boundary on the service method, and remember that a failure in any joined call marks the whole transaction rollback-only.