From 83a3c61b663c46785c90d4a88f7c109e60066991 Mon Sep 17 00:00:00 2001 From: TheCutter Date: Fri, 18 Apr 2014 20:10:32 +0200 Subject: [PATCH] fixed memory leak with closing mysql resources --- .../protection/databases/MySQLDatabase.java | 569 +++++++++++------- 1 file changed, 362 insertions(+), 207 deletions(-) diff --git a/src/main/java/com/sk89q/worldguard/protection/databases/MySQLDatabase.java b/src/main/java/com/sk89q/worldguard/protection/databases/MySQLDatabase.java index 57cd1451..0f07a80a 100755 --- a/src/main/java/com/sk89q/worldguard/protection/databases/MySQLDatabase.java +++ b/src/main/java/com/sk89q/worldguard/protection/databases/MySQLDatabase.java @@ -50,7 +50,6 @@ public class MySQLDatabase extends AbstractProtectionDatabase { private Yaml yaml; private Map regions; - private Map cuboidRegions; private Map poly2dRegions; private Map globalRegions; @@ -66,12 +65,16 @@ public MySQLDatabase(ConfigurationManager config, String world, Logger logger) t String world1 = world; this.logger = logger; + PreparedStatement worldStmt = null; + ResultSet worldResult = null; + PreparedStatement insertWorldStatement = null; try { connect(); + PreparedStatement verTest = null; try { // Test if the database is up to date, if not throw a critical error - PreparedStatement verTest = this.conn.prepareStatement( + verTest = this.conn.prepareStatement( "SELECT `world_id` FROM `region_cuboid` LIMIT 0,1;" ); verTest.execute(); @@ -80,20 +83,23 @@ public MySQLDatabase(ConfigurationManager config, String world, Logger logger) t "region_storage_update_20110325.sql" ); } + finally { + closeResource(verTest); + } - PreparedStatement worldStmt = conn.prepareStatement( + worldStmt = conn.prepareStatement( "SELECT `id` FROM " + "`world` " + "WHERE `name` = ? LIMIT 0,1" ); worldStmt.setString(1, world1); - ResultSet worldResult = worldStmt.executeQuery(); + worldResult = worldStmt.executeQuery(); if (worldResult.first()) { this.worldDbId = worldResult.getInt("id"); } else { - PreparedStatement insertWorldStatement = this.conn.prepareStatement( + insertWorldStatement = this.conn.prepareStatement( "INSERT INTO " + "`world` " + "(`id`, `name`) VALUES (null, ?)", @@ -113,6 +119,11 @@ public MySQLDatabase(ConfigurationManager config, String world, Logger logger) t // initialising the world record, so there is no point continuing return; } + finally { + closeResource(worldResult); + closeResource(worldStmt); + closeResource(insertWorldStatement); + } if (this.worldDbId <= 0) { logger.log(Level.SEVERE, "Could not find or create the world"); @@ -133,9 +144,8 @@ public MySQLDatabase(ConfigurationManager config, String world, Logger logger) t private void connect() throws SQLException { if (conn != null) { - // Make a dummy query to check the connnection is alive. try { - conn.prepareStatement("SELECT 1;").execute(); + conn.isValid(2); } catch (SQLException ex) { // Test if the dummy query failed because the connection is dead, // and if it is mark the connection as closed (the MySQL Driver @@ -153,8 +163,10 @@ private void connect() throws SQLException { private void loadFlags(ProtectedRegion region) { // @TODO: Iterate _ONCE_ + PreparedStatement flagsStatement = null; + ResultSet flagsResultSet = null; try { - PreparedStatement flagsStatement = this.conn.prepareStatement( + flagsStatement = this.conn.prepareStatement( "SELECT " + "`region_flag`.`flag`, " + "`region_flag`.`value` " + @@ -164,7 +176,7 @@ private void loadFlags(ProtectedRegion region) { ); flagsStatement.setString(1, region.getId().toLowerCase()); - ResultSet flagsResultSet = flagsStatement.executeQuery(); + flagsResultSet = flagsStatement.executeQuery(); Map regionFlags = new HashMap(); while (flagsResultSet.next()) { @@ -187,6 +199,10 @@ private void loadFlags(ProtectedRegion region) { + region.getId().toLowerCase() + ": " + ex.getMessage() ); } + finally { + closeResource(flagsResultSet); + closeResource(flagsStatement); + } } private void setFlag(ProtectedRegion region, Flag flag, Object rawValue) { @@ -203,8 +219,10 @@ private void loadOwnersAndMembers(ProtectedRegion region) { DefaultDomain owners = new DefaultDomain(); DefaultDomain members = new DefaultDomain(); + ResultSet userSet = null; + PreparedStatement usersStatement = null; try { - PreparedStatement usersStatement = this.conn.prepareStatement( + usersStatement = this.conn.prepareStatement( "SELECT " + "`user`.`name`, " + "`region_players`.`owner` " + @@ -217,7 +235,7 @@ private void loadOwnersAndMembers(ProtectedRegion region) { ); usersStatement.setString(1, region.getId().toLowerCase()); - ResultSet userSet = usersStatement.executeQuery(); + userSet = usersStatement.executeQuery(); while(userSet.next()) { if (userSet.getBoolean("owner")) { owners.addPlayer(userSet.getString("name")); @@ -228,9 +246,15 @@ private void loadOwnersAndMembers(ProtectedRegion region) { } catch (SQLException ex) { logger.warning("Unable to load users for region " + region.getId().toLowerCase() + ": " + ex.getMessage()); } + finally { + closeResource(userSet); + closeResource(usersStatement); + } + PreparedStatement groupsStatement = null; + ResultSet groupSet = null; try { - PreparedStatement groupsStatement = this.conn.prepareStatement( + groupsStatement = this.conn.prepareStatement( "SELECT " + "`group`.`name`, " + "`region_groups`.`owner` " + @@ -243,7 +267,7 @@ private void loadOwnersAndMembers(ProtectedRegion region) { ); groupsStatement.setString(1, region.getId().toLowerCase()); - ResultSet groupSet = groupsStatement.executeQuery(); + groupSet = groupsStatement.executeQuery(); while(groupSet.next()) { if (groupSet.getBoolean("owner")) { owners.addGroup(groupSet.getString("name")); @@ -254,6 +278,10 @@ private void loadOwnersAndMembers(ProtectedRegion region) { } catch (SQLException ex) { logger.warning("Unable to load groups for region " + region.getId().toLowerCase() + ": " + ex.getMessage()); } + finally { + closeResource(groupSet); + closeResource(groupsStatement); + } region.setOwners(owners); region.setMembers(members); @@ -263,8 +291,10 @@ private void loadGlobal() { Map regions = new HashMap(); + PreparedStatement globalRegionStatement = null; + ResultSet globalResultSet = null; try { - PreparedStatement globalRegionStatement = this.conn.prepareStatement( + globalRegionStatement = this.conn.prepareStatement( "SELECT " + "`region`.`id`, " + "`region`.`priority`, " + @@ -278,7 +308,7 @@ private void loadGlobal() { ); globalRegionStatement.setInt(1, this.worldDbId); - ResultSet globalResultSet = globalRegionStatement.executeQuery(); + globalResultSet = globalRegionStatement.executeQuery(); while (globalResultSet.next()) { ProtectedRegion region = new GlobalProtectedRegion(globalResultSet.getString("id")); @@ -304,6 +334,10 @@ private void loadGlobal() { t = t.getCause(); } } + finally { + closeResource(globalResultSet); + closeResource(globalRegionStatement); + } globalRegions = regions; } @@ -312,8 +346,10 @@ private void loadCuboid() { Map regions = new HashMap(); + PreparedStatement cuboidRegionStatement = null; + ResultSet cuboidResultSet = null; try { - PreparedStatement cuboidRegionStatement = this.conn.prepareStatement( + cuboidRegionStatement = this.conn.prepareStatement( "SELECT " + "`region_cuboid`.`min_z`, " + "`region_cuboid`.`min_y`, " + @@ -335,7 +371,7 @@ private void loadCuboid() { ); cuboidRegionStatement.setInt(1, this.worldDbId); - ResultSet cuboidResultSet = cuboidRegionStatement.executeQuery(); + cuboidResultSet = cuboidRegionStatement.executeQuery(); while (cuboidResultSet.next()) { Vector pt1 = new Vector( @@ -379,6 +415,10 @@ private void loadCuboid() { t = t.getCause(); } } + finally { + closeResource(cuboidResultSet); + closeResource(cuboidRegionStatement); + } cuboidRegions = regions; } @@ -387,8 +427,11 @@ private void loadPoly2d() { Map regions = new HashMap(); + PreparedStatement poly2dRegionStatement = null; + ResultSet poly2dResultSet = null; + PreparedStatement poly2dVectorStatement = null; try { - PreparedStatement poly2dRegionStatement = this.conn.prepareStatement( + poly2dRegionStatement = this.conn.prepareStatement( "SELECT " + "`region_poly2d`.`min_y`, " + "`region_poly2d`.`max_y`, " + @@ -406,9 +449,9 @@ private void loadPoly2d() { ); poly2dRegionStatement.setInt(1, this.worldDbId); - ResultSet poly2dResultSet = poly2dRegionStatement.executeQuery(); + poly2dResultSet = poly2dRegionStatement.executeQuery(); - PreparedStatement poly2dVectorStatement = this.conn.prepareStatement( + poly2dVectorStatement = this.conn.prepareStatement( "SELECT " + "`region_poly2d_point`.`x`, " + "`region_poly2d_point`.`z` " + @@ -433,6 +476,8 @@ private void loadPoly2d() { poly2dVectorResultSet.getInt("z") )); } + closeResource(poly2dVectorResultSet); + ProtectedRegion region = new ProtectedPolygonalRegion(id, points, minY, maxY); region.setPriority(poly2dResultSet.getInt("priority")); @@ -456,6 +501,11 @@ private void loadPoly2d() { t = t.getCause(); } } + finally { + closeResource(poly2dResultSet); + closeResource(poly2dRegionStatement); + closeResource(poly2dVectorStatement); + } poly2dRegions = regions; } @@ -512,8 +562,11 @@ private Map getUserIds(String... usernames) { if (usernames.length < 1) return users; + ResultSet findUsersResults = null; + PreparedStatement insertUserStatement = null; + PreparedStatement findUsersStatement = null; try { - PreparedStatement findUsersStatement = this.conn.prepareStatement( + findUsersStatement = this.conn.prepareStatement( String.format( "SELECT " + "`user`.`id`, " + @@ -526,13 +579,13 @@ private Map getUserIds(String... usernames) { RegionDBUtil.setValues(findUsersStatement, usernames); - ResultSet findUsersResults = findUsersStatement.executeQuery(); + findUsersResults = findUsersStatement.executeQuery(); while(findUsersResults.next()) { users.put(findUsersResults.getString("name"), findUsersResults.getInt("id")); } - PreparedStatement insertUserStatement = this.conn.prepareStatement( + insertUserStatement = this.conn.prepareStatement( "INSERT INTO " + "`user` ( " + "`id`, " + @@ -562,6 +615,11 @@ private Map getUserIds(String... usernames) { t = t.getCause(); } } + finally { + closeResource(findUsersResults); + closeResource(findUsersStatement); + closeResource(insertUserStatement); + } return users; } @@ -576,8 +634,11 @@ private Map getGroupIds(String... groupnames) { if (groupnames.length < 1) return groups; + PreparedStatement findGroupsStatement = null; + ResultSet findGroupsResults = null; + PreparedStatement insertGroupStatement = null; try { - PreparedStatement findGroupsStatement = this.conn.prepareStatement( + findGroupsStatement = this.conn.prepareStatement( String.format( "SELECT " + "`group`.`id`, " + @@ -590,13 +651,13 @@ private Map getGroupIds(String... groupnames) { RegionDBUtil.setValues(findGroupsStatement, groupnames); - ResultSet findGroupsResults = findGroupsStatement.executeQuery(); + findGroupsResults = findGroupsStatement.executeQuery(); while(findGroupsResults.next()) { groups.put(findGroupsResults.getString("name"), findGroupsResults.getInt("id")); } - PreparedStatement insertGroupStatement = this.conn.prepareStatement( + insertGroupStatement = this.conn.prepareStatement( "INSERT INTO " + "`group` ( " + "`id`, " + @@ -620,6 +681,11 @@ private Map getGroupIds(String... groupnames) { } catch (SQLException ex) { logger.warning("Could not get the database id for the groups " + groupnames.toString() + ex.getMessage()); } + finally { + closeResource(findGroupsResults); + closeResource(findGroupsStatement); + closeResource(insertGroupStatement); + } return groups; } @@ -649,15 +715,17 @@ public void save() throws ProtectionDatabaseException { List regionsInDatabase = new ArrayList(); + PreparedStatement getAllRegionsStatement = null; + ResultSet getAllRegionsResult = null; try { - PreparedStatement getAllRegionsStatement = this.conn.prepareStatement( + getAllRegionsStatement = this.conn.prepareStatement( "SELECT `region`.`id` FROM " + "`region` " + "WHERE `world_id` = ? " ); getAllRegionsStatement.setInt(1, this.worldDbId); - ResultSet getAllRegionsResult = getAllRegionsStatement.executeQuery(); + getAllRegionsResult = getAllRegionsStatement.executeQuery(); while(getAllRegionsResult.next()) { regionsInDatabase.add(getAllRegionsResult.getString("id")); @@ -665,6 +733,10 @@ public void save() throws ProtectionDatabaseException { } catch (SQLException ex) { logger.warning("Could not get region list for save comparison: " + ex.getMessage()); } + finally { + closeResource(getAllRegionsResult); + closeResource(getAllRegionsStatement); + } for (Map.Entry entry : regions.entrySet()) { String name = entry.getKey(); @@ -701,10 +773,11 @@ public void save() throws ProtectionDatabaseException { } for (Map.Entry entry : regions.entrySet()) { + PreparedStatement setParentStatement = null; try { if (entry.getValue().getParent() == null) continue; - PreparedStatement setParentStatement = this.conn.prepareStatement( + setParentStatement = this.conn.prepareStatement( "UPDATE `region` SET " + "`parent` = ? " + "WHERE `id` = ? AND `world_id` = " + this.worldDbId @@ -718,11 +791,15 @@ public void save() throws ProtectionDatabaseException { logger.warning("Could not save region parents " + entry.getValue().getId().toLowerCase() + ": " + ex.getMessage()); throw new ProtectionDatabaseException(ex); } + finally { + closeResource(setParentStatement); + } } for (String name : regionsInDatabase) { + PreparedStatement removeRegion = null; try { - PreparedStatement removeRegion = this.conn.prepareStatement( + removeRegion = this.conn.prepareStatement( "DELETE FROM `region` WHERE `id` = ? " ); @@ -731,40 +808,53 @@ public void save() throws ProtectionDatabaseException { } catch (SQLException ex) { logger.warning("Could not remove region from database " + name + ": " + ex.getMessage()); } + finally { + closeResource(removeRegion); + } } } private void updateFlags(ProtectedRegion region) throws SQLException { - PreparedStatement clearCurrentFlagStatement = this.conn.prepareStatement( - "DELETE FROM `region_flag` " + - "WHERE `region_id` = ? " + - "AND `world_id` = " + this.worldDbId - ); - - clearCurrentFlagStatement.setString(1, region.getId().toLowerCase()); - clearCurrentFlagStatement.execute(); - - for (Map.Entry, Object> entry : region.getFlags().entrySet()) { - if (entry.getValue() == null) continue; - - Object flag = sqlMarshal(marshalFlag(entry.getKey(), entry.getValue())); - - PreparedStatement insertFlagStatement = this.conn.prepareStatement( - "INSERT INTO `region_flag` ( " + - "`id`, " + - "`region_id`, " + - "`world_id`, " + - "`flag`, " + - "`value` " + - ") VALUES (null, ?, " + this.worldDbId + ", ?, ?)" + PreparedStatement clearCurrentFlagStatement = null; + try { + clearCurrentFlagStatement = this.conn.prepareStatement( + "DELETE FROM `region_flag` " + + "WHERE `region_id` = ? " + + "AND `world_id` = " + this.worldDbId ); - insertFlagStatement.setString(1, region.getId().toLowerCase()); - insertFlagStatement.setString(2, entry.getKey().getName()); - insertFlagStatement.setObject(3, flag); + clearCurrentFlagStatement.setString(1, region.getId().toLowerCase()); + clearCurrentFlagStatement.execute(); - insertFlagStatement.execute(); + for (Map.Entry, Object> entry : region.getFlags().entrySet()) { + if (entry.getValue() == null) continue; + + Object flag = sqlMarshal(marshalFlag(entry.getKey(), entry.getValue())); + + PreparedStatement insertFlagStatement = null; + try { + insertFlagStatement = this.conn.prepareStatement( + "INSERT INTO `region_flag` ( " + + "`id`, " + + "`region_id`, " + + "`world_id`, " + + "`flag`, " + + "`value` " + + ") VALUES (null, ?, " + this.worldDbId + ", ?, ?)" + ); + + insertFlagStatement.setString(1, region.getId().toLowerCase()); + insertFlagStatement.setString(2, entry.getKey().getName()); + insertFlagStatement.setObject(3, flag); + + insertFlagStatement.execute(); + } finally { + closeResource(insertFlagStatement); + } + } + } finally { + closeResource(clearCurrentFlagStatement); } } @@ -777,57 +867,69 @@ private void updatePlayerAndGroups(ProtectedRegion region, Boolean owners) throw domain = region.getMembers(); } - PreparedStatement deleteUsersForRegion = this.conn.prepareStatement( - "DELETE FROM `region_players` " + - "WHERE `region_id` = ? " + - "AND `world_id` = " + this.worldDbId + " " + - "AND `owner` = ?" - ); + PreparedStatement deleteUsersForRegion = null; + PreparedStatement insertUsersForRegion = null; + PreparedStatement deleteGroupsForRegion = null; + PreparedStatement insertGroupsForRegion = null; - deleteUsersForRegion.setString(1, region.getId().toLowerCase()); - deleteUsersForRegion.setBoolean(2, owners); - deleteUsersForRegion.execute(); + try { + deleteUsersForRegion = this.conn.prepareStatement( + "DELETE FROM `region_players` " + + "WHERE `region_id` = ? " + + "AND `world_id` = " + this.worldDbId + " " + + "AND `owner` = ?" + ); - PreparedStatement insertUsersForRegion = this.conn.prepareStatement( - "INSERT INTO `region_players` " + - "(`region_id`, `world_id`, `user_id`, `owner`) " + - "VALUES (?, " + this.worldDbId + ", ?, ?)" - ); + deleteUsersForRegion.setString(1, region.getId().toLowerCase()); + deleteUsersForRegion.setBoolean(2, owners); + deleteUsersForRegion.execute(); - Set var = domain.getPlayers(); + insertUsersForRegion = this.conn.prepareStatement( + "INSERT INTO `region_players` " + + "(`region_id`, `world_id`, `user_id`, `owner`) " + + "VALUES (?, " + this.worldDbId + ", ?, ?)" + ); - for (Integer player : getUserIds(var.toArray(new String[var.size()])).values()) { - insertUsersForRegion.setString(1, region.getId().toLowerCase()); - insertUsersForRegion.setInt(2, player); - insertUsersForRegion.setBoolean(3, owners); + Set var = domain.getPlayers(); - insertUsersForRegion.execute(); - } + for (Integer player : getUserIds(var.toArray(new String[var.size()])).values()) { + insertUsersForRegion.setString(1, region.getId().toLowerCase()); + insertUsersForRegion.setInt(2, player); + insertUsersForRegion.setBoolean(3, owners); - PreparedStatement deleteGroupsForRegion = this.conn.prepareStatement( - "DELETE FROM `region_groups` " + - "WHERE `region_id` = ? " + - "AND `world_id` = " + this.worldDbId + " " + - "AND `owner` = ?" - ); + insertUsersForRegion.execute(); + } - deleteGroupsForRegion.setString(1, region.getId().toLowerCase()); - deleteGroupsForRegion.setBoolean(2, owners); - deleteGroupsForRegion.execute(); + deleteGroupsForRegion = this.conn.prepareStatement( + "DELETE FROM `region_groups` " + + "WHERE `region_id` = ? " + + "AND `world_id` = " + this.worldDbId + " " + + "AND `owner` = ?" + ); - PreparedStatement insertGroupsForRegion = this.conn.prepareStatement( - "INSERT INTO `region_groups` " + - "(`region_id`, `world_id`, `group_id`, `owner`) " + - "VALUES (?, " + this.worldDbId + ", ?, ?)" - ); + deleteGroupsForRegion.setString(1, region.getId().toLowerCase()); + deleteGroupsForRegion.setBoolean(2, owners); + deleteGroupsForRegion.execute(); - Set groupVar = domain.getGroups(); - for (Integer group : getGroupIds(groupVar.toArray(new String[groupVar.size()])).values()) { - insertGroupsForRegion.setString(1, region.getId().toLowerCase()); - insertGroupsForRegion.setInt(2, group); - insertGroupsForRegion.setBoolean(3, owners); + insertGroupsForRegion = this.conn.prepareStatement( + "INSERT INTO `region_groups` " + + "(`region_id`, `world_id`, `group_id`, `owner`) " + + "VALUES (?, " + this.worldDbId + ", ?, ?)" + ); - insertGroupsForRegion.execute(); + Set groupVar = domain.getGroups(); + for (Integer group : getGroupIds(groupVar.toArray(new String[groupVar.size()])).values()) { + insertGroupsForRegion.setString(1, region.getId().toLowerCase()); + insertGroupsForRegion.setInt(2, group); + insertGroupsForRegion.setBoolean(3, owners); + + insertGroupsForRegion.execute(); + } + } finally { + closeResource(deleteGroupsForRegion); + closeResource(deleteUsersForRegion); + closeResource(insertGroupsForRegion); + closeResource(insertUsersForRegion); } } @@ -837,22 +939,27 @@ private Object marshalFlag(Flag flag, Object val) { } private void insertRegion(ProtectedRegion region, String type) throws SQLException { - PreparedStatement insertRegionStatement = this.conn.prepareStatement( - "INSERT INTO `region` (" + - "`id`, " + - "`world_id`, " + - "`type`, " + - "`priority`, " + - "`parent` " + - ") VALUES (?, ?, ?, ?, null)" - ); + PreparedStatement insertRegionStatement = null; + try { + insertRegionStatement = this.conn.prepareStatement( + "INSERT INTO `region` (" + + "`id`, " + + "`world_id`, " + + "`type`, " + + "`priority`, " + + "`parent` " + + ") VALUES (?, ?, ?, ?, null)" + ); - insertRegionStatement.setString(1, region.getId().toLowerCase()); - insertRegionStatement.setInt(2, this.worldDbId); - insertRegionStatement.setString(3, type); - insertRegionStatement.setInt(4, region.getPriority()); + insertRegionStatement.setString(1, region.getId().toLowerCase()); + insertRegionStatement.setInt(2, this.worldDbId); + insertRegionStatement.setString(3, type); + insertRegionStatement.setInt(4, region.getPriority()); - insertRegionStatement.execute(); + insertRegionStatement.execute(); + } finally { + closeResource(insertRegionStatement); + } updateFlags(region); @@ -863,82 +970,100 @@ private void insertRegion(ProtectedRegion region, String type) throws SQLExcepti private void insertRegionCuboid(ProtectedCuboidRegion region) throws SQLException { insertRegion(region, "cuboid"); - PreparedStatement insertCuboidRegionStatement = this.conn.prepareStatement( - "INSERT INTO `region_cuboid` (" + - "`region_id`, " + - "`world_id`, " + - "`min_z`, " + - "`min_y`, " + - "`min_x`, " + - "`max_z`, " + - "`max_y`, " + - "`max_x` " + - ") VALUES (?, " + this.worldDbId + ", ?, ?, ?, ?, ?, ?)" - ); + PreparedStatement insertCuboidRegionStatement = null; + try { + insertCuboidRegionStatement = this.conn.prepareStatement( + "INSERT INTO `region_cuboid` (" + + "`region_id`, " + + "`world_id`, " + + "`min_z`, " + + "`min_y`, " + + "`min_x`, " + + "`max_z`, " + + "`max_y`, " + + "`max_x` " + + ") VALUES (?, " + this.worldDbId + ", ?, ?, ?, ?, ?, ?)" + ); - BlockVector min = region.getMinimumPoint(); - BlockVector max = region.getMaximumPoint(); + BlockVector min = region.getMinimumPoint(); + BlockVector max = region.getMaximumPoint(); - insertCuboidRegionStatement.setString(1, region.getId().toLowerCase()); - insertCuboidRegionStatement.setInt(2, min.getBlockZ()); - insertCuboidRegionStatement.setInt(3, min.getBlockY()); - insertCuboidRegionStatement.setInt(4, min.getBlockX()); - insertCuboidRegionStatement.setInt(5, max.getBlockZ()); - insertCuboidRegionStatement.setInt(6, max.getBlockY()); - insertCuboidRegionStatement.setInt(7, max.getBlockX()); + insertCuboidRegionStatement.setString(1, region.getId().toLowerCase()); + insertCuboidRegionStatement.setInt(2, min.getBlockZ()); + insertCuboidRegionStatement.setInt(3, min.getBlockY()); + insertCuboidRegionStatement.setInt(4, min.getBlockX()); + insertCuboidRegionStatement.setInt(5, max.getBlockZ()); + insertCuboidRegionStatement.setInt(6, max.getBlockY()); + insertCuboidRegionStatement.setInt(7, max.getBlockX()); - insertCuboidRegionStatement.execute(); + insertCuboidRegionStatement.execute(); + } finally { + closeResource(insertCuboidRegionStatement); + } } private void insertRegionPoly2D(ProtectedPolygonalRegion region) throws SQLException { insertRegion(region, "poly2d"); - PreparedStatement insertPoly2dRegionStatement = this.conn.prepareStatement( - "INSERT INTO `region_poly2d` (" + - "`region_id`, " + - "`world_id`, " + - "`max_y`, " + - "`min_y` " + - ") VALUES (?, " + this.worldDbId + ", ?, ?)" - ); + PreparedStatement insertPoly2dRegionStatement = null; + try { + insertPoly2dRegionStatement = this.conn.prepareStatement( + "INSERT INTO `region_poly2d` (" + + "`region_id`, " + + "`world_id`, " + + "`max_y`, " + + "`min_y` " + + ") VALUES (?, " + this.worldDbId + ", ?, ?)" + ); - insertPoly2dRegionStatement.setString(1, region.getId().toLowerCase()); - insertPoly2dRegionStatement.setInt(2, region.getMaximumPoint().getBlockY()); - insertPoly2dRegionStatement.setInt(3, region.getMinimumPoint().getBlockY()); + insertPoly2dRegionStatement.setString(1, region.getId().toLowerCase()); + insertPoly2dRegionStatement.setInt(2, region.getMaximumPoint().getBlockY()); + insertPoly2dRegionStatement.setInt(3, region.getMinimumPoint().getBlockY()); - insertPoly2dRegionStatement.execute(); + insertPoly2dRegionStatement.execute(); + } finally { + closeResource(insertPoly2dRegionStatement); + } updatePoly2dPoints(region); } private void updatePoly2dPoints(ProtectedPolygonalRegion region) throws SQLException { - PreparedStatement clearPoly2dPointsForRegionStatement = this.conn.prepareStatement( - "DELETE FROM `region_poly2d_point` " + - "WHERE `region_id` = ? " + - "AND `world_id` = " + this.worldDbId - ); + PreparedStatement clearPoly2dPointsForRegionStatement = null; + PreparedStatement insertPoly2dPointStatement = null; - clearPoly2dPointsForRegionStatement.setString(1, region.getId().toLowerCase()); + try { + clearPoly2dPointsForRegionStatement = this.conn.prepareStatement( + "DELETE FROM `region_poly2d_point` " + + "WHERE `region_id` = ? " + + "AND `world_id` = " + this.worldDbId + ); - clearPoly2dPointsForRegionStatement.execute(); + clearPoly2dPointsForRegionStatement.setString(1, region.getId().toLowerCase()); - PreparedStatement insertPoly2dPointStatement = this.conn.prepareStatement( - "INSERT INTO `region_poly2d_point` (" + - "`id`, " + - "`region_id`, " + - "`world_id`, " + - "`z`, " + - "`x` " + - ") VALUES (null, ?, " + this.worldDbId + ", ?, ?)" - ); + clearPoly2dPointsForRegionStatement.execute(); - String lowerId = region.getId(); - for (BlockVector2D point : region.getPoints()) { - insertPoly2dPointStatement.setString(1, lowerId); - insertPoly2dPointStatement.setInt(2, point.getBlockZ()); - insertPoly2dPointStatement.setInt(3, point.getBlockX()); + insertPoly2dPointStatement = this.conn.prepareStatement( + "INSERT INTO `region_poly2d_point` (" + + "`id`, " + + "`region_id`, " + + "`world_id`, " + + "`z`, " + + "`x` " + + ") VALUES (null, ?, " + this.worldDbId + ", ?, ?)" + ); - insertPoly2dPointStatement.execute(); + String lowerId = region.getId(); + for (BlockVector2D point : region.getPoints()) { + insertPoly2dPointStatement.setString(1, lowerId); + insertPoly2dPointStatement.setInt(2, point.getBlockZ()); + insertPoly2dPointStatement.setInt(3, point.getBlockX()); + + insertPoly2dPointStatement.execute(); + } + } finally { + closeResource(clearPoly2dPointsForRegionStatement); + closeResource(insertPoly2dPointStatement); } } @@ -947,15 +1072,20 @@ private void insertRegionGlobal(GlobalProtectedRegion region) throws SQLExceptio } private void updateRegion(ProtectedRegion region, String type) throws SQLException { - PreparedStatement updateRegionStatement = this.conn.prepareStatement( - "UPDATE `region` SET " + - "`priority` = ? WHERE `id` = ? AND `world_id` = " + this.worldDbId - ); + PreparedStatement updateRegionStatement = null; + try { + updateRegionStatement = this.conn.prepareStatement( + "UPDATE `region` SET " + + "`priority` = ? WHERE `id` = ? AND `world_id` = " + this.worldDbId + ); - updateRegionStatement.setInt(1, region.getPriority()); - updateRegionStatement.setString(2, region.getId().toLowerCase()); + updateRegionStatement.setInt(1, region.getPriority()); + updateRegionStatement.setString(2, region.getId().toLowerCase()); - updateRegionStatement.execute(); + updateRegionStatement.execute(); + } finally { + closeResource(updateRegionStatement); + } updateFlags(region); @@ -966,49 +1096,58 @@ private void updateRegion(ProtectedRegion region, String type) throws SQLExcepti private void updateRegionCuboid(ProtectedCuboidRegion region) throws SQLException { updateRegion(region, "cuboid"); - PreparedStatement updateCuboidRegionStatement = this.conn.prepareStatement( - "UPDATE `region_cuboid` SET " + - "`min_z` = ?, " + - "`min_y` = ?, " + - "`min_x` = ?, " + - "`max_z` = ?, " + - "`max_y` = ?, " + - "`max_x` = ? " + - "WHERE `region_id` = ? " + - "AND `world_id` = " + this.worldDbId - ); + PreparedStatement updateCuboidRegionStatement = null; + try { + updateCuboidRegionStatement = this.conn.prepareStatement( + "UPDATE `region_cuboid` SET " + + "`min_z` = ?, " + + "`min_y` = ?, " + + "`min_x` = ?, " + + "`max_z` = ?, " + + "`max_y` = ?, " + + "`max_x` = ? " + + "WHERE `region_id` = ? " + + "AND `world_id` = " + this.worldDbId + ); - BlockVector min = region.getMinimumPoint(); - BlockVector max = region.getMaximumPoint(); + BlockVector min = region.getMinimumPoint(); + BlockVector max = region.getMaximumPoint(); - updateCuboidRegionStatement.setInt(1, min.getBlockZ()); - updateCuboidRegionStatement.setInt(2, min.getBlockY()); - updateCuboidRegionStatement.setInt(3, min.getBlockX()); - updateCuboidRegionStatement.setInt(4, max.getBlockZ()); - updateCuboidRegionStatement.setInt(5, max.getBlockY()); - updateCuboidRegionStatement.setInt(6, max.getBlockX()); - updateCuboidRegionStatement.setString(7, region.getId().toLowerCase()); + updateCuboidRegionStatement.setInt(1, min.getBlockZ()); + updateCuboidRegionStatement.setInt(2, min.getBlockY()); + updateCuboidRegionStatement.setInt(3, min.getBlockX()); + updateCuboidRegionStatement.setInt(4, max.getBlockZ()); + updateCuboidRegionStatement.setInt(5, max.getBlockY()); + updateCuboidRegionStatement.setInt(6, max.getBlockX()); + updateCuboidRegionStatement.setString(7, region.getId().toLowerCase()); - updateCuboidRegionStatement.execute(); + updateCuboidRegionStatement.execute(); + } finally { + closeResource(updateCuboidRegionStatement); + } } private void updateRegionPoly2D(ProtectedPolygonalRegion region) throws SQLException { updateRegion(region, "poly2d"); - PreparedStatement updatePoly2dRegionStatement = this.conn.prepareStatement( - "UPDATE `region_poly2d` SET " + - "`max_y` = ?, " + - "`min_y` = ? " + - "WHERE `region_id` = ? " + - "AND `world_id` = " + this.worldDbId - ); + PreparedStatement updatePoly2dRegionStatement = null; + try { + updatePoly2dRegionStatement = this.conn.prepareStatement( + "UPDATE `region_poly2d` SET " + + "`max_y` = ?, " + + "`min_y` = ? " + + "WHERE `region_id` = ? " + + "AND `world_id` = " + this.worldDbId + ); - updatePoly2dRegionStatement.setInt(1, region.getMaximumPoint().getBlockY()); - updatePoly2dRegionStatement.setInt(2, region.getMinimumPoint().getBlockY()); - updatePoly2dRegionStatement.setString(3, region.getId().toLowerCase()); - - updatePoly2dRegionStatement.execute(); + updatePoly2dRegionStatement.setInt(1, region.getMaximumPoint().getBlockY()); + updatePoly2dRegionStatement.setInt(2, region.getMinimumPoint().getBlockY()); + updatePoly2dRegionStatement.setString(3, region.getId().toLowerCase()); + updatePoly2dRegionStatement.execute(); + } finally { + closeResource(updatePoly2dRegionStatement); + } updatePoly2dPoints(region); } @@ -1016,6 +1155,22 @@ private void updateRegionGlobal(GlobalProtectedRegion region) throws SQLExceptio updateRegion(region, "global"); } + private void closeResource(ResultSet rs) { + if (rs != null) { + try { + rs.close(); + } catch (SQLException e) {} + } + } + + private void closeResource(Statement st) { + if (st != null) { + try { + st.close(); + } catch (SQLException e) {} + } + } + @Override public Map getRegions() { return regions;