Fix resource memory leak introduced by PreparedStatements that were not being closed

This commit is contained in:
Andune
2013-01-12 13:12:34 +05:00
committed by Ammar Askar
parent e361c39937
commit 83de6fc329

View File

@@ -458,7 +458,10 @@ public class Consumer extends TimerTask
public void executeStatements() throws SQLException { public void executeStatements() throws SQLException {
final String table = getWorldConfig(loc.getWorld()).table; final String table = getWorldConfig(loc.getWorld()).table;
PreparedStatement ps1 = connection.prepareStatement("INSERT INTO `" + table + "` (date, playerid, replaced, type, data, x, y, z) VALUES(?, " + playerID(playerName) + ", ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS); PreparedStatement ps1 = null;
PreparedStatement ps = null;
try {
ps1 = connection.prepareStatement("INSERT INTO `" + table + "` (date, playerid, replaced, type, data, x, y, z) VALUES(?, " + playerID(playerName) + ", ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
ps1.setTimestamp(1, new Timestamp(date * 1000)); ps1.setTimestamp(1, new Timestamp(date * 1000));
ps1.setInt(2, replaced); ps1.setInt(2, replaced);
ps1.setInt(3, type); ps1.setInt(3, type);
@@ -474,12 +477,12 @@ public class Consumer extends TimerTask
id = rs.getInt(1); id = rs.getInt(1);
if (signtext != null) { if (signtext != null) {
PreparedStatement ps = connection.prepareStatement("INSERT INTO `" + table + "-sign` (signtext, id) VALUES(?, ?)"); ps = connection.prepareStatement("INSERT INTO `" + table + "-sign` (signtext, id) VALUES(?, ?)");
ps.setString(1, signtext); ps.setString(1, signtext);
ps.setInt(2, id); ps.setInt(2, id);
ps.executeUpdate(); ps.executeUpdate();
} else if (ca != null) { } else if (ca != null) {
PreparedStatement ps = connection.prepareStatement("INSERT INTO `" + table + "-chest` (itemtype, itemamount, itemdata, id) values (?, ?, ?, ?)"); ps = connection.prepareStatement("INSERT INTO `" + table + "-chest` (itemtype, itemamount, itemdata, id) values (?, ?, ?, ?)");
ps.setInt(1, ca.itemType); ps.setInt(1, ca.itemType);
ps.setInt(2, ca.itemAmount); ps.setInt(2, ca.itemAmount);
ps.setInt(3, ca.itemData); ps.setInt(3, ca.itemData);
@@ -487,6 +490,31 @@ public class Consumer extends TimerTask
ps.executeUpdate(); ps.executeUpdate();
} }
} }
// we intentionally do not catch SQLException, it is thrown to the caller
finally {
// individual try/catch here, though ugly, prevents resource leaks
if( ps1 != null ) {
try {
ps1.close();
}
catch(SQLException e) {
// ideally should log to logger, none is available in this class
// at the time of this writing, so I'll leave that to the plugin
// maintainers to integrate if they wish
e.printStackTrace();
}
}
if( ps != null ) {
try {
ps.close();
}
catch(SQLException e) {
e.printStackTrace();
}
}
}
}
} }
private class KillRow implements Row private class KillRow implements Row
@@ -551,7 +579,10 @@ public class Consumer extends TimerTask
sql += "?, "; sql += "?, ";
} }
sql += "?)"; sql += "?)";
PreparedStatement ps = connection.prepareStatement(sql);
PreparedStatement ps = null;
try {
ps = connection.prepareStatement(sql);
ps.setTimestamp(1, new Timestamp(date * 1000)); ps.setTimestamp(1, new Timestamp(date * 1000));
if (!noID) { if (!noID) {
ps.setInt(2, id); ps.setInt(2, id);
@@ -561,6 +592,19 @@ public class Consumer extends TimerTask
} }
ps.execute(); ps.execute();
} }
// we intentionally do not catch SQLException, it is thrown to the caller
finally {
if( ps != null ) {
try {
ps.close();
}
catch(SQLException e) {
// should print to a Logger instead if one is ever added to this class
e.printStackTrace();
}
}
}
}
} }
private class PlayerJoinRow implements Row private class PlayerJoinRow implements Row