Backend Development 1 min read

Alibaba Java Interview: Does annotating the same Spring Boot service method with @Transactional and @Async cause transaction failure?

In Spring Boot, using @Transactional and @Async on the same service method does not invalidate the transaction, but the asynchronous execution runs in a separate thread with its own transaction, isolated from the original thread's transaction due to AOP interceptor ordering.

Cognitive Technology Team
Cognitive Technology Team
Cognitive Technology Team
Alibaba Java Interview: Does annotating the same Spring Boot service method with @Transactional and @Async cause transaction failure?

Answer: It will not cause the transaction to become invalid, but it will be isolated from the transaction of the current thread.

@Async and @Transactional annotations are both implemented via Spring AOP. The method annotated with @Async is intercepted by AnnotationAsyncExecutionInterceptor , while @Transactional is intercepted by TransactionInterceptor . The interceptor for @Async is placed earlier in the chain, and the interceptor for @Transactional is placed later. Consequently, when the method runs in an asynchronous thread, a new transaction is started, and the differing AOP order leads to distinct transactional behavior.

JavaAOPConcurrencySpring BootasyncTransactional
Cognitive Technology Team
Written by

Cognitive Technology Team

Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.

0 followers
Reader feedback

How this landed with the community

login 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.