Ensure LineNumberReader is always closed

Wrap ResourceDatabasePopulator.readScript() code in a try/finally
block to ensure that the LineNumberReader is always closed.

Issue: SPR-9912
master
Eric Weinberg 12 years ago committed by Phillip Webb
parent 073b627f4e
commit 33abee2db2
  1. 26
      spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java

@ -224,20 +224,24 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
*/
private String readScript(EncodedResource resource) throws IOException {
LineNumberReader lnr = new LineNumberReader(resource.getReader());
String currentStatement = lnr.readLine();
StringBuilder scriptBuilder = new StringBuilder();
while (currentStatement != null) {
if (StringUtils.hasText(currentStatement) &&
(this.commentPrefix != null && !currentStatement.startsWith(this.commentPrefix))) {
if (scriptBuilder.length() > 0) {
scriptBuilder.append('\n');
try {
String currentStatement = lnr.readLine();
StringBuilder scriptBuilder = new StringBuilder();
while (currentStatement != null) {
if (StringUtils.hasText(currentStatement)
&& (this.commentPrefix != null && !currentStatement.startsWith(this.commentPrefix))) {
if (scriptBuilder.length() > 0) {
scriptBuilder.append('\n');
}
scriptBuilder.append(currentStatement);
}
scriptBuilder.append(currentStatement);
currentStatement = lnr.readLine();
}
currentStatement = lnr.readLine();
maybeAddSeparatorToScript(scriptBuilder);
return scriptBuilder.toString();
} finally {
lnr.close();
}
maybeAddSeparatorToScript(scriptBuilder);
return scriptBuilder.toString();
}
private void maybeAddSeparatorToScript(StringBuilder scriptBuilder) {

Loading…
Cancel
Save