Understanding Spring @Transactional: Common Pitfalls and Proper Usage

This article explains how Spring's @Transactional annotation works, common pitfalls such as using it on private methods, and the correct ways to configure propagation, isolation, and bean injection to ensure proper transaction rollback and non‑rollback behavior.

Top Architect
Top Architect
Top Architect
Understanding Spring @Transactional: Common Pitfalls and Proper Usage

Transaction management in Spring allows consistent operations; using the @Transactional annotation ensures atomicity.

@Transactional(readOnly = false, rollbackFor = Throwable.class, isolation = Isolation.REPEATABLE_READ)

When a method annotated with @Transactional throws an exception, the transaction rolls back. Adding a non‑rollback operation (e.g., logging) requires special handling.

One approach is to use Propagation.NOT_SUPPORTED on a separate method, but this only works if the method is public and invoked via a Spring bean.

@Transactional(propagation = Propagation.NOT_SUPPORTED) public void doMyExJob(UserDebitCardBean userDebitCard) { System.out.println("do my job..."); // do my job... }

Placing the method as private in the same class causes the annotation to be ignored because Spring AOP proxies only public methods.

The solution is to move the method to another Spring‑managed bean and inject it, ensuring the proxy intercepts the call.

@Override @Transactional(propagation = Propagation.NOT_SUPPORTED) public void doMyExJob(UserDebitCardBean userDebitCard) { System.out.println("do my job..."); // do my job... }

Configuration example: <tx:annotation-driven/> automatically picks up a bean named transactionManager.

<!-- Configure transaction manager --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/> <!-- enables scanning for @Transactional annotations --> <tx:annotation-driven/>

The @Transactional annotation can be placed on classes, methods, interfaces, but Spring recommends using it on public methods of implementation classes.

mode: proxy (default) or aspectj.

proxy-target-class: true for class‑based proxies, false for interface‑based.

order: controls ordering of transaction advice relative to other advices.

transaction-manager: reference to a PlatformTransactionManager bean.

Attributes of @Transactional include isolation, propagation, readOnly, timeout, rollbackFor, noRollbackFor, etc., each controlling specific transaction behavior.

Important notes: only external method calls are intercepted; internal calls within the same class bypass the proxy, and private/protected/default visibility methods are ignored.

In summary, to make @Transactional work as expected, ensure the method is public, defined in a Spring bean, and invoked through that bean reference.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javatransactionaopspringannotationtransactional
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.