【问题标题】:Sonar issue - transactions声纳问题 - 交易
【发布时间】:2020-11-14 08:12:02
【问题描述】:

我正在努力解决声纳问题:

squid:S2229 "方法不应调用具有不兼容 "@Transactional" 值的同类方法"

我不确定我应该如何解决这个问题。我应该在 clean 方法上方添加@Transactional 吗?甚至删除@Transactional注解。

@Override
public void clean(BooleanSupplier isInterrupted) {
        // other code
        while (shouldContinue(isInterrupted) && partitionsIterator.hasNext()) {
            PartitionDeleteSql partition = partitionsIterator.next();
            execute(partition);
        }
    }

@Transactional
public void execute(PartitionDeleteSql sql) {
       // other code
       getJdbcTemplate().execute(sql....());
       getJdbcTemplate().execute(sql....());
       getJdbcTemplate().execute(sql....());
}

【问题讨论】:

    标签: java spring spring-data-jpa sonarqube spring-transactions


    【解决方案1】:

    Sonar 指出的问题是非事务性方法clean 调用事务性execute。因此execute 上的@Transactional 注释被忽略,该方法不会在事务模式下执行。

    您必须使用@Transactional 注释clean 方法或整个类。

    此外,必须将类本身注册为 Spring bean,例如使用 @Service@Copmonent,否则不会为此类创建代理包装器 bean。

    阅读更多:Spring - @Transactional - What happens in background?

    【讨论】:

      猜你喜欢
      • 2011-03-02
      • 2012-11-03
      • 2015-12-02
      • 2011-06-15
      • 2016-03-31
      • 1970-01-01
      • 2019-05-25
      • 1970-01-01
      相关资源
      最近更新 更多