Explicitly close every resource

This commit is contained in:
t00thpick1 2014-07-26 00:00:40 -04:00
parent b2015d68d1
commit 1396d6d9ad
4 changed files with 113 additions and 42 deletions

View File

@ -132,4 +132,9 @@ public interface DatabaseManager {
* @return The type of database * @return The type of database
*/ */
public DatabaseType getDatabaseType(); public DatabaseType getDatabaseType();
/**
* Called when the plugin disables
*/
public void onDisable();
} }

View File

@ -1041,4 +1041,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
public DatabaseType getDatabaseType() { public DatabaseType getDatabaseType() {
return DatabaseType.FLATFILE; return DatabaseType.FLATFILE;
} }
@Override
public void onDisable() { }
} }

View File

@ -29,6 +29,7 @@ import com.gmail.nossr50.runnables.database.UUIDUpdateAsyncTask;
import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Misc;
public final class SQLDatabaseManager implements DatabaseManager { public final class SQLDatabaseManager implements DatabaseManager {
private static final String ALL_QUERY_VERSION = "taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing+alchemy";
private String connectionString; private String connectionString;
private String tablePrefix = Config.getInstance().getMySQLTablePrefix(); private String tablePrefix = Config.getInstance().getMySQLTablePrefix();
@ -50,9 +51,15 @@ public final class SQLDatabaseManager implements DatabaseManager {
connectionProperties.put("user", Config.getInstance().getMySQLUserName()); connectionProperties.put("user", Config.getInstance().getMySQLUserName());
connectionProperties.put("password", Config.getInstance().getMySQLUserPassword()); connectionProperties.put("password", Config.getInstance().getMySQLUserPassword());
connectionProperties.put("autoReconnect", "false"); connectionProperties.put("autoReconnect", "false");
connectionPool = new ConnectionPool("mcMMO-Pool", 1 /*Minimum of one*/, 10 /*max pool size Configurable?*/, 10/*max num connections Configurable?*/, 0 /* idle timeout of connections */, connectionString, connectionProperties); connectionPool = new ConnectionPool("mcMMO-Pool",
1 /*Minimum of one*/,
10 /*max pool size */, // TODO Configurable?
10 /*max num connections*/, // TODO Configurable?
0 /* idle timeout of connections */,
connectionString,
connectionProperties);
connectionPool.init(); // Init first connection connectionPool.init(); // Init first connection
connectionPool.registerShutdownHook(); // Auto release when done connectionPool.registerShutdownHook(); // Auto release on jvm exit just in case
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
// TODO tft do something here // TODO tft do something here
e.printStackTrace(); e.printStackTrace();
@ -81,13 +88,12 @@ public final class SQLDatabaseManager implements DatabaseManager {
resultSet.close(); resultSet.close();
statement.executeQuery("DELETE FROM u, e, h, s, c USING " + tablePrefix + "users u " + statement.executeUpdate("DELETE FROM u, e, h, s, c USING " + tablePrefix + "users u " +
"JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) " + "JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) " +
"JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) " + "JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) " +
"JOIN " + tablePrefix + "skills s ON (u.id = s.user_id) " + "JOIN " + tablePrefix + "skills s ON (u.id = s.user_id) " +
"JOIN " + tablePrefix + "cooldowns c ON (u.id = c.user_id) " + "JOIN " + tablePrefix + "cooldowns c ON (u.id = c.user_id) " +
"WHERE (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"); "WHERE (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");
} catch (SQLException ex) { } catch (SQLException ex) {
printErrors(ex); printErrors(ex);
} finally { } finally {
@ -140,13 +146,12 @@ public final class SQLDatabaseManager implements DatabaseManager {
resultSet.close(); resultSet.close();
statement.executeQuery("DELETE FROM u, e, h, s, c USING " + tablePrefix + "users u " + statement.executeUpdate("DELETE FROM u, e, h, s, c USING " + tablePrefix + "users u " +
"JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) " + "JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) " +
"JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) " + "JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) " +
"JOIN " + tablePrefix + "skills s ON (u.id = s.user_id) " + "JOIN " + tablePrefix + "skills s ON (u.id = s.user_id) " +
"JOIN " + tablePrefix + "cooldowns c ON (u.id = c.user_id) " + "JOIN " + tablePrefix + "cooldowns c ON (u.id = c.user_id) " +
"WHERE ((NOW() - lastlogin * " + Misc.TIME_CONVERSION_FACTOR + ") > " + PURGE_TIME + ")"); "WHERE ((NOW() - lastlogin * " + Misc.TIME_CONVERSION_FACTOR + ") > " + PURGE_TIME + ")");
} catch (SQLException ex) { } catch (SQLException ex) {
printErrors(ex); printErrors(ex);
} finally { } finally {
@ -198,7 +203,6 @@ public final class SQLDatabaseManager implements DatabaseManager {
statement.setString(1, playerName); statement.setString(1, playerName);
success = statement.executeUpdate() != 0; success = statement.executeUpdate() != 0;
} catch (SQLException ex) { } catch (SQLException ex) {
printErrors(ex); printErrors(ex);
} finally { } finally {
@ -306,6 +310,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
statement.setLong(8, profile.getAbilityDATS(AbilityType.BLAST_MINING)); statement.setLong(8, profile.getAbilityDATS(AbilityType.BLAST_MINING));
statement.setInt(9, id); statement.setInt(9, id);
success = (statement.executeUpdate() != 0); success = (statement.executeUpdate() != 0);
statement.close();
statement = connection.prepareStatement("UPDATE " + tablePrefix + "huds SET mobhealthbar = ? WHERE user_id = ?"); statement = connection.prepareStatement("UPDATE " + tablePrefix + "huds SET mobhealthbar = ? WHERE user_id = ?");
statement.setString(1, profile.getMobHealthbarType() == null ? Config.getInstance().getMobHealthbarDefault().name() : profile.getMobHealthbarType().name()); statement.setString(1, profile.getMobHealthbarType() == null ? Config.getInstance().getMobHealthbarDefault().name() : profile.getMobHealthbarType().name());
@ -336,8 +341,8 @@ public final class SQLDatabaseManager implements DatabaseManager {
public List<PlayerStat> readLeaderboard(SkillType skill, int pageNumber, int statsPerPage) { public List<PlayerStat> readLeaderboard(SkillType skill, int pageNumber, int statsPerPage) {
List<PlayerStat> stats = new ArrayList<PlayerStat>(); List<PlayerStat> stats = new ArrayList<PlayerStat>();
String query = skill == null ? "taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing+alchemy" : skill.name().toLowerCase(); String query = skill == null ? ALL_QUERY_VERSION : skill.name().toLowerCase();
ResultSet resultSet; ResultSet resultSet = null;
PreparedStatement statement = null; PreparedStatement statement = null;
Connection connection = null; Connection connection = null;
@ -360,6 +365,13 @@ public final class SQLDatabaseManager implements DatabaseManager {
} catch (SQLException ex) { } catch (SQLException ex) {
printErrors(ex); printErrors(ex);
} finally { } finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
// Ignore
}
}
if (statement != null) { if (statement != null) {
try { try {
statement.close(); statement.close();
@ -382,7 +394,8 @@ public final class SQLDatabaseManager implements DatabaseManager {
public Map<SkillType, Integer> readRank(String playerName) { public Map<SkillType, Integer> readRank(String playerName) {
Map<SkillType, Integer> skills = new HashMap<SkillType, Integer>(); Map<SkillType, Integer> skills = new HashMap<SkillType, Integer>();
ResultSet resultSet; ResultSet resultSet = null;
PreparedStatement statement = null;
Connection connection = null; Connection connection = null;
try { try {
@ -393,7 +406,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
"AND " + skillName + " > (SELECT " + skillName + " FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " + "AND " + skillName + " > (SELECT " + skillName + " FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " +
"WHERE user = ?)"; "WHERE user = ?)";
PreparedStatement statement = connection.prepareStatement(sql); statement = connection.prepareStatement(sql);
statement.setString(1, playerName); statement.setString(1, playerName);
resultSet = statement.executeQuery(); resultSet = statement.executeQuery();
@ -405,6 +418,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
"AND " + skillName + " = (SELECT " + skillName + " FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " + "AND " + skillName + " = (SELECT " + skillName + " FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " +
"WHERE user = '" + playerName + "') ORDER BY user"; "WHERE user = '" + playerName + "') ORDER BY user";
resultSet.close();
statement.close(); statement.close();
statement = connection.prepareStatement(sql); statement = connection.prepareStatement(sql);
@ -417,16 +431,17 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
} }
resultSet.close();
statement.close(); statement.close();
} }
String sql = "SELECT COUNT(*) AS rank FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " + String sql = "SELECT COUNT(*) AS rank FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " +
"WHERE taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing+alchemy > 0 " + "WHERE " + ALL_QUERY_VERSION + " > 0 " +
"AND taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing+alchemy > " + "AND " + ALL_QUERY_VERSION + " > " +
"(SELECT taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing+alchemy " + "(SELECT " + ALL_QUERY_VERSION + " " +
"FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE user = ?)"; "FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE user = ?)";
PreparedStatement statement = connection.prepareStatement(sql); statement = connection.prepareStatement(sql);
statement.setString(1, playerName); statement.setString(1, playerName);
resultSet = statement.executeQuery(); resultSet = statement.executeQuery();
@ -434,13 +449,14 @@ public final class SQLDatabaseManager implements DatabaseManager {
int rank = resultSet.getInt("rank"); int rank = resultSet.getInt("rank");
resultSet.close();
statement.close(); statement.close();
sql = "SELECT user, taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing+alchemy " + sql = "SELECT user, " + ALL_QUERY_VERSION + " " +
"FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " + "FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " +
"WHERE taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing+alchemy > 0 " + "WHERE " + ALL_QUERY_VERSION + " > 0 " +
"AND taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing+alchemy = " + "AND " + ALL_QUERY_VERSION + " = " +
"(SELECT taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing+alchemy " + "(SELECT " + ALL_QUERY_VERSION + " " +
"FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE user = ?) ORDER BY user"; "FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE user = ?) ORDER BY user";
statement = connection.prepareStatement(sql); statement = connection.prepareStatement(sql);
@ -454,10 +470,25 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
} }
resultSet.close();
statement.close(); statement.close();
} catch (SQLException ex) { } catch (SQLException ex) {
printErrors(ex); printErrors(ex);
} finally { } finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
// Ignore
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
// Ignore
}
}
if (connection != null) { if (connection != null) {
try { try {
connection.close(); connection.close();
@ -534,6 +565,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
private PlayerProfile loadPlayerNameProfile(String playerName, String uuid, boolean create, boolean retry) { private PlayerProfile loadPlayerNameProfile(String playerName, String uuid, boolean create, boolean retry) {
PreparedStatement statement = null; PreparedStatement statement = null;
Connection connection = null; Connection connection = null;
ResultSet resultSet = null;
try { try {
connection = connectionPool.getConnection(VALID_TIMEOUT); connection = connectionPool.getConnection(VALID_TIMEOUT);
@ -566,20 +598,25 @@ public final class SQLDatabaseManager implements DatabaseManager {
+ "WHERE u.user = ?"); + "WHERE u.user = ?");
statement.setString(1, playerName); statement.setString(1, playerName);
ResultSet result = statement.executeQuery(); resultSet = statement.executeQuery();
if (result.next()) { if (resultSet.next()) {
try { try {
PlayerProfile ret = loadFromResult(playerName, result); PlayerProfile ret = loadFromResult(playerName, resultSet);
result.close();
return ret; return ret;
} catch (SQLException e) { } catch (SQLException e) {
} }
} }
result.close();
} catch (SQLException ex) { } catch (SQLException ex) {
printErrors(ex); printErrors(ex);
} finally { } finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
// Ignore
}
}
if (statement != null) { if (statement != null) {
try { try {
statement.close(); statement.close();
@ -623,6 +660,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
private PlayerProfile loadPlayerProfile(String playerName, String uuid, boolean create, boolean retry) { private PlayerProfile loadPlayerProfile(String playerName, String uuid, boolean create, boolean retry) {
PreparedStatement statement = null; PreparedStatement statement = null;
Connection connection = null; Connection connection = null;
ResultSet resultSet = null;
try { try {
connection = connectionPool.getConnection(VALID_TIMEOUT); connection = connectionPool.getConnection(VALID_TIMEOUT);
@ -655,12 +693,12 @@ public final class SQLDatabaseManager implements DatabaseManager {
+ "WHERE u.UUID = ?"); + "WHERE u.UUID = ?");
statement.setString(1, uuid); statement.setString(1, uuid);
ResultSet result = statement.executeQuery(); resultSet = statement.executeQuery();
if (result.next()) { if (resultSet.next()) {
try { try {
PlayerProfile profile = loadFromResult(playerName, result); PlayerProfile profile = loadFromResult(playerName, resultSet);
result.close(); resultSet.close();
statement.close(); statement.close();
if (!playerName.isEmpty() && !profile.getPlayerName().isEmpty()) { if (!playerName.isEmpty() && !profile.getPlayerName().isEmpty()) {
@ -670,18 +708,25 @@ public final class SQLDatabaseManager implements DatabaseManager {
+ "WHERE UUID = ?"); + "WHERE UUID = ?");
statement.setString(1, playerName); statement.setString(1, playerName);
statement.setString(2, uuid); statement.setString(2, uuid);
result = statement.executeQuery(); statement.executeUpdate();
result.close(); statement.close();
} }
return profile; return profile;
} catch (SQLException e) { } catch (SQLException e) {
} }
} }
result.close(); resultSet.close();
} catch (SQLException ex) { } catch (SQLException ex) {
printErrors(ex); printErrors(ex);
} finally { } finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
// Ignore
}
}
if (statement != null) { if (statement != null) {
try { try {
statement.close(); statement.close();
@ -712,6 +757,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
public void convertUsers(DatabaseManager destination) { public void convertUsers(DatabaseManager destination) {
PreparedStatement statement = null; PreparedStatement statement = null;
Connection connection = null; Connection connection = null;
ResultSet resultSet = null;
try { try {
connection = connectionPool.getConnection(VALID_TIMEOUT); connection = connectionPool.getConnection(VALID_TIMEOUT);
@ -728,7 +774,6 @@ public final class SQLDatabaseManager implements DatabaseManager {
+ "JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) " + "JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) "
+ "WHERE u.user = ?"); + "WHERE u.user = ?");
List<String> usernames = getStoredUsers(); List<String> usernames = getStoredUsers();
ResultSet resultSet;
int convertedUsers = 0; int convertedUsers = 0;
long startMillis = System.currentTimeMillis(); long startMillis = System.currentTimeMillis();
for (String playerName : usernames) { for (String playerName : usernames) {
@ -747,6 +792,13 @@ public final class SQLDatabaseManager implements DatabaseManager {
} catch (SQLException e) { } catch (SQLException e) {
printErrors(e); printErrors(e);
} finally { } finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
// Ignore
}
}
if (statement != null) { if (statement != null) {
try { try {
statement.close(); statement.close();
@ -797,8 +849,6 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
} }
} }
// Problem, nothing was returned
} }
public boolean saveUserUUIDs(Map<String, UUID> fetchedUUIDs) { public boolean saveUserUUIDs(Map<String, UUID> fetchedUUIDs) {
@ -854,23 +904,30 @@ public final class SQLDatabaseManager implements DatabaseManager {
public List<String> getStoredUsers() { public List<String> getStoredUsers() {
ArrayList<String> users = new ArrayList<String>(); ArrayList<String> users = new ArrayList<String>();
Statement stmt = null; Statement statement = null;
Connection connection = null; Connection connection = null;
ResultSet resultSet = null;
try { try {
connection = connectionPool.getConnection(VALID_TIMEOUT); connection = connectionPool.getConnection(VALID_TIMEOUT);
stmt = connection.createStatement(); statement = connection.createStatement();
ResultSet result = stmt.executeQuery("SELECT user FROM " + tablePrefix + "users"); resultSet = statement.executeQuery("SELECT user FROM " + tablePrefix + "users");
while (result.next()) { while (resultSet.next()) {
users.add(result.getString("user")); users.add(resultSet.getString("user"));
} }
result.close();
} catch (SQLException e) { } catch (SQLException e) {
printErrors(e); printErrors(e);
} finally { } finally {
if (stmt != null) { if (resultSet != null) {
try { try {
stmt.close(); resultSet.close();
} catch (SQLException e) {
// Ignore
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) { } catch (SQLException e) {
// Ignore // Ignore
} }
@ -1468,4 +1525,9 @@ public final class SQLDatabaseManager implements DatabaseManager {
return -1; return -1;
} }
@Override
public void onDisable() {
connectionPool.release();
}
} }

View File

@ -242,6 +242,7 @@ public class mcMMO extends JavaPlugin {
} }
} }
databaseManager.onDisable();
debug("Was disabled."); // How informative! debug("Was disabled."); // How informative!
} }