Spring @Transactional applies both Jdk dynamic proxy and aspect aspect

I am trying to add Spring declarative transactions through the @Transactional annotation to an existing Java project.

When I ran into a problem (not related to this issue), I turned on full debug logging. Curiously, I noticed the following:

17: 47: 27,834 DEBUG HibernateTransactionManager: 437 - Found thread-bound Session [ org.hibernate.impl.SessionImpl@10ed8a8e ] for Hibernate transaction
17: 47: 27,845 DEBUG HibernateTransactionManager: 470 - Participating in existing transaction
17: 47: 27,865 DEBUG AnnotationTransactionAttributeSource: 106 - Adding transactional method 'updateUserProfile' with attribute: PROPAGATION_REQUIRED, ISOLATION_DEFAULT; ''
17: 47: 27,875 DEBUG AnnotationTransactionAspect: 321 - Skipping transactional joinpoint [se.myservice.UserService.updateUserProfile] because no transaction manager has been configured

After some debugging, I found out that the first three log entries that say they found a thread-bound session and use this transaction are created by JdkDynamicAopProxy in my UserService class.

The latest journal post looks alarming. It is called at the junction point before the method executes. When you look at the source of AnnotationTransactionAspect, it generates this message if no transaction manager is installed. In this case, since Spring never performs any dependency injection on this aspect.

It seems to me that two different "styles" of transactions are used: a dynamic proxy server and an aspect. The only transaction-related configuration I have is:

<tx:annotation-driven transaction-manager="txManager" />

We use AspectJ in the project, but there is no AnnotationTransactionAspect aspect registered in my aop.xml. We are using Spring 3.0.2.RELEASE.

Should I be alarmed by this? Spring register this aspect for me? Should I use annotation-drivenwhen using AspectJ?

+5
source share
2 answers

Strange, it looks like you have this configuration:

<tx:annotation-driven
    transaction-manager="transactionManager" mode="aspectj" />

(Transaction support using AspectJ, not JDK proxies)

mode, ( ). AnnotationTransactionAspect - , aspectj.

+8

aspectj, java.

@EnableWebMvc
@Configuration
@ComponentScan("com.yourdomain")
@EnableTransactionManagement(mode=AdviceMode.ASPECTJ)
public class ApplicationConfig extends WebMvcConfigurerAdapter {

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

        //...
    }

    @Bean
    public JpaTransactionManager transactionManager() {

        JpaTransactionManager bean = new JpaTransactionManager(entityManagerFactory().getObject());
        return bean ;
    }

    @Bean
    public AnnotationTransactionAspect annotationTransactionAspect() {

        AnnotationTransactionAspect bean = AnnotationTransactionAspect.aspectOf();
        bean.setTransactionManager(transactionManager());
        return bean;
    }
}

maven:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.7</version>
    <configuration>
        <aspectLibraries>
            <aspectLibrary>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
            </aspectLibrary>
        </aspectLibraries>
        <complianceLevel>1.8</complianceLevel>
        <source>1.8</source>
        <target>1.8</target>
        <showWeaveInfo>true</showWeaveInfo>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>

eclipse, , eclipse:

http://www.eclipse.org/ajdt/

+2

All Articles