Switch to a different ConnectionPool implementation

This commit is contained in:
t00thpick1 2014-10-14 23:59:41 -04:00
parent dbc7b98b0d
commit 4723a7cbdb
2 changed files with 65 additions and 52 deletions

View File

@ -147,9 +147,10 @@
<version>0.0.5-SNAPSHOT</version> <version>0.0.5-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.snaq</groupId> <groupId>org.apache.tomcat</groupId>
<artifactId>dbpool</artifactId> <artifactId>tomcat-jdbc</artifactId>
<version>5.1</version> <version>7.0.52</version>
<scope>compile</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<distributionManagement> <distributionManagement>

View File

@ -15,6 +15,9 @@ import java.util.Properties;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import org.apache.tomcat.jdbc.pool.ConnectionPool;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
@ -29,19 +32,15 @@ import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.runnables.database.UUIDUpdateAsyncTask; import com.gmail.nossr50.runnables.database.UUIDUpdateAsyncTask;
import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Misc;
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 String tablePrefix = Config.getInstance().getMySQLTablePrefix(); private String tablePrefix = Config.getInstance().getMySQLTablePrefix();
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 miscPool; private DataSource miscPool;
private ConnectionPool loadPool; private DataSource loadPool;
private ConnectionPool savePool; private DataSource savePool;
private ReentrantLock massUpdateLock = new ReentrantLock(); private ReentrantLock massUpdateLock = new ReentrantLock();
@ -58,44 +57,57 @@ public final class SQLDatabaseManager implements DatabaseManager {
//throw e; // aborts onEnable() Riking if you want to do this, fully implement it. //throw e; // aborts onEnable() Riking if you want to do this, fully implement it.
} }
Properties connectionProperties = new Properties();
connectionProperties.put("user", Config.getInstance().getMySQLUserName()); PoolProperties poolProperties = new PoolProperties();
connectionProperties.put("password", Config.getInstance().getMySQLUserPassword()); poolProperties.setDriverClassName("com.mysql.jdbc.Driver");
connectionProperties.put("autoReconnect", "true"); poolProperties.setUrl(connectionString);
connectionProperties.put("cachePrepStmts", "true"); poolProperties.setUsername(Config.getInstance().getMySQLUserName());
connectionProperties.put("prepStmtCacheSize", "64"); poolProperties.setPassword(Config.getInstance().getMySQLUserPassword());
connectionProperties.put("prepStmtCacheSqlLimit", "2048"); poolProperties.setMaxIdle(Config.getInstance().getMySQLMaxPoolSize(PoolIdentifier.MISC));
connectionProperties.put("useServerPrepStmts", "true"); poolProperties.setMaxActive(Config.getInstance().getMySQLMaxConnections(PoolIdentifier.MISC));
miscPool = new ConnectionPool("mcMMO-Misc-Pool", poolProperties.setInitialSize(0);
0 /*No Minimum really needed*/, poolProperties.setMaxIdle(400);
Config.getInstance().getMySQLMaxPoolSize(PoolIdentifier.MISC) /*max pool size */, poolProperties.setMaxWait(-1);
Config.getInstance().getMySQLMaxConnections(PoolIdentifier.MISC) /*max num connections*/, poolProperties.setRemoveAbandoned(true);
400 /* idle timeout of connections */, poolProperties.setRemoveAbandonedTimeout(60);
connectionString, poolProperties.setTestOnBorrow(true);
connectionProperties); poolProperties.setValidationQuery("SELECT 1");
loadPool = new ConnectionPool("mcMMO-Load-Pool", poolProperties.setValidationInterval(30000);
1 /*Minimum of one*/, miscPool = new DataSource(poolProperties);
Config.getInstance().getMySQLMaxPoolSize(PoolIdentifier.LOAD) /*max pool size */, poolProperties = new PoolProperties();
Config.getInstance().getMySQLMaxConnections(PoolIdentifier.LOAD) /*max num connections*/, poolProperties.setDriverClassName("com.mysql.jdbc.Driver");
400 /* idle timeout of connections */, poolProperties.setUrl(connectionString);
connectionString, poolProperties.setUsername(Config.getInstance().getMySQLUserName());
connectionProperties); poolProperties.setPassword(Config.getInstance().getMySQLUserPassword());
savePool = new ConnectionPool("mcMMO-Save-Pool", poolProperties.setInitialSize(0);
1 /*Minimum of one*/, poolProperties.setMaxIdle(Config.getInstance().getMySQLMaxPoolSize(PoolIdentifier.SAVE));
Config.getInstance().getMySQLMaxPoolSize(PoolIdentifier.SAVE) /*max pool size */, poolProperties.setMaxActive(Config.getInstance().getMySQLMaxConnections(PoolIdentifier.SAVE));
Config.getInstance().getMySQLMaxConnections(PoolIdentifier.SAVE) /*max num connections*/, poolProperties.setMaxIdle(400);
400 /* idle timeout of connections */, poolProperties.setMaxWait(-1);
connectionString, poolProperties.setRemoveAbandoned(true);
connectionProperties); poolProperties.setRemoveAbandonedTimeout(60);
miscPool.init(); // Init first connection poolProperties.setTestOnBorrow(true);
miscPool.registerShutdownHook(); // Auto release on jvm exit just in case poolProperties.setValidationQuery("SELECT 1");
loadPool.init(); poolProperties.setValidationInterval(30000);
loadPool.registerShutdownHook(); savePool = new DataSource(poolProperties);
savePool.init(); poolProperties = new PoolProperties();
savePool.registerShutdownHook(); poolProperties.setDriverClassName("com.mysql.jdbc.Driver");
poolProperties.setUrl(connectionString);
poolProperties.setUsername(Config.getInstance().getMySQLUserName());
poolProperties.setPassword(Config.getInstance().getMySQLUserPassword());
poolProperties.setInitialSize(0);
poolProperties.setMaxIdle(Config.getInstance().getMySQLMaxPoolSize(PoolIdentifier.LOAD));
poolProperties.setMaxActive(Config.getInstance().getMySQLMaxConnections(PoolIdentifier.LOAD));
poolProperties.setMaxIdle(400);
poolProperties.setMaxWait(-1);
poolProperties.setRemoveAbandoned(true);
poolProperties.setRemoveAbandonedTimeout(60);
poolProperties.setTestOnBorrow(true);
poolProperties.setValidationQuery("SELECT 1");
poolProperties.setValidationInterval(30000);
loadPool = new DataSource(poolProperties);
checkStructure(); checkStructure();
} }
public void purgePowerlessUsers() { public void purgePowerlessUsers() {
@ -1094,13 +1106,13 @@ public final class SQLDatabaseManager implements DatabaseManager {
Connection connection = null; Connection connection = null;
switch (identifier) { switch (identifier) {
case LOAD: case LOAD:
connection = loadPool.getConnection(POOL_FETCH_TIMEOUT); connection = loadPool.getConnection();
break; break;
case MISC: case MISC:
connection = miscPool.getConnection(POOL_FETCH_TIMEOUT); connection = miscPool.getConnection();
break; break;
case SAVE: case SAVE:
connection = savePool.getConnection(POOL_FETCH_TIMEOUT); connection = savePool.getConnection();
break; break;
} }
if (connection == null) { if (connection == null) {
@ -1594,9 +1606,9 @@ 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...");
miscPool.release(); miscPool.close();
loadPool.release(); loadPool.close();
savePool.release(); savePool.close();
} }
public enum PoolIdentifier { public enum PoolIdentifier {