forked from LogBlock/LogBlock
Fix resource memory leak introduced by PreparedStatements that were not being closed
This commit is contained in:
@@ -458,33 +458,61 @@ 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;
|
||||||
ps1.setTimestamp(1, new Timestamp(date * 1000));
|
PreparedStatement ps = null;
|
||||||
ps1.setInt(2, replaced);
|
try {
|
||||||
ps1.setInt(3, type);
|
ps1 = connection.prepareStatement("INSERT INTO `" + table + "` (date, playerid, replaced, type, data, x, y, z) VALUES(?, " + playerID(playerName) + ", ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
|
||||||
ps1.setInt(4, data);
|
ps1.setTimestamp(1, new Timestamp(date * 1000));
|
||||||
ps1.setInt(5, loc.getBlockX());
|
ps1.setInt(2, replaced);
|
||||||
ps1.setInt(6, loc.getBlockY());
|
ps1.setInt(3, type);
|
||||||
ps1.setInt(7, loc.getBlockZ());
|
ps1.setInt(4, data);
|
||||||
ps1.executeUpdate();
|
ps1.setInt(5, loc.getBlockX());
|
||||||
|
ps1.setInt(6, loc.getBlockY());
|
||||||
int id;
|
ps1.setInt(7, loc.getBlockZ());
|
||||||
ResultSet rs = ps1.getGeneratedKeys();
|
ps1.executeUpdate();
|
||||||
rs.next();
|
|
||||||
id = rs.getInt(1);
|
int id;
|
||||||
|
ResultSet rs = ps1.getGeneratedKeys();
|
||||||
if (signtext != null) {
|
rs.next();
|
||||||
PreparedStatement ps = connection.prepareStatement("INSERT INTO `" + table + "-sign` (signtext, id) VALUES(?, ?)");
|
id = rs.getInt(1);
|
||||||
ps.setString(1, signtext);
|
|
||||||
ps.setInt(2, id);
|
if (signtext != null) {
|
||||||
ps.executeUpdate();
|
ps = connection.prepareStatement("INSERT INTO `" + table + "-sign` (signtext, id) VALUES(?, ?)");
|
||||||
} else if (ca != null) {
|
ps.setString(1, signtext);
|
||||||
PreparedStatement ps = connection.prepareStatement("INSERT INTO `" + table + "-chest` (itemtype, itemamount, itemdata, id) values (?, ?, ?, ?)");
|
ps.setInt(2, id);
|
||||||
ps.setInt(1, ca.itemType);
|
ps.executeUpdate();
|
||||||
ps.setInt(2, ca.itemAmount);
|
} else if (ca != null) {
|
||||||
ps.setInt(3, ca.itemData);
|
ps = connection.prepareStatement("INSERT INTO `" + table + "-chest` (itemtype, itemamount, itemdata, id) values (?, ?, ?, ?)");
|
||||||
ps.setInt(4, id);
|
ps.setInt(1, ca.itemType);
|
||||||
ps.executeUpdate();
|
ps.setInt(2, ca.itemAmount);
|
||||||
|
ps.setInt(3, ca.itemData);
|
||||||
|
ps.setInt(4, id);
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -551,15 +579,31 @@ public class Consumer extends TimerTask
|
|||||||
sql += "?, ";
|
sql += "?, ";
|
||||||
}
|
}
|
||||||
sql += "?)";
|
sql += "?)";
|
||||||
PreparedStatement ps = connection.prepareStatement(sql);
|
|
||||||
ps.setTimestamp(1, new Timestamp(date * 1000));
|
PreparedStatement ps = null;
|
||||||
if (!noID) {
|
try {
|
||||||
ps.setInt(2, id);
|
ps = connection.prepareStatement(sql);
|
||||||
ps.setString(3, message);
|
ps.setTimestamp(1, new Timestamp(date * 1000));
|
||||||
} else {
|
if (!noID) {
|
||||||
ps.setString(2, message);
|
ps.setInt(2, id);
|
||||||
|
ps.setString(3, message);
|
||||||
|
} else {
|
||||||
|
ps.setString(2, message);
|
||||||
|
}
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ps.execute();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user