Fix #2197 There is no wait forever option, so we actually need a timeout.

This commit is contained in:
t00thpick1 2014-08-12 11:39:43 -04:00
parent 294141c353
commit 0a066f51bb
2 changed files with 25 additions and 17 deletions

View File

@ -314,8 +314,8 @@ public class Config extends AutoUpdateConfigLoader {
public int getMySQLServerPort() { return config.getInt("MySQL.Server.Port", 3306); } public int getMySQLServerPort() { return config.getInt("MySQL.Server.Port", 3306); }
public String getMySQLServerName() { return config.getString("MySQL.Server.Address", "localhost"); } public String getMySQLServerName() { return config.getString("MySQL.Server.Address", "localhost"); }
public String getMySQLUserPassword() { return getStringIncludingInts("MySQL.Database.User_Password"); } public String getMySQLUserPassword() { return getStringIncludingInts("MySQL.Database.User_Password"); }
public int getMySQLMaxConnections() { return config.getInt("MySQL.Database.MaxConnections"); } public int getMySQLMaxConnections() { return config.getInt("MySQL.Database.MaxConnections", 30); }
public int getMySQLMaxPoolSize() { return config.getInt("MySQL.Database.MaxPoolSize"); } public int getMySQLMaxPoolSize() { return config.getInt("MySQL.Database.MaxPoolSize", 20); }
private String getStringIncludingInts(String key) { private String getStringIncludingInts(String key) {
String str = config.getString(key); String str = config.getString(key);

View File

@ -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 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 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<UUID, Integer> cachedUserIDs = new HashMap<UUID, Integer>(); private final Map<UUID, Integer> cachedUserIDs = new HashMap<UUID, Integer>();
@ -88,7 +88,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
int purged = 0; int purged = 0;
try { try {
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); connection = getConnection();
statement = connection.createStatement(); statement = connection.createStatement();
purged = statement.executeUpdate("DELETE FROM u, e, h, s, c USING " + tablePrefix + "users u " + 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; int purged = 0;
try { try {
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); connection = getConnection();
statement = connection.createStatement(); statement = connection.createStatement();
purged = statement.executeUpdate("DELETE FROM u, e, h, s, c USING " + tablePrefix + "users u " + 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; PreparedStatement statement = null;
try { try {
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); connection = getConnection();
statement = connection.prepareStatement("DELETE FROM u, e, h, s, c " + statement = connection.prepareStatement("DELETE FROM u, e, h, s, c " +
"USING " + tablePrefix + "users u " + "USING " + tablePrefix + "users u " +
"JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) " + "JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) " +
@ -223,7 +223,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
Connection connection = null; Connection connection = null;
try { try {
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); connection = getConnection();
int id = getUserID(connection, profile.getPlayerName(), profile.getUniqueId()); int id = getUserID(connection, profile.getPlayerName(), profile.getUniqueId());
@ -338,7 +338,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
Connection connection = null; Connection connection = null;
try { 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 = 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(1, (pageNumber * statsPerPage) - statsPerPage);
statement.setInt(2, statsPerPage); statement.setInt(2, statsPerPage);
@ -395,7 +395,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
Connection connection = null; Connection connection = null;
try { try {
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); connection = getConnection();
for (SkillType skillType : SkillType.NON_CHILD_SKILLS) { for (SkillType skillType : SkillType.NON_CHILD_SKILLS) {
String skillName = skillType.name().toLowerCase(); String skillName = skillType.name().toLowerCase();
String sql = "SELECT COUNT(*) AS rank FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE " + skillName + " > 0 " + 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; Connection connection = null;
try { try {
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); connection = getConnection();
newUser(connection, playerName, uuid); newUser(connection, playerName, uuid);
} }
catch (SQLException ex) { catch (SQLException ex) {
@ -586,7 +586,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
ResultSet resultSet = null; ResultSet resultSet = null;
try { try {
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); connection = getConnection();
int id = getUserID(connection, playerName, uuid); int id = getUserID(connection, playerName, uuid);
if (id == -1) { if (id == -1) {
@ -692,7 +692,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
ResultSet resultSet = null; ResultSet resultSet = null;
try { try {
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); connection = getConnection();
statement = connection.prepareStatement( statement = connection.prepareStatement(
"SELECT " "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, " + "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; Connection connection = null;
try { try {
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); connection = getConnection();
statement = connection.prepareStatement( statement = connection.prepareStatement(
"UPDATE `" + tablePrefix + "users` SET " "UPDATE `" + tablePrefix + "users` SET "
+ " uuid = ? WHERE user = ?"); + " uuid = ? WHERE user = ?");
@ -800,7 +800,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
Connection connection = null; Connection connection = null;
try { try {
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); connection = getConnection();
statement = connection.prepareStatement("UPDATE " + tablePrefix + "users SET uuid = ? WHERE user = ?"); statement = connection.prepareStatement("UPDATE " + tablePrefix + "users SET uuid = ? WHERE user = ?");
for (Map.Entry<String, UUID> entry : fetchedUUIDs.entrySet()) { for (Map.Entry<String, UUID> entry : fetchedUUIDs.entrySet()) {
@ -855,7 +855,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
ResultSet resultSet = null; ResultSet resultSet = null;
try { try {
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); connection = getConnection();
statement = connection.createStatement(); statement = connection.createStatement();
resultSet = statement.executeQuery("SELECT user FROM " + tablePrefix + "users"); resultSet = statement.executeQuery("SELECT user FROM " + tablePrefix + "users");
while (resultSet.next()) { while (resultSet.next()) {
@ -906,7 +906,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
Connection connection = null; Connection connection = null;
try { try {
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); connection = getConnection();
statement = connection.prepareStatement("SELECT table_name FROM INFORMATION_SCHEMA.TABLES" statement = connection.prepareStatement("SELECT table_name FROM INFORMATION_SCHEMA.TABLES"
+ " WHERE table_schema = ?" + " WHERE table_schema = ?"
+ " AND table_name = ?"); + " 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. * Check database structure for necessary upgrades.
* *
@ -1250,7 +1258,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
private void printErrors(SQLException ex) { private void printErrors(SQLException ex) {
StackTraceElement element = ex.getStackTrace()[ex.getStackTrace().length - 1]; 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("SQLException: " + ex.getMessage());
mcMMO.p.getLogger().severe("SQLState: " + ex.getSQLState()); mcMMO.p.getLogger().severe("SQLState: " + ex.getSQLState());
mcMMO.p.getLogger().severe("VendorError: " + ex.getErrorCode()); mcMMO.p.getLogger().severe("VendorError: " + ex.getErrorCode());