Fix TransactionRequiredException in Spring and JPA
Fix TransactionRequiredException when saving or running an update query by placing a transaction around the call and checking that the proxy applies.

TransactionRequiredException is Hibernate's way of saying that you asked it to write something and no transaction was open. The message varies slightly, but it always means the same: a write operation ran outside a transaction. In Spring the cause is almost never that you forgot transactions exist. It is that the annotation you wrote did not take effect, because of how Spring applies it. This post covers the four causes in order of frequency, with the fix for each, on Spring Framework 7, Spring Boot 4 and Hibernate 7.
What the error looks like
jakarta.persistence.TransactionRequiredException:
No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' calland for bulk queries:
jakarta.persistence.TransactionRequiredException:
Executing an update/delete queryThe first comes from a call to persist, merge or remove on a shared EntityManager with no transaction. The second comes from a @Modifying query method on a Spring Data repository, or a JPQL update or delete statement. Reads work without a transaction, which is why the bug appears only when someone adds a write.
Cause 1: the service method has no @Transactional
The simplest case. A service calls entityManager.persist(order) or a custom repository method, and nothing wraps it.
@Service
class OrderService {
@PersistenceContext EntityManager em;
@Transactional
void place(Order order) {
em.persist(order);
}
}@Transactional on the method makes Spring open a transaction before it runs and commit when it returns, or roll back on a runtime exception. Put it on the service method that represents one unit of work. Not on a controller, which would hold a transaction while rendering the response, and not on each repository call, which would split one business operation into several commits. Methods inherited from JpaRepository, such as save and delete, are already transactional, which is why repository.save(x) appears to work without an annotation and a direct EntityManager call does not. Understanding where Spring applies the annotation is covered in how the container works.
Cause 2: a @Modifying repository query
public interface OrderRepository extends JpaRepository<Order, Long> {
@Modifying
@Query("update Order o set o.status = 'CANCELLED' where o.createdAt < :cutoff")
int cancelOlderThan(@Param("cutoff") Instant cutoff);
}Custom @Modifying queries are not transactional by themselves. The interface default is readOnly = true for query methods in SimpleJpaRepository, and your declared method has no annotation of its own. Either call it from a @Transactional service method, which is the right choice because the bulk change is part of a business operation, or annotate the repository method:
@Transactional
@Modifying(clearAutomatically = true)
@Query("...")
int cancelOlderThan(...);clearAutomatically = true clears the persistence context afterwards, so entities loaded before the update do not hold stale values. Without it, a later findById in the same transaction can return the old status from the first-level cache, which looks like an update that did not happen.
Cause 3: the annotation is there and ignored
This is the cause people struggle with, since the code looks right. Spring applies @Transactional through a proxy around the bean. A call that does not go through the proxy gets no transaction. Four situations bypass it.
A method calling another method on this. The inner method's annotation is never seen, because the call does not leave the object. The detailed explanation and the fixes are in @Transactional not working: move the method to another bean, or inject a self reference.
A private method, or a final method on a class proxied by subclassing. Only public methods of proxied beans are advised in the default mode.
An object created with new. A new OrderService() is not a Spring bean and has no proxy; inject it.
A new thread. The transaction is bound to the thread that opened it. A CompletableFuture.runAsync or @Async method starts without one, so annotate the async method itself, which gives it its own transaction.
A quick diagnostic is to log TransactionSynchronizationManager.isActualTransactionActive() at the start of the failing method. If it prints false where you expected true, one of the four above applies. Setting logging.level.org.springframework.transaction.interceptor=TRACE prints each transaction the interceptor starts and commits, and shows when the call was never intercepted.
Cause 4: a read-only transaction
@Transactional(readOnly = true)
public void rename(long id, String name) {
repository.findById(id).orElseThrow().setName(name);
}A read-only transaction does not trigger this exception. It does something worse: Hibernate sets the flush mode to manual, the change is never written, and with some databases the driver rejects the update with a different error. A class-level @Transactional(readOnly = true) with a write method that forgot to override it is the usual source. Override at the method:
@Transactional
public void rename(long id, String name) { /* ... */ }Putting readOnly = true at the class level is a good default for query services, but every writing method needs its own plain @Transactional.
Checking which transaction you got
A transaction can exist and still be the wrong one. A method marked propagation = REQUIRES_NEW suspends the caller's transaction and opens an independent one, so entities loaded in the outer transaction are detached and a write in the inner one is not visible to the outer until it commits. NOT_SUPPORTED and NEVER run without a transaction on purpose and will produce this exception for a write. If a stack trace shows one of these on the path, the exception is working as designed and the fix is the propagation setting, not the missing annotation.
Open-in-view is a related trap. With spring.jpa.open-in-view=true, the default in Boot, the EntityManager stays open for the whole web request, so reads in a controller appear to work even outside a service transaction, and a write in the same place still fails. People sometimes mistake that for a bug in transactions, when it is the persistence context staying open without a transaction around it. Turn open-in-view off in new projects so that lazy loading and missing transactions fail where you can see them.
A short checklist
Is the method public, on a bean, called from another bean? Is the annotation the Spring one, org.springframework.transaction.annotation.Transactional, or the Jakarta one, which also works but has different attribute names? Is the call on the same thread? Is readOnly set where it should not be? Run those four checks before reading any more logs; they find the cause in nearly every case.
Programmatic transactions
When an annotation does not fit, because the transaction boundary is inside a method, use TransactionTemplate:
@Service
class ImportService {
private final TransactionTemplate tx;
ImportService(PlatformTransactionManager manager) {
this.tx = new TransactionTemplate(manager);
}
void importBatch(List<Row> rows) {
for (List<Row> chunk : Lists.partition(rows, 500)) {
tx.executeWithoutResult(status -> chunk.forEach(this::save));
}
}
}This opens a transaction per chunk and avoids the proxy question entirely, since nothing depends on the call path. It is the right tool for batch jobs that should commit in pieces.
Where this does not apply
If the exception names a different operation, such as a lock, you are looking at a lock mode issue. PessimisticLockingFailureException and StaleObjectStateException are separate problems, see locking failures. The transaction semantics are in the declarative transaction reference, the wider data-access picture is in the JPA and Hibernate guide, and the exception is defined in the Jakarta Persistence specification.


