Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sonar issue - transactions

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....());
}
like image 787
pqa1222 Avatar asked Oct 23 '25 10:10

pqa1222


1 Answers

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: Spring - @Transactional - What happens in background?

like image 131
Nikolas Charalambidis Avatar answered Oct 25 '25 00:10

Nikolas Charalambidis