spring-projects / spring-boot

Spring Boot
https://spring.io/projects/spring-boot
Apache License 2.0
74.97k stars 40.65k forks source link

AspectJ transaction management with compile-time weaving does not work with spring.main.lazy-initialization=true #37506

Closed kicktipp closed 1 year ago

kicktipp commented 3 years ago

We use AspectJ compile Time Weaving to manage @Transactional annotations. Everything works fine until we activate spring.main.lazy-initialization=true. The Transaction is not started.

The error is somewhere in TransactionAspectSupport, as the beanFactory is still null when running with lazy-init activated:

    protected TransactionManager determineTransactionManager(@Nullable TransactionAttribute txAttr) {
        // Do not attempt to lookup tx manager if no tx attributes are set
        if (txAttr == null || this.beanFactory == null) {
            return getTransactionManager();
        }

I hope this is a spring issue and not a Spring Boot issue. I searched the docs if lazy-init is not supported with AspectJ but I did not found anything and didn't any other issue regarding this bug.

Very small project reproducing the issue: https://github.com/kicktipp/spring-lazy-aspectj-issue

Just checkout and run

./gradlew test --tests NonLazyTest
./gradlew test --tests LazyTest

First on with success, second one fails.

If you need further information I am eager to help.

snicoll commented 1 year ago

Thanks very much for the sample, and sorry it took so long to process it. I can confirm the issue, but this shouldn't be addressed in the core framework so I am going to move this issue to the Spring Boot issue tracker.

I've updated your sample to a supported Spring Boot version, and added a way to opt-out explicitly from Lazy processing of TransactionAspectSupport. This made your sample pass this way.

See https://github.com/snicoll-scratches/spring-lazy-aspectj-issue