Jamie Goodfellow opened SPR-7147 and commented

The AsyncAnnotationBeanPostProcessor class is configured to put the AsyncAnnotationAdvisor last in the advisor chain. In general this may be acceptable, but in the case of using both @Transactional and @Async on a method, this order does not work. The transaction is started on the caller thread and does not apply to the async execution thread. As a simple example of where this applies, consider the following interface method:

@Async @Transactional(readOnly=false, propagation=Propagation.REQUIRES_NEW) void createAuditEvent(CreateAuditEventData data);

Creation of the audit event should not block normal program flow execution, hence @Async. And the method is a single DB Transaction, hence @Transactional.

A quick fix for this issue is to force the async advisor into position 0 in the advisor chain, or at least a position before the transaction advisor. A more robust fix would be to allow order to be specified, similar to how order can be placed on the . The fix would be .


Affects: 3.0.1

Referenced from: commits https://github.com/spring-projects/spring-framework/commit/68055ba4c9b2db737f9f4ec013d78ec0891b4dda

Comment From: spring-projects-issues

Juergen Hoeller commented

Actually, thinking about it, there is no good reason for @Async processing to ever come later in the Advisor chain. This always needs to be the first Advisor in the chain in order to provide meaningful around-invocation semantics.

This was simply a bug before, where the actual order was even dependent on the order of post-processing... I've fixed this for Spring 3.0.3 now.

Juergen

Comment From: spring-projects-issues

Lasse Bergstrom commented

Hi,

We run 3.1.1.RELEASE and still have issues regarding this. Merge problem?

Comment From: spring-projects-issues

Lasse Bergstrom commented

Finally I got it working. The error was that I was passing an @Entity to the the @Async method that in turn called a facade with @Transactional. As soon as I changed the parameter into a primitive it worked.

Hope it helps someone.

Comment From: spring-projects-issues

Chris Beams commented

Glad you worked it out Lasse; thanks for following up!

Comment From: spring-projects-issues

Nick Hristov commented

Hello,

I would like to potentially re-open this bug.

Unfortunately, the fact that @Async is implemented as a separate bean post processor makes it impossible to reasonably @Order it with other Aspects: the options are either @Async always first or always last, depending on the ordering of the bean post processors.

Comment From: HimanshuGuptaDavita

I am trying to achieve transactional with async but no luck.

Comment From: brandonfl

Same here, impossible to make @Async and @Transactional :cry:

Comment From: sbrannen

@HimanshuGuptaDavita anbd @brandonfl, this issue was resolved over a decade ago in May 2010.

If you feel that you have encountered a bug in Spring Framework 5.3.x or 6.0.x, please create a new issue.

Thanks