From 0a066f51bb1a877e31c8dc6732659c6b8f3c81a5 Mon Sep 17 00:00:00 2001 From: t00thpick1 Date: Tue, 12 Aug 2014 11:39:43 -0400 Subject: [PATCH] Fix #2197 There is no wait forever option, so we actually need a timeout. --- .../java/com/gmail/nossr50/config/Config.java | 4 +- .../nossr50/database/SQLDatabaseManager.java | 38 +++++++++++-------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/config/Config.java b/src/main/java/com/gmail/nossr50/config/Config.java index f7ce43726..0a0d6ac85 100644 --- a/src/main/java/com/gmail/nossr50/config/Config.java +++ b/src/main/java/com/gmail/nossr50/config/Config.java @@ -314,8 +314,8 @@ public class Config extends AutoUpdateConfigLoader { public int getMySQLServerPort() { return config.getInt("MySQL.Server.Port", 3306); } public String getMySQLServerName() { return config.getString("MySQL.Server.Address", "localhost"); } public String getMySQLUserPassword() { return getStringIncludingInts("MySQL.Database.User_Password"); } - public int getMySQLMaxConnections() { return config.getInt("MySQL.Database.MaxConnections"); } - public int getMySQLMaxPoolSize() { return config.getInt("MySQL.Database.MaxPoolSize"); } + public int getMySQLMaxConnections() { return config.getInt("MySQL.Database.MaxConnections", 30); } + public int getMySQLMaxPoolSize() { return config.getInt("MySQL.Database.MaxPoolSize", 20); } private String getStringIncludingInts(String key) { String str = config.getString(key); diff --git a/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java b/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java index 488798c50..876e86d6f 100644 --- a/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java +++ b/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java @@ -36,7 +36,7 @@ public final class SQLDatabaseManager implements DatabaseManager { private static final String S_ALL_QUERY_STRING = "s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing+s.alchemy"; private String tablePrefix = Config.getInstance().getMySQLTablePrefix(); - 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 int POOL_FETCH_TIMEOUT = 360000; private final Map cachedUserIDs = new HashMap(); @@ -88,7 +88,7 @@ public final class SQLDatabaseManager implements DatabaseManager { int purged = 0; try { - connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); + connection = getConnection(); statement = connection.createStatement(); purged = statement.executeUpdate("DELETE FROM u, e, h, s, c USING " + tablePrefix + "users u " + @@ -133,7 +133,7 @@ public final class SQLDatabaseManager implements DatabaseManager { int purged = 0; try { - connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); + connection = getConnection(); statement = connection.createStatement(); purged = statement.executeUpdate("DELETE FROM u, e, h, s, c USING " + tablePrefix + "users u " + @@ -175,7 +175,7 @@ public final class SQLDatabaseManager implements DatabaseManager { PreparedStatement statement = null; try { - connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); + connection = getConnection(); statement = connection.prepareStatement("DELETE FROM u, e, h, s, c " + "USING " + tablePrefix + "users u " + "JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) " + @@ -223,7 +223,7 @@ public final class SQLDatabaseManager implements DatabaseManager { Connection connection = null; try { - connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); + connection = getConnection(); int id = getUserID(connection, profile.getPlayerName(), profile.getUniqueId()); @@ -338,7 +338,7 @@ public final class SQLDatabaseManager implements DatabaseManager { Connection connection = null; try { - connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); + connection = getConnection(); 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); @@ -395,7 +395,7 @@ public final class SQLDatabaseManager implements DatabaseManager { Connection connection = null; try { - connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); + connection = getConnection(); 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 " + @@ -506,7 +506,7 @@ public final class SQLDatabaseManager implements DatabaseManager { Connection connection = null; try { - connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); + connection = getConnection(); newUser(connection, playerName, uuid); } catch (SQLException ex) { @@ -586,7 +586,7 @@ public final class SQLDatabaseManager implements DatabaseManager { ResultSet resultSet = null; try { - connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); + connection = getConnection(); int id = getUserID(connection, playerName, uuid); if (id == -1) { @@ -692,7 +692,7 @@ public final class SQLDatabaseManager implements DatabaseManager { ResultSet resultSet = null; try { - connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); + connection = getConnection(); 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, " @@ -760,7 +760,7 @@ public final class SQLDatabaseManager implements DatabaseManager { Connection connection = null; try { - connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); + connection = getConnection(); statement = connection.prepareStatement( "UPDATE `" + tablePrefix + "users` SET " + " uuid = ? WHERE user = ?"); @@ -800,7 +800,7 @@ public final class SQLDatabaseManager implements DatabaseManager { Connection connection = null; try { - connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); + connection = getConnection(); statement = connection.prepareStatement("UPDATE " + tablePrefix + "users SET uuid = ? WHERE user = ?"); for (Map.Entry entry : fetchedUUIDs.entrySet()) { @@ -855,7 +855,7 @@ public final class SQLDatabaseManager implements DatabaseManager { ResultSet resultSet = null; try { - connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); + connection = getConnection(); statement = connection.createStatement(); resultSet = statement.executeQuery("SELECT user FROM " + tablePrefix + "users"); while (resultSet.next()) { @@ -906,7 +906,7 @@ public final class SQLDatabaseManager implements DatabaseManager { Connection connection = null; try { - connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); + connection = getConnection(); statement = connection.prepareStatement("SELECT table_name FROM INFORMATION_SCHEMA.TABLES" + " WHERE table_schema = ?" + " AND table_name = ?"); @@ -1066,6 +1066,14 @@ public final class SQLDatabaseManager implements DatabaseManager { } + private Connection getConnection() throws SQLException { + Connection connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); + if (connection == null) { + throw new RuntimeException("getConnection() timed out. Increase max connections settings."); + } + return connection; + } + /** * Check database structure for necessary upgrades. * @@ -1250,7 +1258,7 @@ public final class SQLDatabaseManager implements DatabaseManager { private void printErrors(SQLException ex) { StackTraceElement element = ex.getStackTrace()[ex.getStackTrace().length - 1]; - mcMMO.p.getLogger().severe("Location: " + element.getMethodName() + " " + element.getLineNumber()); + mcMMO.p.getLogger().severe("Location: " + element.getClassName() + " " + element.getMethodName() + " " + element.getLineNumber()); mcMMO.p.getLogger().severe("SQLException: " + ex.getMessage()); mcMMO.p.getLogger().severe("SQLState: " + ex.getSQLState()); mcMMO.p.getLogger().severe("VendorError: " + ex.getErrorCode());