mirror of
https://github.com/EngineHub/WorldGuard.git
synced 2024-12-18 15:17:36 +01:00
fixed memory leak with closing mysql resources
This commit is contained in:
parent
4422d536b7
commit
83a3c61b66
@ -50,7 +50,6 @@ public class MySQLDatabase extends AbstractProtectionDatabase {
|
|||||||
private Yaml yaml;
|
private Yaml yaml;
|
||||||
|
|
||||||
private Map<String, ProtectedRegion> regions;
|
private Map<String, ProtectedRegion> regions;
|
||||||
|
|
||||||
private Map<String, ProtectedRegion> cuboidRegions;
|
private Map<String, ProtectedRegion> cuboidRegions;
|
||||||
private Map<String, ProtectedRegion> poly2dRegions;
|
private Map<String, ProtectedRegion> poly2dRegions;
|
||||||
private Map<String, ProtectedRegion> globalRegions;
|
private Map<String, ProtectedRegion> globalRegions;
|
||||||
@ -66,12 +65,16 @@ public MySQLDatabase(ConfigurationManager config, String world, Logger logger) t
|
|||||||
String world1 = world;
|
String world1 = world;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
|
|
||||||
|
PreparedStatement worldStmt = null;
|
||||||
|
ResultSet worldResult = null;
|
||||||
|
PreparedStatement insertWorldStatement = null;
|
||||||
try {
|
try {
|
||||||
connect();
|
connect();
|
||||||
|
|
||||||
|
PreparedStatement verTest = null;
|
||||||
try {
|
try {
|
||||||
// Test if the database is up to date, if not throw a critical error
|
// 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;"
|
"SELECT `world_id` FROM `region_cuboid` LIMIT 0,1;"
|
||||||
);
|
);
|
||||||
verTest.execute();
|
verTest.execute();
|
||||||
@ -80,20 +83,23 @@ public MySQLDatabase(ConfigurationManager config, String world, Logger logger) t
|
|||||||
"region_storage_update_20110325.sql"
|
"region_storage_update_20110325.sql"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
finally {
|
||||||
|
closeResource(verTest);
|
||||||
|
}
|
||||||
|
|
||||||
PreparedStatement worldStmt = conn.prepareStatement(
|
worldStmt = conn.prepareStatement(
|
||||||
"SELECT `id` FROM " +
|
"SELECT `id` FROM " +
|
||||||
"`world` " +
|
"`world` " +
|
||||||
"WHERE `name` = ? LIMIT 0,1"
|
"WHERE `name` = ? LIMIT 0,1"
|
||||||
);
|
);
|
||||||
|
|
||||||
worldStmt.setString(1, world1);
|
worldStmt.setString(1, world1);
|
||||||
ResultSet worldResult = worldStmt.executeQuery();
|
worldResult = worldStmt.executeQuery();
|
||||||
|
|
||||||
if (worldResult.first()) {
|
if (worldResult.first()) {
|
||||||
this.worldDbId = worldResult.getInt("id");
|
this.worldDbId = worldResult.getInt("id");
|
||||||
} else {
|
} else {
|
||||||
PreparedStatement insertWorldStatement = this.conn.prepareStatement(
|
insertWorldStatement = this.conn.prepareStatement(
|
||||||
"INSERT INTO " +
|
"INSERT INTO " +
|
||||||
"`world` " +
|
"`world` " +
|
||||||
"(`id`, `name`) VALUES (null, ?)",
|
"(`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
|
// initialising the world record, so there is no point continuing
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
finally {
|
||||||
|
closeResource(worldResult);
|
||||||
|
closeResource(worldStmt);
|
||||||
|
closeResource(insertWorldStatement);
|
||||||
|
}
|
||||||
|
|
||||||
if (this.worldDbId <= 0) {
|
if (this.worldDbId <= 0) {
|
||||||
logger.log(Level.SEVERE, "Could not find or create the world");
|
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 {
|
private void connect() throws SQLException {
|
||||||
if (conn != null) {
|
if (conn != null) {
|
||||||
// Make a dummy query to check the connnection is alive.
|
|
||||||
try {
|
try {
|
||||||
conn.prepareStatement("SELECT 1;").execute();
|
conn.isValid(2);
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
// Test if the dummy query failed because the connection is dead,
|
// Test if the dummy query failed because the connection is dead,
|
||||||
// and if it is mark the connection as closed (the MySQL Driver
|
// 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) {
|
private void loadFlags(ProtectedRegion region) {
|
||||||
// @TODO: Iterate _ONCE_
|
// @TODO: Iterate _ONCE_
|
||||||
|
PreparedStatement flagsStatement = null;
|
||||||
|
ResultSet flagsResultSet = null;
|
||||||
try {
|
try {
|
||||||
PreparedStatement flagsStatement = this.conn.prepareStatement(
|
flagsStatement = this.conn.prepareStatement(
|
||||||
"SELECT " +
|
"SELECT " +
|
||||||
"`region_flag`.`flag`, " +
|
"`region_flag`.`flag`, " +
|
||||||
"`region_flag`.`value` " +
|
"`region_flag`.`value` " +
|
||||||
@ -164,7 +176,7 @@ private void loadFlags(ProtectedRegion region) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
flagsStatement.setString(1, region.getId().toLowerCase());
|
flagsStatement.setString(1, region.getId().toLowerCase());
|
||||||
ResultSet flagsResultSet = flagsStatement.executeQuery();
|
flagsResultSet = flagsStatement.executeQuery();
|
||||||
|
|
||||||
Map<String,Object> regionFlags = new HashMap<String,Object>();
|
Map<String,Object> regionFlags = new HashMap<String,Object>();
|
||||||
while (flagsResultSet.next()) {
|
while (flagsResultSet.next()) {
|
||||||
@ -187,6 +199,10 @@ private void loadFlags(ProtectedRegion region) {
|
|||||||
+ region.getId().toLowerCase() + ": " + ex.getMessage()
|
+ region.getId().toLowerCase() + ": " + ex.getMessage()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
finally {
|
||||||
|
closeResource(flagsResultSet);
|
||||||
|
closeResource(flagsStatement);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> void setFlag(ProtectedRegion region, Flag<T> flag, Object rawValue) {
|
private <T> void setFlag(ProtectedRegion region, Flag<T> flag, Object rawValue) {
|
||||||
@ -203,8 +219,10 @@ private void loadOwnersAndMembers(ProtectedRegion region) {
|
|||||||
DefaultDomain owners = new DefaultDomain();
|
DefaultDomain owners = new DefaultDomain();
|
||||||
DefaultDomain members = new DefaultDomain();
|
DefaultDomain members = new DefaultDomain();
|
||||||
|
|
||||||
|
ResultSet userSet = null;
|
||||||
|
PreparedStatement usersStatement = null;
|
||||||
try {
|
try {
|
||||||
PreparedStatement usersStatement = this.conn.prepareStatement(
|
usersStatement = this.conn.prepareStatement(
|
||||||
"SELECT " +
|
"SELECT " +
|
||||||
"`user`.`name`, " +
|
"`user`.`name`, " +
|
||||||
"`region_players`.`owner` " +
|
"`region_players`.`owner` " +
|
||||||
@ -217,7 +235,7 @@ private void loadOwnersAndMembers(ProtectedRegion region) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
usersStatement.setString(1, region.getId().toLowerCase());
|
usersStatement.setString(1, region.getId().toLowerCase());
|
||||||
ResultSet userSet = usersStatement.executeQuery();
|
userSet = usersStatement.executeQuery();
|
||||||
while(userSet.next()) {
|
while(userSet.next()) {
|
||||||
if (userSet.getBoolean("owner")) {
|
if (userSet.getBoolean("owner")) {
|
||||||
owners.addPlayer(userSet.getString("name"));
|
owners.addPlayer(userSet.getString("name"));
|
||||||
@ -228,9 +246,15 @@ private void loadOwnersAndMembers(ProtectedRegion region) {
|
|||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
logger.warning("Unable to load users for region " + region.getId().toLowerCase() + ": " + ex.getMessage());
|
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 {
|
try {
|
||||||
PreparedStatement groupsStatement = this.conn.prepareStatement(
|
groupsStatement = this.conn.prepareStatement(
|
||||||
"SELECT " +
|
"SELECT " +
|
||||||
"`group`.`name`, " +
|
"`group`.`name`, " +
|
||||||
"`region_groups`.`owner` " +
|
"`region_groups`.`owner` " +
|
||||||
@ -243,7 +267,7 @@ private void loadOwnersAndMembers(ProtectedRegion region) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
groupsStatement.setString(1, region.getId().toLowerCase());
|
groupsStatement.setString(1, region.getId().toLowerCase());
|
||||||
ResultSet groupSet = groupsStatement.executeQuery();
|
groupSet = groupsStatement.executeQuery();
|
||||||
while(groupSet.next()) {
|
while(groupSet.next()) {
|
||||||
if (groupSet.getBoolean("owner")) {
|
if (groupSet.getBoolean("owner")) {
|
||||||
owners.addGroup(groupSet.getString("name"));
|
owners.addGroup(groupSet.getString("name"));
|
||||||
@ -254,6 +278,10 @@ private void loadOwnersAndMembers(ProtectedRegion region) {
|
|||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
logger.warning("Unable to load groups for region " + region.getId().toLowerCase() + ": " + ex.getMessage());
|
logger.warning("Unable to load groups for region " + region.getId().toLowerCase() + ": " + ex.getMessage());
|
||||||
}
|
}
|
||||||
|
finally {
|
||||||
|
closeResource(groupSet);
|
||||||
|
closeResource(groupsStatement);
|
||||||
|
}
|
||||||
|
|
||||||
region.setOwners(owners);
|
region.setOwners(owners);
|
||||||
region.setMembers(members);
|
region.setMembers(members);
|
||||||
@ -263,8 +291,10 @@ private void loadGlobal() {
|
|||||||
Map<String,ProtectedRegion> regions =
|
Map<String,ProtectedRegion> regions =
|
||||||
new HashMap<String,ProtectedRegion>();
|
new HashMap<String,ProtectedRegion>();
|
||||||
|
|
||||||
|
PreparedStatement globalRegionStatement = null;
|
||||||
|
ResultSet globalResultSet = null;
|
||||||
try {
|
try {
|
||||||
PreparedStatement globalRegionStatement = this.conn.prepareStatement(
|
globalRegionStatement = this.conn.prepareStatement(
|
||||||
"SELECT " +
|
"SELECT " +
|
||||||
"`region`.`id`, " +
|
"`region`.`id`, " +
|
||||||
"`region`.`priority`, " +
|
"`region`.`priority`, " +
|
||||||
@ -278,7 +308,7 @@ private void loadGlobal() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
globalRegionStatement.setInt(1, this.worldDbId);
|
globalRegionStatement.setInt(1, this.worldDbId);
|
||||||
ResultSet globalResultSet = globalRegionStatement.executeQuery();
|
globalResultSet = globalRegionStatement.executeQuery();
|
||||||
|
|
||||||
while (globalResultSet.next()) {
|
while (globalResultSet.next()) {
|
||||||
ProtectedRegion region = new GlobalProtectedRegion(globalResultSet.getString("id"));
|
ProtectedRegion region = new GlobalProtectedRegion(globalResultSet.getString("id"));
|
||||||
@ -304,6 +334,10 @@ private void loadGlobal() {
|
|||||||
t = t.getCause();
|
t = t.getCause();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
finally {
|
||||||
|
closeResource(globalResultSet);
|
||||||
|
closeResource(globalRegionStatement);
|
||||||
|
}
|
||||||
|
|
||||||
globalRegions = regions;
|
globalRegions = regions;
|
||||||
}
|
}
|
||||||
@ -312,8 +346,10 @@ private void loadCuboid() {
|
|||||||
Map<String,ProtectedRegion> regions =
|
Map<String,ProtectedRegion> regions =
|
||||||
new HashMap<String,ProtectedRegion>();
|
new HashMap<String,ProtectedRegion>();
|
||||||
|
|
||||||
|
PreparedStatement cuboidRegionStatement = null;
|
||||||
|
ResultSet cuboidResultSet = null;
|
||||||
try {
|
try {
|
||||||
PreparedStatement cuboidRegionStatement = this.conn.prepareStatement(
|
cuboidRegionStatement = this.conn.prepareStatement(
|
||||||
"SELECT " +
|
"SELECT " +
|
||||||
"`region_cuboid`.`min_z`, " +
|
"`region_cuboid`.`min_z`, " +
|
||||||
"`region_cuboid`.`min_y`, " +
|
"`region_cuboid`.`min_y`, " +
|
||||||
@ -335,7 +371,7 @@ private void loadCuboid() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
cuboidRegionStatement.setInt(1, this.worldDbId);
|
cuboidRegionStatement.setInt(1, this.worldDbId);
|
||||||
ResultSet cuboidResultSet = cuboidRegionStatement.executeQuery();
|
cuboidResultSet = cuboidRegionStatement.executeQuery();
|
||||||
|
|
||||||
while (cuboidResultSet.next()) {
|
while (cuboidResultSet.next()) {
|
||||||
Vector pt1 = new Vector(
|
Vector pt1 = new Vector(
|
||||||
@ -379,6 +415,10 @@ private void loadCuboid() {
|
|||||||
t = t.getCause();
|
t = t.getCause();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
finally {
|
||||||
|
closeResource(cuboidResultSet);
|
||||||
|
closeResource(cuboidRegionStatement);
|
||||||
|
}
|
||||||
|
|
||||||
cuboidRegions = regions;
|
cuboidRegions = regions;
|
||||||
}
|
}
|
||||||
@ -387,8 +427,11 @@ private void loadPoly2d() {
|
|||||||
Map<String,ProtectedRegion> regions =
|
Map<String,ProtectedRegion> regions =
|
||||||
new HashMap<String,ProtectedRegion>();
|
new HashMap<String,ProtectedRegion>();
|
||||||
|
|
||||||
|
PreparedStatement poly2dRegionStatement = null;
|
||||||
|
ResultSet poly2dResultSet = null;
|
||||||
|
PreparedStatement poly2dVectorStatement = null;
|
||||||
try {
|
try {
|
||||||
PreparedStatement poly2dRegionStatement = this.conn.prepareStatement(
|
poly2dRegionStatement = this.conn.prepareStatement(
|
||||||
"SELECT " +
|
"SELECT " +
|
||||||
"`region_poly2d`.`min_y`, " +
|
"`region_poly2d`.`min_y`, " +
|
||||||
"`region_poly2d`.`max_y`, " +
|
"`region_poly2d`.`max_y`, " +
|
||||||
@ -406,9 +449,9 @@ private void loadPoly2d() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
poly2dRegionStatement.setInt(1, this.worldDbId);
|
poly2dRegionStatement.setInt(1, this.worldDbId);
|
||||||
ResultSet poly2dResultSet = poly2dRegionStatement.executeQuery();
|
poly2dResultSet = poly2dRegionStatement.executeQuery();
|
||||||
|
|
||||||
PreparedStatement poly2dVectorStatement = this.conn.prepareStatement(
|
poly2dVectorStatement = this.conn.prepareStatement(
|
||||||
"SELECT " +
|
"SELECT " +
|
||||||
"`region_poly2d_point`.`x`, " +
|
"`region_poly2d_point`.`x`, " +
|
||||||
"`region_poly2d_point`.`z` " +
|
"`region_poly2d_point`.`z` " +
|
||||||
@ -433,6 +476,8 @@ private void loadPoly2d() {
|
|||||||
poly2dVectorResultSet.getInt("z")
|
poly2dVectorResultSet.getInt("z")
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
closeResource(poly2dVectorResultSet);
|
||||||
|
|
||||||
ProtectedRegion region = new ProtectedPolygonalRegion(id, points, minY, maxY);
|
ProtectedRegion region = new ProtectedPolygonalRegion(id, points, minY, maxY);
|
||||||
|
|
||||||
region.setPriority(poly2dResultSet.getInt("priority"));
|
region.setPriority(poly2dResultSet.getInt("priority"));
|
||||||
@ -456,6 +501,11 @@ private void loadPoly2d() {
|
|||||||
t = t.getCause();
|
t = t.getCause();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
finally {
|
||||||
|
closeResource(poly2dResultSet);
|
||||||
|
closeResource(poly2dRegionStatement);
|
||||||
|
closeResource(poly2dVectorStatement);
|
||||||
|
}
|
||||||
|
|
||||||
poly2dRegions = regions;
|
poly2dRegions = regions;
|
||||||
}
|
}
|
||||||
@ -512,8 +562,11 @@ private Map<String,Integer> getUserIds(String... usernames) {
|
|||||||
|
|
||||||
if (usernames.length < 1) return users;
|
if (usernames.length < 1) return users;
|
||||||
|
|
||||||
|
ResultSet findUsersResults = null;
|
||||||
|
PreparedStatement insertUserStatement = null;
|
||||||
|
PreparedStatement findUsersStatement = null;
|
||||||
try {
|
try {
|
||||||
PreparedStatement findUsersStatement = this.conn.prepareStatement(
|
findUsersStatement = this.conn.prepareStatement(
|
||||||
String.format(
|
String.format(
|
||||||
"SELECT " +
|
"SELECT " +
|
||||||
"`user`.`id`, " +
|
"`user`.`id`, " +
|
||||||
@ -526,13 +579,13 @@ private Map<String,Integer> getUserIds(String... usernames) {
|
|||||||
|
|
||||||
RegionDBUtil.setValues(findUsersStatement, usernames);
|
RegionDBUtil.setValues(findUsersStatement, usernames);
|
||||||
|
|
||||||
ResultSet findUsersResults = findUsersStatement.executeQuery();
|
findUsersResults = findUsersStatement.executeQuery();
|
||||||
|
|
||||||
while(findUsersResults.next()) {
|
while(findUsersResults.next()) {
|
||||||
users.put(findUsersResults.getString("name"), findUsersResults.getInt("id"));
|
users.put(findUsersResults.getString("name"), findUsersResults.getInt("id"));
|
||||||
}
|
}
|
||||||
|
|
||||||
PreparedStatement insertUserStatement = this.conn.prepareStatement(
|
insertUserStatement = this.conn.prepareStatement(
|
||||||
"INSERT INTO " +
|
"INSERT INTO " +
|
||||||
"`user` ( " +
|
"`user` ( " +
|
||||||
"`id`, " +
|
"`id`, " +
|
||||||
@ -562,6 +615,11 @@ private Map<String,Integer> getUserIds(String... usernames) {
|
|||||||
t = t.getCause();
|
t = t.getCause();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
finally {
|
||||||
|
closeResource(findUsersResults);
|
||||||
|
closeResource(findUsersStatement);
|
||||||
|
closeResource(insertUserStatement);
|
||||||
|
}
|
||||||
|
|
||||||
return users;
|
return users;
|
||||||
}
|
}
|
||||||
@ -576,8 +634,11 @@ private Map<String,Integer> getGroupIds(String... groupnames) {
|
|||||||
|
|
||||||
if (groupnames.length < 1) return groups;
|
if (groupnames.length < 1) return groups;
|
||||||
|
|
||||||
|
PreparedStatement findGroupsStatement = null;
|
||||||
|
ResultSet findGroupsResults = null;
|
||||||
|
PreparedStatement insertGroupStatement = null;
|
||||||
try {
|
try {
|
||||||
PreparedStatement findGroupsStatement = this.conn.prepareStatement(
|
findGroupsStatement = this.conn.prepareStatement(
|
||||||
String.format(
|
String.format(
|
||||||
"SELECT " +
|
"SELECT " +
|
||||||
"`group`.`id`, " +
|
"`group`.`id`, " +
|
||||||
@ -590,13 +651,13 @@ private Map<String,Integer> getGroupIds(String... groupnames) {
|
|||||||
|
|
||||||
RegionDBUtil.setValues(findGroupsStatement, groupnames);
|
RegionDBUtil.setValues(findGroupsStatement, groupnames);
|
||||||
|
|
||||||
ResultSet findGroupsResults = findGroupsStatement.executeQuery();
|
findGroupsResults = findGroupsStatement.executeQuery();
|
||||||
|
|
||||||
while(findGroupsResults.next()) {
|
while(findGroupsResults.next()) {
|
||||||
groups.put(findGroupsResults.getString("name"), findGroupsResults.getInt("id"));
|
groups.put(findGroupsResults.getString("name"), findGroupsResults.getInt("id"));
|
||||||
}
|
}
|
||||||
|
|
||||||
PreparedStatement insertGroupStatement = this.conn.prepareStatement(
|
insertGroupStatement = this.conn.prepareStatement(
|
||||||
"INSERT INTO " +
|
"INSERT INTO " +
|
||||||
"`group` ( " +
|
"`group` ( " +
|
||||||
"`id`, " +
|
"`id`, " +
|
||||||
@ -620,6 +681,11 @@ private Map<String,Integer> getGroupIds(String... groupnames) {
|
|||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
logger.warning("Could not get the database id for the groups " + groupnames.toString() + ex.getMessage());
|
logger.warning("Could not get the database id for the groups " + groupnames.toString() + ex.getMessage());
|
||||||
}
|
}
|
||||||
|
finally {
|
||||||
|
closeResource(findGroupsResults);
|
||||||
|
closeResource(findGroupsStatement);
|
||||||
|
closeResource(insertGroupStatement);
|
||||||
|
}
|
||||||
|
|
||||||
return groups;
|
return groups;
|
||||||
}
|
}
|
||||||
@ -649,15 +715,17 @@ public void save() throws ProtectionDatabaseException {
|
|||||||
|
|
||||||
List<String> regionsInDatabase = new ArrayList<String>();
|
List<String> regionsInDatabase = new ArrayList<String>();
|
||||||
|
|
||||||
|
PreparedStatement getAllRegionsStatement = null;
|
||||||
|
ResultSet getAllRegionsResult = null;
|
||||||
try {
|
try {
|
||||||
PreparedStatement getAllRegionsStatement = this.conn.prepareStatement(
|
getAllRegionsStatement = this.conn.prepareStatement(
|
||||||
"SELECT `region`.`id` FROM " +
|
"SELECT `region`.`id` FROM " +
|
||||||
"`region` " +
|
"`region` " +
|
||||||
"WHERE `world_id` = ? "
|
"WHERE `world_id` = ? "
|
||||||
);
|
);
|
||||||
|
|
||||||
getAllRegionsStatement.setInt(1, this.worldDbId);
|
getAllRegionsStatement.setInt(1, this.worldDbId);
|
||||||
ResultSet getAllRegionsResult = getAllRegionsStatement.executeQuery();
|
getAllRegionsResult = getAllRegionsStatement.executeQuery();
|
||||||
|
|
||||||
while(getAllRegionsResult.next()) {
|
while(getAllRegionsResult.next()) {
|
||||||
regionsInDatabase.add(getAllRegionsResult.getString("id"));
|
regionsInDatabase.add(getAllRegionsResult.getString("id"));
|
||||||
@ -665,6 +733,10 @@ public void save() throws ProtectionDatabaseException {
|
|||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
logger.warning("Could not get region list for save comparison: " + ex.getMessage());
|
logger.warning("Could not get region list for save comparison: " + ex.getMessage());
|
||||||
}
|
}
|
||||||
|
finally {
|
||||||
|
closeResource(getAllRegionsResult);
|
||||||
|
closeResource(getAllRegionsStatement);
|
||||||
|
}
|
||||||
|
|
||||||
for (Map.Entry<String, ProtectedRegion> entry : regions.entrySet()) {
|
for (Map.Entry<String, ProtectedRegion> entry : regions.entrySet()) {
|
||||||
String name = entry.getKey();
|
String name = entry.getKey();
|
||||||
@ -701,10 +773,11 @@ public void save() throws ProtectionDatabaseException {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (Map.Entry<String, ProtectedRegion> entry : regions.entrySet()) {
|
for (Map.Entry<String, ProtectedRegion> entry : regions.entrySet()) {
|
||||||
|
PreparedStatement setParentStatement = null;
|
||||||
try {
|
try {
|
||||||
if (entry.getValue().getParent() == null) continue;
|
if (entry.getValue().getParent() == null) continue;
|
||||||
|
|
||||||
PreparedStatement setParentStatement = this.conn.prepareStatement(
|
setParentStatement = this.conn.prepareStatement(
|
||||||
"UPDATE `region` SET " +
|
"UPDATE `region` SET " +
|
||||||
"`parent` = ? " +
|
"`parent` = ? " +
|
||||||
"WHERE `id` = ? AND `world_id` = " + this.worldDbId
|
"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());
|
logger.warning("Could not save region parents " + entry.getValue().getId().toLowerCase() + ": " + ex.getMessage());
|
||||||
throw new ProtectionDatabaseException(ex);
|
throw new ProtectionDatabaseException(ex);
|
||||||
}
|
}
|
||||||
|
finally {
|
||||||
|
closeResource(setParentStatement);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (String name : regionsInDatabase) {
|
for (String name : regionsInDatabase) {
|
||||||
|
PreparedStatement removeRegion = null;
|
||||||
try {
|
try {
|
||||||
PreparedStatement removeRegion = this.conn.prepareStatement(
|
removeRegion = this.conn.prepareStatement(
|
||||||
"DELETE FROM `region` WHERE `id` = ? "
|
"DELETE FROM `region` WHERE `id` = ? "
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -731,12 +808,17 @@ public void save() throws ProtectionDatabaseException {
|
|||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
logger.warning("Could not remove region from database " + name + ": " + ex.getMessage());
|
logger.warning("Could not remove region from database " + name + ": " + ex.getMessage());
|
||||||
}
|
}
|
||||||
|
finally {
|
||||||
|
closeResource(removeRegion);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateFlags(ProtectedRegion region) throws SQLException {
|
private void updateFlags(ProtectedRegion region) throws SQLException {
|
||||||
PreparedStatement clearCurrentFlagStatement = this.conn.prepareStatement(
|
PreparedStatement clearCurrentFlagStatement = null;
|
||||||
|
try {
|
||||||
|
clearCurrentFlagStatement = this.conn.prepareStatement(
|
||||||
"DELETE FROM `region_flag` " +
|
"DELETE FROM `region_flag` " +
|
||||||
"WHERE `region_id` = ? " +
|
"WHERE `region_id` = ? " +
|
||||||
"AND `world_id` = " + this.worldDbId
|
"AND `world_id` = " + this.worldDbId
|
||||||
@ -750,7 +832,9 @@ private void updateFlags(ProtectedRegion region) throws SQLException {
|
|||||||
|
|
||||||
Object flag = sqlMarshal(marshalFlag(entry.getKey(), entry.getValue()));
|
Object flag = sqlMarshal(marshalFlag(entry.getKey(), entry.getValue()));
|
||||||
|
|
||||||
PreparedStatement insertFlagStatement = this.conn.prepareStatement(
|
PreparedStatement insertFlagStatement = null;
|
||||||
|
try {
|
||||||
|
insertFlagStatement = this.conn.prepareStatement(
|
||||||
"INSERT INTO `region_flag` ( " +
|
"INSERT INTO `region_flag` ( " +
|
||||||
"`id`, " +
|
"`id`, " +
|
||||||
"`region_id`, " +
|
"`region_id`, " +
|
||||||
@ -765,6 +849,12 @@ private void updateFlags(ProtectedRegion region) throws SQLException {
|
|||||||
insertFlagStatement.setObject(3, flag);
|
insertFlagStatement.setObject(3, flag);
|
||||||
|
|
||||||
insertFlagStatement.execute();
|
insertFlagStatement.execute();
|
||||||
|
} finally {
|
||||||
|
closeResource(insertFlagStatement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
closeResource(clearCurrentFlagStatement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -777,7 +867,13 @@ private void updatePlayerAndGroups(ProtectedRegion region, Boolean owners) throw
|
|||||||
domain = region.getMembers();
|
domain = region.getMembers();
|
||||||
}
|
}
|
||||||
|
|
||||||
PreparedStatement deleteUsersForRegion = this.conn.prepareStatement(
|
PreparedStatement deleteUsersForRegion = null;
|
||||||
|
PreparedStatement insertUsersForRegion = null;
|
||||||
|
PreparedStatement deleteGroupsForRegion = null;
|
||||||
|
PreparedStatement insertGroupsForRegion = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
deleteUsersForRegion = this.conn.prepareStatement(
|
||||||
"DELETE FROM `region_players` " +
|
"DELETE FROM `region_players` " +
|
||||||
"WHERE `region_id` = ? " +
|
"WHERE `region_id` = ? " +
|
||||||
"AND `world_id` = " + this.worldDbId + " " +
|
"AND `world_id` = " + this.worldDbId + " " +
|
||||||
@ -788,7 +884,7 @@ private void updatePlayerAndGroups(ProtectedRegion region, Boolean owners) throw
|
|||||||
deleteUsersForRegion.setBoolean(2, owners);
|
deleteUsersForRegion.setBoolean(2, owners);
|
||||||
deleteUsersForRegion.execute();
|
deleteUsersForRegion.execute();
|
||||||
|
|
||||||
PreparedStatement insertUsersForRegion = this.conn.prepareStatement(
|
insertUsersForRegion = this.conn.prepareStatement(
|
||||||
"INSERT INTO `region_players` " +
|
"INSERT INTO `region_players` " +
|
||||||
"(`region_id`, `world_id`, `user_id`, `owner`) " +
|
"(`region_id`, `world_id`, `user_id`, `owner`) " +
|
||||||
"VALUES (?, " + this.worldDbId + ", ?, ?)"
|
"VALUES (?, " + this.worldDbId + ", ?, ?)"
|
||||||
@ -804,7 +900,7 @@ private void updatePlayerAndGroups(ProtectedRegion region, Boolean owners) throw
|
|||||||
insertUsersForRegion.execute();
|
insertUsersForRegion.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
PreparedStatement deleteGroupsForRegion = this.conn.prepareStatement(
|
deleteGroupsForRegion = this.conn.prepareStatement(
|
||||||
"DELETE FROM `region_groups` " +
|
"DELETE FROM `region_groups` " +
|
||||||
"WHERE `region_id` = ? " +
|
"WHERE `region_id` = ? " +
|
||||||
"AND `world_id` = " + this.worldDbId + " " +
|
"AND `world_id` = " + this.worldDbId + " " +
|
||||||
@ -815,7 +911,7 @@ private void updatePlayerAndGroups(ProtectedRegion region, Boolean owners) throw
|
|||||||
deleteGroupsForRegion.setBoolean(2, owners);
|
deleteGroupsForRegion.setBoolean(2, owners);
|
||||||
deleteGroupsForRegion.execute();
|
deleteGroupsForRegion.execute();
|
||||||
|
|
||||||
PreparedStatement insertGroupsForRegion = this.conn.prepareStatement(
|
insertGroupsForRegion = this.conn.prepareStatement(
|
||||||
"INSERT INTO `region_groups` " +
|
"INSERT INTO `region_groups` " +
|
||||||
"(`region_id`, `world_id`, `group_id`, `owner`) " +
|
"(`region_id`, `world_id`, `group_id`, `owner`) " +
|
||||||
"VALUES (?, " + this.worldDbId + ", ?, ?)"
|
"VALUES (?, " + this.worldDbId + ", ?, ?)"
|
||||||
@ -829,6 +925,12 @@ private void updatePlayerAndGroups(ProtectedRegion region, Boolean owners) throw
|
|||||||
|
|
||||||
insertGroupsForRegion.execute();
|
insertGroupsForRegion.execute();
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
closeResource(deleteGroupsForRegion);
|
||||||
|
closeResource(deleteUsersForRegion);
|
||||||
|
closeResource(insertGroupsForRegion);
|
||||||
|
closeResource(insertUsersForRegion);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@ -837,7 +939,9 @@ private <V> Object marshalFlag(Flag<V> flag, Object val) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void insertRegion(ProtectedRegion region, String type) throws SQLException {
|
private void insertRegion(ProtectedRegion region, String type) throws SQLException {
|
||||||
PreparedStatement insertRegionStatement = this.conn.prepareStatement(
|
PreparedStatement insertRegionStatement = null;
|
||||||
|
try {
|
||||||
|
insertRegionStatement = this.conn.prepareStatement(
|
||||||
"INSERT INTO `region` (" +
|
"INSERT INTO `region` (" +
|
||||||
"`id`, " +
|
"`id`, " +
|
||||||
"`world_id`, " +
|
"`world_id`, " +
|
||||||
@ -853,6 +957,9 @@ private void insertRegion(ProtectedRegion region, String type) throws SQLExcepti
|
|||||||
insertRegionStatement.setInt(4, region.getPriority());
|
insertRegionStatement.setInt(4, region.getPriority());
|
||||||
|
|
||||||
insertRegionStatement.execute();
|
insertRegionStatement.execute();
|
||||||
|
} finally {
|
||||||
|
closeResource(insertRegionStatement);
|
||||||
|
}
|
||||||
|
|
||||||
updateFlags(region);
|
updateFlags(region);
|
||||||
|
|
||||||
@ -863,7 +970,9 @@ private void insertRegion(ProtectedRegion region, String type) throws SQLExcepti
|
|||||||
private void insertRegionCuboid(ProtectedCuboidRegion region) throws SQLException {
|
private void insertRegionCuboid(ProtectedCuboidRegion region) throws SQLException {
|
||||||
insertRegion(region, "cuboid");
|
insertRegion(region, "cuboid");
|
||||||
|
|
||||||
PreparedStatement insertCuboidRegionStatement = this.conn.prepareStatement(
|
PreparedStatement insertCuboidRegionStatement = null;
|
||||||
|
try {
|
||||||
|
insertCuboidRegionStatement = this.conn.prepareStatement(
|
||||||
"INSERT INTO `region_cuboid` (" +
|
"INSERT INTO `region_cuboid` (" +
|
||||||
"`region_id`, " +
|
"`region_id`, " +
|
||||||
"`world_id`, " +
|
"`world_id`, " +
|
||||||
@ -888,12 +997,17 @@ private void insertRegionCuboid(ProtectedCuboidRegion region) throws SQLExceptio
|
|||||||
insertCuboidRegionStatement.setInt(7, max.getBlockX());
|
insertCuboidRegionStatement.setInt(7, max.getBlockX());
|
||||||
|
|
||||||
insertCuboidRegionStatement.execute();
|
insertCuboidRegionStatement.execute();
|
||||||
|
} finally {
|
||||||
|
closeResource(insertCuboidRegionStatement);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void insertRegionPoly2D(ProtectedPolygonalRegion region) throws SQLException {
|
private void insertRegionPoly2D(ProtectedPolygonalRegion region) throws SQLException {
|
||||||
insertRegion(region, "poly2d");
|
insertRegion(region, "poly2d");
|
||||||
|
|
||||||
PreparedStatement insertPoly2dRegionStatement = this.conn.prepareStatement(
|
PreparedStatement insertPoly2dRegionStatement = null;
|
||||||
|
try {
|
||||||
|
insertPoly2dRegionStatement = this.conn.prepareStatement(
|
||||||
"INSERT INTO `region_poly2d` (" +
|
"INSERT INTO `region_poly2d` (" +
|
||||||
"`region_id`, " +
|
"`region_id`, " +
|
||||||
"`world_id`, " +
|
"`world_id`, " +
|
||||||
@ -907,12 +1021,19 @@ private void insertRegionPoly2D(ProtectedPolygonalRegion region) throws SQLExcep
|
|||||||
insertPoly2dRegionStatement.setInt(3, region.getMinimumPoint().getBlockY());
|
insertPoly2dRegionStatement.setInt(3, region.getMinimumPoint().getBlockY());
|
||||||
|
|
||||||
insertPoly2dRegionStatement.execute();
|
insertPoly2dRegionStatement.execute();
|
||||||
|
} finally {
|
||||||
|
closeResource(insertPoly2dRegionStatement);
|
||||||
|
}
|
||||||
|
|
||||||
updatePoly2dPoints(region);
|
updatePoly2dPoints(region);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updatePoly2dPoints(ProtectedPolygonalRegion region) throws SQLException {
|
private void updatePoly2dPoints(ProtectedPolygonalRegion region) throws SQLException {
|
||||||
PreparedStatement clearPoly2dPointsForRegionStatement = this.conn.prepareStatement(
|
PreparedStatement clearPoly2dPointsForRegionStatement = null;
|
||||||
|
PreparedStatement insertPoly2dPointStatement = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
clearPoly2dPointsForRegionStatement = this.conn.prepareStatement(
|
||||||
"DELETE FROM `region_poly2d_point` " +
|
"DELETE FROM `region_poly2d_point` " +
|
||||||
"WHERE `region_id` = ? " +
|
"WHERE `region_id` = ? " +
|
||||||
"AND `world_id` = " + this.worldDbId
|
"AND `world_id` = " + this.worldDbId
|
||||||
@ -922,7 +1043,7 @@ private void updatePoly2dPoints(ProtectedPolygonalRegion region) throws SQLExcep
|
|||||||
|
|
||||||
clearPoly2dPointsForRegionStatement.execute();
|
clearPoly2dPointsForRegionStatement.execute();
|
||||||
|
|
||||||
PreparedStatement insertPoly2dPointStatement = this.conn.prepareStatement(
|
insertPoly2dPointStatement = this.conn.prepareStatement(
|
||||||
"INSERT INTO `region_poly2d_point` (" +
|
"INSERT INTO `region_poly2d_point` (" +
|
||||||
"`id`, " +
|
"`id`, " +
|
||||||
"`region_id`, " +
|
"`region_id`, " +
|
||||||
@ -940,6 +1061,10 @@ private void updatePoly2dPoints(ProtectedPolygonalRegion region) throws SQLExcep
|
|||||||
|
|
||||||
insertPoly2dPointStatement.execute();
|
insertPoly2dPointStatement.execute();
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
closeResource(clearPoly2dPointsForRegionStatement);
|
||||||
|
closeResource(insertPoly2dPointStatement);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void insertRegionGlobal(GlobalProtectedRegion region) throws SQLException {
|
private void insertRegionGlobal(GlobalProtectedRegion region) throws SQLException {
|
||||||
@ -947,7 +1072,9 @@ private void insertRegionGlobal(GlobalProtectedRegion region) throws SQLExceptio
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void updateRegion(ProtectedRegion region, String type) throws SQLException {
|
private void updateRegion(ProtectedRegion region, String type) throws SQLException {
|
||||||
PreparedStatement updateRegionStatement = this.conn.prepareStatement(
|
PreparedStatement updateRegionStatement = null;
|
||||||
|
try {
|
||||||
|
updateRegionStatement = this.conn.prepareStatement(
|
||||||
"UPDATE `region` SET " +
|
"UPDATE `region` SET " +
|
||||||
"`priority` = ? WHERE `id` = ? AND `world_id` = " + this.worldDbId
|
"`priority` = ? WHERE `id` = ? AND `world_id` = " + this.worldDbId
|
||||||
);
|
);
|
||||||
@ -956,6 +1083,9 @@ private void updateRegion(ProtectedRegion region, String type) throws SQLExcepti
|
|||||||
updateRegionStatement.setString(2, region.getId().toLowerCase());
|
updateRegionStatement.setString(2, region.getId().toLowerCase());
|
||||||
|
|
||||||
updateRegionStatement.execute();
|
updateRegionStatement.execute();
|
||||||
|
} finally {
|
||||||
|
closeResource(updateRegionStatement);
|
||||||
|
}
|
||||||
|
|
||||||
updateFlags(region);
|
updateFlags(region);
|
||||||
|
|
||||||
@ -966,7 +1096,9 @@ private void updateRegion(ProtectedRegion region, String type) throws SQLExcepti
|
|||||||
private void updateRegionCuboid(ProtectedCuboidRegion region) throws SQLException {
|
private void updateRegionCuboid(ProtectedCuboidRegion region) throws SQLException {
|
||||||
updateRegion(region, "cuboid");
|
updateRegion(region, "cuboid");
|
||||||
|
|
||||||
PreparedStatement updateCuboidRegionStatement = this.conn.prepareStatement(
|
PreparedStatement updateCuboidRegionStatement = null;
|
||||||
|
try {
|
||||||
|
updateCuboidRegionStatement = this.conn.prepareStatement(
|
||||||
"UPDATE `region_cuboid` SET " +
|
"UPDATE `region_cuboid` SET " +
|
||||||
"`min_z` = ?, " +
|
"`min_z` = ?, " +
|
||||||
"`min_y` = ?, " +
|
"`min_y` = ?, " +
|
||||||
@ -990,12 +1122,17 @@ private void updateRegionCuboid(ProtectedCuboidRegion region) throws SQLExceptio
|
|||||||
updateCuboidRegionStatement.setString(7, region.getId().toLowerCase());
|
updateCuboidRegionStatement.setString(7, region.getId().toLowerCase());
|
||||||
|
|
||||||
updateCuboidRegionStatement.execute();
|
updateCuboidRegionStatement.execute();
|
||||||
|
} finally {
|
||||||
|
closeResource(updateCuboidRegionStatement);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateRegionPoly2D(ProtectedPolygonalRegion region) throws SQLException {
|
private void updateRegionPoly2D(ProtectedPolygonalRegion region) throws SQLException {
|
||||||
updateRegion(region, "poly2d");
|
updateRegion(region, "poly2d");
|
||||||
|
|
||||||
PreparedStatement updatePoly2dRegionStatement = this.conn.prepareStatement(
|
PreparedStatement updatePoly2dRegionStatement = null;
|
||||||
|
try {
|
||||||
|
updatePoly2dRegionStatement = this.conn.prepareStatement(
|
||||||
"UPDATE `region_poly2d` SET " +
|
"UPDATE `region_poly2d` SET " +
|
||||||
"`max_y` = ?, " +
|
"`max_y` = ?, " +
|
||||||
"`min_y` = ? " +
|
"`min_y` = ? " +
|
||||||
@ -1008,7 +1145,9 @@ private void updateRegionPoly2D(ProtectedPolygonalRegion region) throws SQLExcep
|
|||||||
updatePoly2dRegionStatement.setString(3, region.getId().toLowerCase());
|
updatePoly2dRegionStatement.setString(3, region.getId().toLowerCase());
|
||||||
|
|
||||||
updatePoly2dRegionStatement.execute();
|
updatePoly2dRegionStatement.execute();
|
||||||
|
} finally {
|
||||||
|
closeResource(updatePoly2dRegionStatement);
|
||||||
|
}
|
||||||
updatePoly2dPoints(region);
|
updatePoly2dPoints(region);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1016,6 +1155,22 @@ private void updateRegionGlobal(GlobalProtectedRegion region) throws SQLExceptio
|
|||||||
updateRegion(region, "global");
|
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
|
@Override
|
||||||
public Map<String, ProtectedRegion> getRegions() {
|
public Map<String, ProtectedRegion> getRegions() {
|
||||||
return regions;
|
return regions;
|
||||||
|
Loading…
Reference in New Issue
Block a user