From ed82a838c82efb07e022411b657b8305dd6bbded Mon Sep 17 00:00:00 2001 From: t00thpick1 Date: Sun, 27 Jul 2014 14:56:34 -0400 Subject: [PATCH] I like these to be explictly closed rather than in a new method. --- .../database/FlatfileDatabaseManager.java | 176 +++++++++++++++--- .../nossr50/database/SQLDatabaseManager.java | 33 ++-- 2 files changed, 161 insertions(+), 48 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java b/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java index 67e410f51..75e24ce0f 100644 --- a/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java +++ b/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java @@ -98,8 +98,22 @@ public final class FlatfileDatabaseManager implements DatabaseManager { mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString()); } finally { - tryClose(in); - tryClose(out); + if (in != null) { + try { + in.close(); + } + catch (IOException e) { + // Ignore + } + } + if (out != null) { + try { + out.close(); + } + catch (IOException e) { + // Ignore + } + } } } @@ -164,8 +178,22 @@ public final class FlatfileDatabaseManager implements DatabaseManager { mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString()); } finally { - tryClose(in); - tryClose(out); + if (in != null) { + try { + in.close(); + } + catch (IOException e) { + // Ignore + } + } + if (out != null) { + try { + out.close(); + } + catch (IOException e) { + // Ignore + } + } } } @@ -203,8 +231,22 @@ public final class FlatfileDatabaseManager implements DatabaseManager { mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString()); } finally { - tryClose(in); - tryClose(out); + if (in != null) { + try { + in.close(); + } + catch (IOException e) { + // Ignore + } + } + if (out != null) { + try { + out.close(); + } + catch (IOException e) { + // Ignore + } + } } } @@ -294,8 +336,22 @@ public final class FlatfileDatabaseManager implements DatabaseManager { return false; } finally { - tryClose(in); - tryClose(out); + if (in != null) { + try { + in.close(); + } + catch (IOException e) { + // Ignore + } + } + if (out != null) { + try { + out.close(); + } + catch (IOException e) { + // Ignore + } + } } } } @@ -381,7 +437,14 @@ public final class FlatfileDatabaseManager implements DatabaseManager { e.printStackTrace(); } finally { - tryClose(out); + if (out != null) { + try { + out.close(); + } + catch (IOException e) { + // Ignore + } + } } } } @@ -448,7 +511,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager { in.close(); } catch (IOException e) { - e.printStackTrace(); + // Ignore } } } @@ -491,7 +554,14 @@ public final class FlatfileDatabaseManager implements DatabaseManager { e.printStackTrace(); } finally { - tryClose(in); + if (in != null) { + try { + in.close(); + } + catch (IOException e) { + // Ignore + } + } } } } @@ -532,8 +602,22 @@ public final class FlatfileDatabaseManager implements DatabaseManager { mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString()); } finally { - tryClose(in); - tryClose(out); + if (in != null) { + try { + in.close(); + } + catch (IOException e) { + // Ignore + } + } + if (out != null) { + try { + out.close(); + } + catch (IOException e) { + // Ignore + } + } } } @@ -573,8 +657,22 @@ public final class FlatfileDatabaseManager implements DatabaseManager { mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString()); } finally { - tryClose(in); - tryClose(out); + if (in != null) { + try { + in.close(); + } + catch (IOException e) { + // Ignore + } + } + if (out != null) { + try { + out.close(); + } + catch (IOException e) { + // Ignore + } + } } } @@ -601,7 +699,14 @@ public final class FlatfileDatabaseManager implements DatabaseManager { e.printStackTrace(); } finally { - tryClose(in); + if (in != null) { + try { + in.close(); + } + catch (IOException e) { + // Ignore + } + } } } return users; @@ -671,7 +776,14 @@ public final class FlatfileDatabaseManager implements DatabaseManager { mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " during user " + playerName + " (Are you sure you formatted it correctly?) " + e.toString()); } finally { - tryClose(in); + if (in != null) { + try { + in.close(); + } + catch (IOException e) { + // Ignore + } + } } } @@ -897,8 +1009,22 @@ public final class FlatfileDatabaseManager implements DatabaseManager { mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString()); } finally { - tryClose(in); - tryClose(out); + if (in != null) { + try { + in.close(); + } + catch (IOException e) { + // Ignore + } + } + if (out != null) { + try { + out.close(); + } + catch (IOException e) { + // Ignore + } + } } } @@ -923,18 +1049,6 @@ public final class FlatfileDatabaseManager implements DatabaseManager { } } - private void tryClose(Closeable c) { - if (c == null) { - return; - } - try { - c.close(); - } - catch (IOException e) { - e.printStackTrace(); - } - } - private Integer getPlayerRank(String playerName, List statsList) { if (statsList == null) { return null; diff --git a/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java b/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java index 30c8a24a8..ae90578ee 100644 --- a/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java +++ b/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java @@ -33,8 +33,7 @@ public final class SQLDatabaseManager implements DatabaseManager { private String connectionString; private String tablePrefix = Config.getInstance().getMySQLTablePrefix(); - // How long to wait when checking if connection is valid (default 3 seconds) - private final int VALID_TIMEOUT = 3; + private final int POOL_FETCH_TIMEOUT = 0; // How long a method will wait for a connection. Since none are on main thread, we can safely say wait for as long as you like. private final Map cachedUserIDs = new HashMap(); private final Map cachedUserIDsByName = new HashMap(); @@ -62,7 +61,7 @@ public final class SQLDatabaseManager implements DatabaseManager { connectionPool.registerShutdownHook(); // Auto release on jvm exit just in case } catch (ClassNotFoundException e) { - // TODO tft do something here + // TODO tft do something here everything will blow up e.printStackTrace(); } @@ -79,7 +78,7 @@ public final class SQLDatabaseManager implements DatabaseManager { List usernames = new ArrayList(); try { - connection = connectionPool.getConnection(VALID_TIMEOUT); + connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); statement = connection.createStatement(); resultSet = statement.executeQuery("SELECT u.user FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE s.user_id = u.id AND (s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing) = 0"); @@ -142,7 +141,7 @@ public final class SQLDatabaseManager implements DatabaseManager { List usernames = new ArrayList(); try { - connection = connectionPool.getConnection(VALID_TIMEOUT); + connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); statement = connection.createStatement(); resultSet = statement.executeQuery("SELECT user FROM " + tablePrefix + "users WHERE ((NOW() - lastlogin * " + Misc.TIME_CONVERSION_FACTOR + ") > " + PURGE_TIME + ")"); @@ -202,7 +201,7 @@ public final class SQLDatabaseManager implements DatabaseManager { PreparedStatement statement = null; try { - connection = connectionPool.getConnection(VALID_TIMEOUT); + connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); statement = connection.prepareStatement("DELETE FROM u, e, h, s, c " + "USING " + tablePrefix + "users u " + "JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) " + @@ -250,7 +249,7 @@ public final class SQLDatabaseManager implements DatabaseManager { Connection connection = null; try { - connection = connectionPool.getConnection(VALID_TIMEOUT); + connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); int id = getUserID(connection, profile.getUniqueId()); @@ -366,7 +365,7 @@ public final class SQLDatabaseManager implements DatabaseManager { Connection connection = null; try { - connection = connectionPool.getConnection(VALID_TIMEOUT); + connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); statement = connection.prepareStatement("SELECT " + query + ", user, NOW() FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON (user_id = id) WHERE " + query + " > 0 ORDER BY " + query + " DESC, user LIMIT ?, ?"); statement.setInt(1, (pageNumber * statsPerPage) - statsPerPage); statement.setInt(2, statsPerPage); @@ -423,7 +422,7 @@ public final class SQLDatabaseManager implements DatabaseManager { Connection connection = null; try { - connection = connectionPool.getConnection(VALID_TIMEOUT); + connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); for (SkillType skillType : SkillType.NON_CHILD_SKILLS) { String skillName = skillType.name().toLowerCase(); String sql = "SELECT COUNT(*) AS rank FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE " + skillName + " > 0 " + @@ -534,7 +533,7 @@ public final class SQLDatabaseManager implements DatabaseManager { Connection connection = null; try { - connection = connectionPool.getConnection(VALID_TIMEOUT); + connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); newUser(connection, playerName, uuid); } catch (SQLException ex) { @@ -604,7 +603,7 @@ public final class SQLDatabaseManager implements DatabaseManager { ResultSet resultSet = null; try { - connection = connectionPool.getConnection(VALID_TIMEOUT); + connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); int id = getUserID(connection, playerName); if (id == -1) { @@ -705,7 +704,7 @@ public final class SQLDatabaseManager implements DatabaseManager { ResultSet resultSet = null; try { - connection = connectionPool.getConnection(VALID_TIMEOUT); + connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); int id = getUserID(connection, playerName); if (id == -1) { @@ -808,7 +807,7 @@ public final class SQLDatabaseManager implements DatabaseManager { ResultSet resultSet = null; try { - connection = connectionPool.getConnection(VALID_TIMEOUT); + connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); statement = connection.prepareStatement( "SELECT " + "s.taming, s.mining, s.repair, s.woodcutting, s.unarmed, s.herbalism, s.excavation, s.archery, s.swords, s.axes, s.acrobatics, s.fishing, s.alchemy, " @@ -876,7 +875,7 @@ public final class SQLDatabaseManager implements DatabaseManager { Connection connection = null; try { - connection = connectionPool.getConnection(VALID_TIMEOUT); + connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); statement = connection.prepareStatement( "UPDATE `" + tablePrefix + "users` SET " + " uuid = ? WHERE user = ?"); @@ -916,7 +915,7 @@ public final class SQLDatabaseManager implements DatabaseManager { Connection connection = null; try { - connection = connectionPool.getConnection(VALID_TIMEOUT); + connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); statement = connection.prepareStatement("UPDATE " + tablePrefix + "users SET uuid = ? WHERE user = ?"); for (Map.Entry entry : fetchedUUIDs.entrySet()) { @@ -971,7 +970,7 @@ public final class SQLDatabaseManager implements DatabaseManager { ResultSet resultSet = null; try { - connection = connectionPool.getConnection(VALID_TIMEOUT); + connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); statement = connection.createStatement(); resultSet = statement.executeQuery("SELECT user FROM " + tablePrefix + "users"); while (resultSet.next()) { @@ -1020,7 +1019,7 @@ public final class SQLDatabaseManager implements DatabaseManager { Connection connection = null; try { - connection = connectionPool.getConnection(VALID_TIMEOUT); + connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); statement = connection.createStatement(); statement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "users` ("