Uploaded image for project: 'SonarJava'
  1. SonarJava
  2. SONARJAVA-2060

FP in S2095 : java.sql.Statement will implicitly close created ResultSets

    Details

    • Type: False-Positive
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 6.7
    • Component/s: Symbolic Execution
    • Labels:
      None

      Description

      As documented in javadoc of java.sql.ResultSet

      https://docs.oracle.com/javase/8/docs/api/java/sql/ResultSet.html

      A ResultSet object is automatically closed when the Statement object that generated it is closed, re-executed, or used to retrieve the next result from a sequence of multiple results.

      Currently this is not taken into account in RSPEC-2095 and following code produces FP

      public void properHandlingWithMoreResults(String url, String query) {
          try (Connection connection = DriverManager.getConnection(url);) {
            try (Statement statement = connection.createStatement();) {
              boolean hasResultSets = statement.execute(query);
              while (hasResultSets) {
                ResultSet result = statement.getResultSet(); // Noncompliant {{Close this "ResultSet".}}
                String name = result.getString(0);
                hasResultSets = statement.getMoreResults();
              }
            }
          }
        }
      
          try(Statement stmt = con.createStatement()) {
            ResultSet rs = stmt.executeQuery("SELECT a FROM tbl"); // false-positive
            // ...
          }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                sebastian.hungerecker Sebastian Hungerecker
                Reporter:
                tibor.blenessy Tibor Blenessy
              • Votes:
                3 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: