微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当我使用 EnableTransactionManegment(mode=Advice.ASPECTJ) 时,为什么 Spring 事务不起作用? 加载时间编织 (LTW)编译时编织 (CTW)

如何解决当我使用 EnableTransactionManegment(mode=Advice.ASPECTJ) 时,为什么 Spring 事务不起作用? 加载时间编织 (LTW)编译时编织 (CTW)

我尝试使用带有 AspectJ 的 Spring 事务

**我的项目: build.config

plugins {

    id 'org.springframework.boot' version '2.3.3.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    
    id "io.freefair.aspectj.post-compile-weaving" version "5.1.0"
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

project.ext {
    aspectjVersion = "1.8.2"
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework:spring-jdbc'
    implementation 'org.springframework:spring-tx'

    implementation 'org.postgresql:postgresql'

    implementation 'org.aspectj:aspectjrt'
    implementation 'org.aspectj:aspectjweaver'
    implementation 'org.aspectj:aspectjtools'
    implementation 'org.springframework:spring-aspects:5.3.2'
    implementation 'org.springframework:spring-instrument:5.3.2'

}

test {
    useJUnitPlatform()
}

DataConfig.class

   @Configuration
   @EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
   public class DataConfig {

     @Bean
     public DataSource postgres() {
         DriverManagerDataSource dataSource = new DriverManagerDataSource();
         dataSource.setDriverClassName("org.postgresql.Driver");
         dataSource.setUrl("jdbc:postgresql://localhost:5432/postgres?serverTimezone=UTC");
         dataSource.setUsername("postgres");
         dataSource.setPassword("admin");
         dataSource.setSchema("public");
         return dataSource;
     }

     @Bean
     public PlatformTransactionManager transactionManager() {
         return new DataSourceTransactionManager(postgres());
     }
  }

MainDAO.class

@Repository
public class MainDAO {
    private final JdbcTemplate jdbcTemplate;
    

    public MainDAO(DataSource postgres) {
        this.jdbcTemplate = new JdbcTemplate(postgres);
    }

    public Integer getSoundsCount() {
        return jdbcTemplate.queryForObject(
                "SELECT COUNT(*) FROM Sound",Integer.class);
    }


    @Transactional(propagation = Propagation.MANDATORY)
    public void insertSound() {
        insertAuthor();
        jdbcTemplate.update(
                "INSERT INTO Sound (author,name,id) VALUES (?,?,?)","Spring",0);
    }
}

当我从我的服务中调用方法 insertSound 时,它无一例外地运行。 但它应该抛出异常,因为方法 insertSound 有传播。MANDATORY。

enter image description here

如果我将 EnableTransactionManegment 的 Advice 模式更改为 mode=Advice.PROXY 然后我得到异常

enter image description here

但是使用 mode=Advice.ASPECTJ,事务不起作用。

我还尝试使用注释 EnableLoadTimeWeaving 运行应用程序并将库 spring-instrument 设置为 java 代理,但它也无法进行事务:

enter image description here

enter image description here

我应该怎么做才能使交易与 mode=Advice.ASPECTJ 一起工作?

解决方法

加载时间编织 (LTW)

为了使 LTW 能够正常工作,您可以在命令行中将 -javaagent:path/to/aspectjweaver.jar-javaagent:path/to/spring-instrument.jar 结合使用,并将您之前提到的 @EnableLoadTimeWeaving 添加到您的配置中。>

编译时编织 (CTW)

我找到了一种使 Spring 的原生 AspectJ 声明式事务与编译时编织一起工作的方法,请参阅我的 pull request。其他细节中的一个关键是:

@Bean
public PlatformTransactionManager transactionManager() {
    DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(h2());
    // Make native AspectJ declarative transactions work with compile-time weaving
    AnnotationTransactionAspect.aspectOf().setTransactionManager(transactionManager);
    return transactionManager;
}

另请参阅 this answer 以供参考。

-javaagent 之后正常运行应用程序时(即没有任何 mvn compile 参数,因为事务方面已经被编译进来),控制台日志应该显示 Native AspectJ active = true 每次 URI {{ 1}} 被调用。该日志行是由我添加的调试语句触发的:

sounds/add

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。