Spring @Async and the AbstractRoutingDataSource
Think I've found the answer: method execution interception will fail to set the variable on the ThreadLocal
, as the AsyncExecutionInterceptor
will always have higher precedence and start the Hibernate transaction. Instead, what I've done is externalized the logic from the async method to its own class, and marked that method as requiring its own transaction, via @Transactional(propagation=Propagation.REQUIRES_NEW)
. Since the sub-method now runs in its own transaction, the variable on the ThreadLocal
is correctly picked up by the AbstractRoutingDataSource
at the onset of the new Hibernate transaction.