Articles Spring

Spring @Transactional not working: 7 causes and fixes

When Spring @Transactional is not working, the cause is almost always one of two things. Either the call never passed through the transactional proxy that Spring wraps around your bean (a call through this, a private or final method, another thread), or it did and the rollback rules said “commit” (a checked exception, or an exception you caught yourself). In both cases nothing fails loudly. The method runs, the data is written, and the rollback you counted on never happens.

Every output below comes from one small application: Spring Boot 4.2.0 (Spring Framework 7.1.0), Spring Data JPA, H2, JDK 25. The same code gave the same results on Spring Boot 3.5.15 (Spring Framework 6.2.19), with one difference in logging that is pointed out where it matters.

How does @Transactional work in Spring?

The annotation is only metadata. At startup Spring creates a proxy around the bean, and other beans receive that proxy, not your object. Spring Boot uses CGLIB proxies by default, which are generated subclasses of your class:

service class: demo.PaymentService$$SpringCGLIB$$1

The proxy opens a transaction, calls the real method, then commits or rolls back depending on how the method ended. So two conditions must hold: the call has to arrive through the proxy, and the proxy has to see an exception that its rules treat as a reason to roll back.

Why is @Transactional ignored when the method is called from the same class?

This is self-invocation, the most common cause. importBatch() has no annotation and calls a transactional method of the same bean:

@Service
public class PaymentService {

    public void importBatch() {
        saveTwo(); // this.saveTwo(): the proxy is not involved
    }

    @Transactional
    public void saveTwo() {
        System.out.println("in saveTwo(): active="
                + TransactionSynchronizationManager.isActualTransactionActive());
        payments.save(new Payment("first"));
        throw new IllegalStateException("second payment failed");
    }
}
in saveTwo(): active=false
exception: java.lang.IllegalStateException: second payment failed
rows in table: 1 [first]

The row survives the exception. saveTwo() ran on the target object with no transaction around it, and payments.save committed in the repository’s own short transaction. The reference documentation states it directly: in proxy mode only external calls coming in through the proxy are intercepted.

There are three ways out:

  • Move the method to another bean. A call from bean A to bean B goes through B’s proxy. This is usually the cleanest fix, and it tends to improve the design as well.
  • Use TransactionTemplate. Programmatic transactions do not depend on a proxy, so they work anywhere, including private methods and lambdas.
  • Inject the bean into itself (@Lazy PaymentService self) and call self.saveTwo(). It works, but it creates a circular reference and hides the reason from the next reader. Treat it as a last resort.
public void importBatch() {
    transactionTemplate.executeWithoutResult(status -> {
        payments.save(new Payment("first"));
        throw new IllegalStateException("second payment failed");
    });
}

With the template, and with the self variant, the same run ends with rows in table: 0 [].

Does @Transactional work on private, protected and final methods?

The rules changed in Spring Framework 6.0. Up to 5.3 only public methods were transactional. Since 6.0, protected and package-visible methods are too, for class-based (CGLIB) proxies. Observed with CGLIB proxies on Spring Framework 7.1 and 6.2:

MethodCalled from another beanWhat happens
publictransactionThe normal case
protected, package-privatetransactionSince 6.0; ignored up to 5.3
privatenot callableReached only through this, so the annotation is ignored without a warning
finalno transactionThe proxy cannot override it; the code runs on the proxy instance itself

The final case is the nasty one. The method body runs on the proxy object, whose fields were never initialised:

final: active=false, name=null, payments=null
exception: java.lang.NullPointerException: Cannot invoke "demo.PaymentRepository.count()" because "this.payments" is null

Spring Framework 7 reports this at startup:

WARN  o.s.a.f.CglibAopProxy : Public final method [public final void demo.PaymentService.finalMethod()] cannot get proxied via CGLIB, consider removing the final marker or using interface-based JDK proxies.

On Spring Boot 3.5 the same application starts without that warning. Framework 6.2 writes a similar message at DEBUG level only, unless the final method implements an interface.

With JDK interface proxies (spring.aop.proxy-target-class=false), transactional methods must be public and declared in the proxied interface.

Why does @Transactional not roll back on a checked exception?

By default a transaction rolls back on RuntimeException and Error. A checked exception commits.

@Transactional
public void export() throws IOException {
    payments.save(new Payment("checked"));
    throw new IOException("export failed");
}
exception: java.io.IOException: export failed
rows in table: 1 [checked]

Declare the rule on the method:

@Transactional(rollbackFor = Exception.class)

The same call then leaves rows in table: 0 []. Since Spring Framework 6.2 the default can also be changed for the whole application with @EnableTransactionManagement(rollbackOn = RollbackOn.ALL_EXCEPTIONS), described under rolling back a declarative transaction. With that annotation on the application class, the first version rolled back too.

What happens when you catch the exception inside the method?

