Skip to content

SONARJAVA-4881 Update S2230 to only target Spring <= 5 #4969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 12, 2025

Conversation

romainbrenguier
Copy link
Contributor

@romainbrenguier romainbrenguier commented Apr 23, 2025

SONARJAVA-4881

Their is an associated PR on sonar-java to change the behaviour of the rule: #5094

Review

A dedicated reviewer checked the rule description successfully for:

  • logical errors and incorrect information
  • information gaps and missing content
  • text style and tone
  • PR summary and labels follow the guidelines

Nor does Spring make provision for the methods invoked by the method it called.

Therefore marking a private method, for instance, @Transactional can only result in a runtime error or exception if the method is annotated as @Transactional.
Therefore, marking a private method, for instance, @Transactional can only result in a runtime error or exception if the method is annotated as @Transactional.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is true actually. There just seems no way to invoke the private method through the proxy that is generated.
You can call this method just fine by calling it from a public method in the same class, but since it doesn't go through the generated proxy, it will not apply the effect of the @Transactional annotation.

    void delegate() {
        privateMethod();
    }

    @Transactional
    private void privateMethod() {
        jdbcClient.sql("INSERT INTO foo(id) VALUES (1)").update();
        if (true) {
            throw new RuntimeException("Always crashes");
        }
        jdbcClient.sql("INSERT INTO foo(id) VALUES (2)").update();
    }

///
            try {
                testRepo.delegate();
            } catch (Exception e) {
                e.printStackTrace();
            }

            System.out.println("delegate " + testRepo.findAll());
            System.out.println(testRepo.findAll());

Will print delegate [1] even though the private transaction annotated method results in an exception and should have rolled back the insert.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. I've rephrased that part.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, the new description aligns much better with the actual behavior

@romainbrenguier romainbrenguier force-pushed the romain/spring-version-S2230 branch from 59c0bfc to 0a44352 Compare April 24, 2025 07:09
Therefore marking a private method, for instance, @Transactional can only result in a runtime error or exception if the method is annotated as @Transactional.
Therefore, marking a private method, for instance, @Transactional gives a false sense of security, and can lead to incorrect assumptions and potential bugs.

Note that the rule only targets Spring up to Spring 5, because Spring 6 takes into account all non-private methods.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move this sentence into a new Exceptions section?

Their is an associated PR on sonar-java to change the behaviour of the rule:
SonarSource/sonar-java#5094
@romainbrenguier romainbrenguier force-pushed the romain/spring-version-S2230 branch from 515ff23 to 5513bcc Compare April 28, 2025 11:47
Copy link

Quality Gate passed Quality Gate passed for 'rspec-tools'

Issues
0 New issues
0 Fixed issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarQube

Copy link

Quality Gate passed Quality Gate passed for 'rspec-frontend'

Issues
0 New issues
0 Fixed issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarQube

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make sure to merge this update to the documentation in the same sonar-java PR as the implementation change.

@romainbrenguier romainbrenguier added this pull request to the merge queue May 12, 2025
Merged via the queue into master with commit 94eab08 May 12, 2025
9 of 10 checks passed
@romainbrenguier romainbrenguier deleted the romain/spring-version-S2230 branch May 12, 2025 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants