|
@@ -33,14 +33,15 @@ import snaq.db.ConnectionPool;
|
|
|
|
|
|
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 static final String ALL_QUERY_VERSION = "taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing+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 = 360000;
|
|
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>();
|
|
|
|
|
|
- private ConnectionPool connectionPool;
|
|
|
|
|
|
+ private ConnectionPool miscPool;
|
|
|
|
+ private ConnectionPool loadPool;
|
|
|
|
+ private ConnectionPool savePool;
|
|
|
|
|
|
private ReentrantLock massUpdateLock = new ReentrantLock();
|
|
private ReentrantLock massUpdateLock = new ReentrantLock();
|
|
|
|
|
|
@@ -65,15 +66,33 @@ public final class SQLDatabaseManager implements DatabaseManager {
|
|
connectionProperties.put("prepStmtCacheSize", "64");
|
|
connectionProperties.put("prepStmtCacheSize", "64");
|
|
connectionProperties.put("prepStmtCacheSqlLimit", "2048");
|
|
connectionProperties.put("prepStmtCacheSqlLimit", "2048");
|
|
connectionProperties.put("useServerPrepStmts", "true");
|
|
connectionProperties.put("useServerPrepStmts", "true");
|
|
- connectionPool = new ConnectionPool("mcMMO-Pool",
|
|
|
|
|
|
+ miscPool = new ConnectionPool("mcMMO-Misc-Pool",
|
|
|
|
+ 0 /*No Minimum really needed*/,
|
|
|
|
+ Config.getInstance().getMySQLMaxPoolSize(PoolIdentifier.MISC) /*max pool size */,
|
|
|
|
+ Config.getInstance().getMySQLMaxConnections(PoolIdentifier.MISC) /*max num connections*/,
|
|
|
|
+ 0 /* idle timeout of connections */,
|
|
|
|
+ connectionString,
|
|
|
|
+ connectionProperties);
|
|
|
|
+ loadPool = new ConnectionPool("mcMMO-Load-Pool",
|
|
1 /*Minimum of one*/,
|
|
1 /*Minimum of one*/,
|
|
- Config.getInstance().getMySQLMaxPoolSize() /*max pool size */,
|
|
|
|
- Config.getInstance().getMySQLMaxConnections() /*max num connections*/,
|
|
|
|
|
|
+ Config.getInstance().getMySQLMaxPoolSize(PoolIdentifier.LOAD) /*max pool size */,
|
|
|
|
+ Config.getInstance().getMySQLMaxConnections(PoolIdentifier.LOAD) /*max num connections*/,
|
|
0 /* idle timeout of connections */,
|
|
0 /* idle timeout of connections */,
|
|
connectionString,
|
|
connectionString,
|
|
connectionProperties);
|
|
connectionProperties);
|
|
- connectionPool.init(); // Init first connection
|
|
|
|
- connectionPool.registerShutdownHook(); // Auto release on jvm exit just in case
|
|
|
|
|
|
+ savePool = new ConnectionPool("mcMMO-Save-Pool",
|
|
|
|
+ 1 /*Minimum of one*/,
|
|
|
|
+ Config.getInstance().getMySQLMaxPoolSize(PoolIdentifier.SAVE) /*max pool size */,
|
|
|
|
+ Config.getInstance().getMySQLMaxConnections(PoolIdentifier.SAVE) /*max num connections*/,
|
|
|
|
+ 0 /* idle timeout of connections */,
|
|
|
|
+ connectionString,
|
|
|
|
+ connectionProperties);
|
|
|
|
+ miscPool.init(); // Init first connection
|
|
|
|
+ miscPool.registerShutdownHook(); // Auto release on jvm exit just in case
|
|
|
|
+ loadPool.init();
|
|
|
|
+ loadPool.registerShutdownHook();
|
|
|
|
+ savePool.init();
|
|
|
|
+ savePool.registerShutdownHook();
|
|
|
|
|
|
checkStructure();
|
|
checkStructure();
|
|
|
|
|
|
@@ -88,7 +107,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
|
|
int purged = 0;
|
|
int purged = 0;
|
|
|
|
|
|
try {
|
|
try {
|
|
- connection = getConnection();
|
|
|
|
|
|
+ connection = getConnection(PoolIdentifier.MISC);
|
|
statement = connection.createStatement();
|
|
statement = connection.createStatement();
|
|
|
|
|
|
purged = statement.executeUpdate("DELETE FROM " + tablePrefix + "skills WHERE "
|
|
purged = statement.executeUpdate("DELETE FROM " + tablePrefix + "skills WHERE "
|
|
@@ -137,7 +156,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
|
|
int purged = 0;
|
|
int purged = 0;
|
|
|
|
|
|
try {
|
|
try {
|
|
- connection = getConnection();
|
|
|
|
|
|
+ connection = getConnection(PoolIdentifier.MISC);
|
|
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 " +
|
|
@@ -179,7 +198,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
|
|
PreparedStatement statement = null;
|
|
PreparedStatement statement = null;
|
|
|
|
|
|
try {
|
|
try {
|
|
- connection = getConnection();
|
|
|
|
|
|
+ connection = getConnection(PoolIdentifier.MISC);
|
|
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) " +
|
|
@@ -227,7 +246,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
|
|
Connection connection = null;
|
|
Connection connection = null;
|
|
|
|
|
|
try {
|
|
try {
|
|
- connection = getConnection();
|
|
|
|
|
|
+ connection = getConnection(PoolIdentifier.SAVE);
|
|
|
|
|
|
int id = getUserID(connection, profile.getPlayerName(), profile.getUniqueId());
|
|
int id = getUserID(connection, profile.getPlayerName(), profile.getUniqueId());
|
|
|
|
|
|
@@ -342,7 +361,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
|
|
Connection connection = null;
|
|
Connection connection = null;
|
|
|
|
|
|
try {
|
|
try {
|
|
- connection = getConnection();
|
|
|
|
|
|
+ connection = getConnection(PoolIdentifier.MISC);
|
|
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);
|
|
@@ -399,7 +418,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
|
|
Connection connection = null;
|
|
Connection connection = null;
|
|
|
|
|
|
try {
|
|
try {
|
|
- connection = getConnection();
|
|
|
|
|
|
+ connection = getConnection(PoolIdentifier.MISC);
|
|
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 " +
|
|
@@ -510,7 +529,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
|
|
Connection connection = null;
|
|
Connection connection = null;
|
|
|
|
|
|
try {
|
|
try {
|
|
- connection = getConnection();
|
|
|
|
|
|
+ connection = getConnection(PoolIdentifier.MISC);
|
|
newUser(connection, playerName, uuid);
|
|
newUser(connection, playerName, uuid);
|
|
}
|
|
}
|
|
catch (SQLException ex) {
|
|
catch (SQLException ex) {
|
|
@@ -590,7 +609,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
|
|
ResultSet resultSet = null;
|
|
ResultSet resultSet = null;
|
|
|
|
|
|
try {
|
|
try {
|
|
- connection = getConnection();
|
|
|
|
|
|
+ connection = getConnection(PoolIdentifier.LOAD);
|
|
int id = getUserID(connection, playerName, uuid);
|
|
int id = getUserID(connection, playerName, uuid);
|
|
|
|
|
|
if (id == -1) {
|
|
if (id == -1) {
|
|
@@ -696,7 +715,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
|
|
ResultSet resultSet = null;
|
|
ResultSet resultSet = null;
|
|
|
|
|
|
try {
|
|
try {
|
|
- connection = getConnection();
|
|
|
|
|
|
+ connection = getConnection(PoolIdentifier.MISC);
|
|
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, "
|
|
@@ -764,7 +783,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
|
|
Connection connection = null;
|
|
Connection connection = null;
|
|
|
|
|
|
try {
|
|
try {
|
|
- connection = getConnection();
|
|
|
|
|
|
+ connection = getConnection(PoolIdentifier.MISC);
|
|
statement = connection.prepareStatement(
|
|
statement = connection.prepareStatement(
|
|
"UPDATE `" + tablePrefix + "users` SET "
|
|
"UPDATE `" + tablePrefix + "users` SET "
|
|
+ " uuid = ? WHERE user = ?");
|
|
+ " uuid = ? WHERE user = ?");
|
|
@@ -804,7 +823,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
|
|
Connection connection = null;
|
|
Connection connection = null;
|
|
|
|
|
|
try {
|
|
try {
|
|
- connection = getConnection();
|
|
|
|
|
|
+ connection = getConnection(PoolIdentifier.MISC);
|
|
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()) {
|
|
@@ -859,7 +878,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
|
|
ResultSet resultSet = null;
|
|
ResultSet resultSet = null;
|
|
|
|
|
|
try {
|
|
try {
|
|
- connection = getConnection();
|
|
|
|
|
|
+ connection = getConnection(PoolIdentifier.MISC);
|
|
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()) {
|
|
@@ -910,7 +929,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
|
|
Connection connection = null;
|
|
Connection connection = null;
|
|
|
|
|
|
try {
|
|
try {
|
|
- connection = getConnection();
|
|
|
|
|
|
+ connection = getConnection(PoolIdentifier.MISC);
|
|
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 = ?");
|
|
@@ -1070,10 +1089,21 @@ public final class SQLDatabaseManager implements DatabaseManager {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- private Connection getConnection() throws SQLException {
|
|
|
|
- Connection connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
|
|
|
|
|
|
+ private Connection getConnection(PoolIdentifier identifier) throws SQLException {
|
|
|
|
+ Connection connection = null;
|
|
|
|
+ switch (identifier) {
|
|
|
|
+ case LOAD:
|
|
|
|
+ connection = loadPool.getConnection(POOL_FETCH_TIMEOUT);
|
|
|
|
+ break;
|
|
|
|
+ case MISC:
|
|
|
|
+ connection = miscPool.getConnection(POOL_FETCH_TIMEOUT);
|
|
|
|
+ break;
|
|
|
|
+ case SAVE:
|
|
|
|
+ connection = savePool.getConnection(POOL_FETCH_TIMEOUT);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
if (connection == null) {
|
|
if (connection == null) {
|
|
- throw new RuntimeException("getConnection() timed out. Increase max connections settings.");
|
|
|
|
|
|
+ throw new RuntimeException("getConnection() for " + identifier.name().toLowerCase() + " pool timed out. Increase max connections settings.");
|
|
}
|
|
}
|
|
return connection;
|
|
return connection;
|
|
}
|
|
}
|
|
@@ -1399,7 +1429,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
|
|
ResultSet resultSet = null;
|
|
ResultSet resultSet = null;
|
|
try {
|
|
try {
|
|
try {
|
|
try {
|
|
- connection = connectionPool.getConnection();
|
|
|
|
|
|
+ connection = miscPool.getConnection();
|
|
statement = connection.createStatement();
|
|
statement = connection.createStatement();
|
|
resultSet = statement.executeQuery("SELECT `user` FROM `" + tablePrefix + "users` WHERE `uuid` IS NULL");
|
|
resultSet = statement.executeQuery("SELECT `user` FROM `" + tablePrefix + "users` WHERE `uuid` IS NULL");
|
|
|
|
|
|
@@ -1563,6 +1593,14 @@ public final class SQLDatabaseManager implements DatabaseManager {
|
|
@Override
|
|
@Override
|
|
public void onDisable() {
|
|
public void onDisable() {
|
|
mcMMO.p.debug("Releasing connection pool resource...");
|
|
mcMMO.p.debug("Releasing connection pool resource...");
|
|
- connectionPool.release();
|
|
|
|
|
|
+ miscPool.release();
|
|
|
|
+ loadPool.release();
|
|
|
|
+ savePool.release();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public enum PoolIdentifier {
|
|
|
|
+ MISC,
|
|
|
|
+ LOAD,
|
|
|
|
+ SAVE;
|
|
}
|
|
}
|
|
}
|
|
}
|