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.