Uploaded image for project: 'OODT (Retired)'
  1. OODT (Retired)
  2. OODT-576

BufferedReader needs to be closed after use in loadScript method in SqlScript class

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • 0.6
    • 0.6
    • commons
    • None

    Description

      The loadScript method in the org.apache.oodt.commons.database.SqlScript class uses a BufferedReader resource as follows:

      SqlScript.java
      public void loadScript() throws IOException, SQLException {
        BufferedReader reader = new BufferedReader(new FileReader(script));
        String line;
        StringBuffer query = new StringBuffer();
        boolean queryEnds = false;
      
        while ((line = reader.readLine()) != null) {
          if (isComment(line))
            continue;
          queryEnds = checkStatementEnds(line);
          query.append(line);
          if (queryEnds) {
            statementList.add(query.toString());
            query.setLength(0);
          }
        }
      }
      

      The reader should be closed after use, for example using try...finally as shown below. Additionally, we can also remove 'throws SQLException' as the code in this method won't cause this type of exception to be thrown.

      SqlScript.java
      public void loadScript() throws IOException {
          BufferedReader reader = new BufferedReader(new FileReader(script));
      
          try {
              String line = null;
              StringBuffer query = new StringBuffer();
              boolean queryEnds = false;
      
              while ((line = reader.readLine()) != null) {
                  if (isComment(line))
                      continue;
                  queryEnds = checkStatementEnds(line);
                  query.append(line);
                  if (queryEnds) {
                      statementList.add(query.toString());
                      query.setLength(0);
                  }
              }
          }
          finally {
              reader.close();
          }
      }
      

      If OODT is upgraded to use Java 7 at some point in the future, we could look into using a try-with-resources statement to replace the above try...finally combination.

      Attachments

        Activity

          People

            rlaidlaw Ross Laidlaw
            rlaidlaw Ross Laidlaw
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: