FN dataSource.getConnection().prepareStatement(...)

On SonarCloud, we scanned the following code:

 try (var connection = dataSource.getConnection().prepareStatement("...")) {
...

This (obviously) does not close the connection acquired via getConnection(). I assume Sonar did not discover this because it is not assigned to a variable.

compliant code:

public Long getNextValue() {
        try (var connection = dataSource.getConnection();
             var preparedStatement = connection.prepareStatement("...")) {
...

I would have expected this to cause S2095 (Resources should be closed)

Hello Daniel,

Thank you for your report. Indeed, the issue here is that the rule considers any resource to be properly handled if it is appears inside the header of a try-with-resources statement, even when the resource is not assigned to a variable.

I’ve created a ticket for this problem.

Cheers,
Sebastion

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.