英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论