Search code examples
javaspringspring-data-jpasonarqubespring-transactions

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

Solution

  • 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?