The proxy decides by the exception that leaves the method. Catch it inside, and the method returns normally, so the transaction commits:

@Transactional
public void swallowed() {
    try {
        payments.save(new Payment("swallowed"));
        throw new IllegalStateException("boom");
    } catch (RuntimeException e) {
        System.out.println("caught: " + e.getMessage());
    }
}

After this call the table holds the swallowed row. If the work must be undone, rethrow, or call TransactionAspectSupport.currentTransactionStatus().setRollbackOnly() in the catch block.

The opposite surprise appears with two beans. The outer method catches an exception thrown by an inner @Transactional method of another bean:

@Transactional
public void outer() {
    payments.save(new Payment("outer"));
    try {
        audit.failing(); // @Transactional in AuditService, throws IllegalStateException
    } catch (RuntimeException e) {
        // carry on
    }
}
exception: org.springframework.transaction.UnexpectedRollbackException: Transaction silently rolled back because it has been marked as rollback-only
rows in table: 0 []

Both methods share one physical transaction, because the default propagation is REQUIRED. When the exception crossed the inner proxy, that proxy marked the shared transaction as rollback-only. The outer method then returned normally and asked for a commit, which is no longer possible, so Spring rolls back and tells the caller. If the inner failure really is optional, run the inner method with Propagation.REQUIRES_NEW, or do not let the exception leave it.

Does @Transactional work with @Async, CompletableFuture and parallel streams?

A transaction is bound to the thread that started it. The @Transactional Javadoc says that it does not propagate to newly started threads within the method.

@Transactional
public void otherThread() {
    System.out.println("caller thread: " + Tx.state());
    CompletableFuture.runAsync(() -> {
        System.out.println("async thread:  " + Tx.state());
        payments.save(new Payment("async"));
    }).join();
    throw new IllegalStateException("rolling back the caller");
}

Tx.state() prints isActualTransactionActive() and getCurrentTransactionName() of TransactionSynchronizationManager.

caller thread: active=true, name=demo.PaymentService.otherThread
async thread:  active=false, name=null
exception: java.lang.IllegalStateException: rolling back the caller
rows in table: 1 [async]

The caller rolled back, and the row written on the other thread stayed. A parallel stream behaves the same way: the lambdas that ran on ForkJoinPool.commonPool-worker-1 printed active=false, those that ran on the calling thread printed active=true. An @Async method runs on an executor thread, and a virtual thread is a separate thread as well. Work that must be atomic stays on one thread. Work handed to another thread needs its own transactional method, and it commits or fails on its own.

Why does REQUIRES_NEW not start a new transaction?

It is self-invocation again. Propagation settings are read by the proxy, so through this they are never read at all:

@Transactional
public void pay() {
    payments.save(new Payment("payment"));
    audit(); // REQUIRES_NEW is ignored
    throw new IllegalStateException("payment rejected");
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void audit() {
    payments.save(new Payment("audit"));
}

Inside audit() the current transaction name is still demo.PaymentService.pay, and the audit row disappears with the payment: rows in table: 0 []. With audit() moved to an AuditService bean, the name becomes demo.AuditService.record and the run ends with rows in table: 1 [audit].

How to check whether a transaction is active

  1. Print TransactionSynchronizationManager.isActualTransactionActive() in the method. false means the proxy was bypassed.
  2. Check bean.getClass(). With Spring Boot’s defaults a proxied bean has $$SpringCGLIB$$ in its class name. A plain class name means there is no proxy, for example because the object was created with new.
  3. Turn on transaction logging:
logging.level.org.springframework.transaction.interceptor=TRACE
logging.level.org.springframework.orm.jpa.JpaTransactionManager=DEBUG

A call that goes through the proxy leaves a trail like this one:

DEBUG o.s.o.j.JpaTransactionManager : Creating new transaction with name [demo.PaymentService.saveTwo]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
TRACE o.s.t.i.TransactionInterceptor : Getting transaction for [demo.PaymentService.saveTwo]
TRACE o.s.t.i.TransactionInterceptor : Completing transaction for [demo.PaymentService.saveTwo] after exception: java.lang.IllegalStateException: second payment failed

No line with your method name means no interception. Participating in existing transaction means the method joined the caller’s transaction and did not start its own.

Summary

  • @Transactional is applied by a proxy. A call through this skips it, and that includes REQUIRES_NEW.
  • Public, protected and package-private methods are transactional with CGLIB proxies since Spring Framework 6.0. Private methods never are. Final methods break in a worse way.
  • Checked exceptions commit unless rollbackFor or a global rule says otherwise.
  • An exception caught inside the method commits. An exception caught one level up, after it crossed an inner proxy, ends in UnexpectedRollbackException.
  • A transaction lives on one thread.
  • When in doubt, ask TransactionSynchronizationManager and read the TRACE log.