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.
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.
Cognitive Technology Team
Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.