声纳问题 – 交易

huangapple go评论108阅读模式
英文:

Sonar issue - transactions

问题

我在处理声纳问题:

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

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

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

@Transactional
public void execute(PartitionDeleteSql sql) {
       // 其他代码
       getJdbcTemplate().execute(sql....());
       getJdbcTemplate().execute(sql....());
       getJdbcTemplate().execute(sql....());
}
英文:

I'm struggling with sonar issue:

> squid:S2229 "Methods should not call same-class methods with incompatible "@Transactional" values"

I'm not sure how am I supposed to resolve this. Should I add @Transactional above clean method or something? Or even delete @Transactional annotation.

@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....());
}

答案1

得分: 2

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

您必须在clean方法或整个类上注释@Transactional

另外,这个类本身必须被注册为Spring bean,例如使用@Service@Component注解,否则代理包装的bean将不会为这样的类创建。

了解更多:https://stackoverflow.com/questions/1099025/spring-transactional-what-happens-in-background

英文:

The problem that Sonar points as is that the non-transactional method clean calls transactional execute. Therefore the @Transactional annotation on execute is ignored and the method will not get executed in the transactional mode.

You have to annotate either clean method or the whole class with @Transactional.

Also the class itself has to be registered as a Spring bean using for example @Service or @Copmonent, otherwise the proxy wrapper bean will not be created for such class.

Read more at: https://stackoverflow.com/questions/1099025/spring-transactional-what-happens-in-background

huangapple
  • 本文由 发表于 2020年7月24日 20:51:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63073944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定