I am trying to synchronize declarative transactions (i.e. methods annotated with @Transactional
) using AspectJ like so:
...
import org.aspectj.lang.annotation.Aspect;
...
@Component
@Aspect
public class TransactionMonitor extends TransactionSynchronizationAdapter {
@Before("execution(@org.springframework.transaction.annotation.Transactional * *.*(..))")
private void registerTransactionSynchronizationOnAnnotation(JoinPoint joinPoint) {
TransactionSynchronizationManager.registerSynchronization(this);
}
}
This currently fails with java.lang.IllegalStateException: Transaction synchronization is not active
which indicates that the synchronization is not run inside the transaction execution, but before. I want to ensure that this is the other way round, of course.
I found this answer, however @Order(Ordered.LOWEST_PRECEDENCE)
had no effect, and
@DeclarePrecedence(
"org.springframework.transaction.aspectj.AnnotationTransactionAspect, xxx.xxx.TransactionMonitor, *"
)
led to this during startup:
java.lang.IllegalArgumentException: DeclarePrecedence not presently supported in Spring AOP
I have the feeling this is AOP and AspectJ not being happy with each other, but I am not sure. I am thankful for any ideas.
EDIT: I have to use @EnableTransactionManagement(proxyTargetClass = true)
, can this be related to the issue?