Ross Laidlaw created OODT-576:
---------------------------------

             Summary: BufferedReader needs to be closed after use in loadScript 
method in SqlScript class
                 Key: OODT-576
                 URL: https://issues.apache.org/jira/browse/OODT-576
             Project: OODT
          Issue Type: Bug
          Components: commons
    Affects Versions: 0.6
            Reporter: Ross Laidlaw
            Assignee: Ross Laidlaw
            Priority: Minor


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

{code:title=SqlScript.java|borderStyle=solid}
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);
    }
  }
}
{code}

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.

{code:title=SqlScript.java|borderStyle=solid}
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();
    }
}
{code}

If OODT is upgraded to use Java 7 at some point in the future, we could look 
into using a 
[try-with-resources|http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html]
 statement to replace the above try...finally combination.


--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to