@Transactional on Spring Data JPA Methods
Fast recall
Quick revision
Core answer
Whether you place transactions at the service layer for atomicity and understand Spring's proxy-based behavior
Putting @Transactional on repository interfaces (redundant) or calling a @Transactional method from the same class (proxy bypassed, no transaction)
Explain readOnly=true skipping dirty checking and that checked exceptions don't roll back by default
Interview-ready guide
Interview answer
A transfer that debits one account and credits another belongs in one @Transactional service method. Calling each repository method separately without an outer transaction can create separate boundaries and allow a partial result. Spring Data supplies defaults for methods inherited from SimpleJpaRepository, but a custom query method may need an explicit transaction when its behavior requires one. For modifying @Query methods, use @Modifying and a transaction. readOnly=true can tune flush behavior, but the database and provider decide the exact enforcement.
@Transactional on Spring Data JPA Methods
Direct Answer
Spring Data's inherited CRUD methods already have transaction settings: reads are commonly read-only and writes use a regular transaction. Put @Transactional on a service method when several repository operations form one unit of work. readOnly=true is a hint and optimization opportunity, not a portable guarantee that dirty checking is skipped or writes are impossible. Proxy and rollback rules still apply.
Common Mistakes & Pitfalls
@Transactionalon repository interfaces — redundant; writes are already transactional. It belongs on service methods.- Skipping
readOnly = trueon read-only services — Hibernate needlessly tracks entities for changes. - Self-invocation — calling a
@Transactionalmethod from another method in the same class bypasses the proxy, so no transaction starts. - Expecting checked exceptions to roll back — only
RuntimeException/Errorroll back by default; userollbackFor = Exception.classfor checked ones.
Check your understanding
Answer these without looking back at the article.
- What is @Transactional(readOnly=true)?
- What is transaction propagation?
- Why does @Transactional roll back on RuntimeException but not checked Exception?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.