From 8de8d31eec54717e30497b0404d9c9f9b709e6a1 Mon Sep 17 00:00:00 2001 From: Jonathan Williams Date: Sat, 10 Jan 2015 11:53:25 -0800 Subject: [PATCH] Added pool based repository and converted everything but account and donation repositories to it. --- .../src/mineplex/core/database/DBPool.java | 4 +- .../core/database/RepositoryBase.java | 6 +- .../core/database/RepositoryPoolBase.java | 257 ++++++++++++++++++ .../src/mineplex/core/elo/EloRepository.java | 7 +- .../core/friend/data/FriendRepository.java | 6 +- .../inventory/data/InventoryRepository.java | 6 +- .../core/preferences/PreferencesManager.java | 2 +- .../preferences/PreferencesRepository.java | 14 +- .../mineplex/core/report/ReportManager.java | 2 +- .../core/report/ReportRepository.java | 18 +- .../mineplex/core/spawn/SpawnRepository.java | 6 +- .../src/mineplex/core/stats/StatsManager.java | 4 +- .../mineplex/core/stats/StatsRepository.java | 49 ++-- .../src/mineplex/hub/mail/MailRepository.java | 46 +++- .../src/mineplex/hub/poll/PollRepository.java | 6 +- .../password/PasswordRepository.java | 6 +- 16 files changed, 363 insertions(+), 76 deletions(-) create mode 100644 Plugins/Mineplex.Core/src/mineplex/core/database/RepositoryPoolBase.java diff --git a/Plugins/Mineplex.Core/src/mineplex/core/database/DBPool.java b/Plugins/Mineplex.Core/src/mineplex/core/database/DBPool.java index f542e1836..5c267f648 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/database/DBPool.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/database/DBPool.java @@ -23,8 +23,8 @@ public final class DBPool source.setUrl(url); source.setUsername(username); source.setPassword(password); - source.setMaxTotal(10); - source.setMaxIdle(3); + source.setMaxTotal(4); + source.setMaxIdle(2); source.setTimeBetweenEvictionRunsMillis(180 * 1000); source.setSoftMinEvictableIdleTimeMillis(180 * 1000); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/database/RepositoryBase.java b/Plugins/Mineplex.Core/src/mineplex/core/database/RepositoryBase.java index dd157d0af..56b6b11cd 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/database/RepositoryBase.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/database/RepositoryBase.java @@ -66,7 +66,7 @@ public abstract class RepositoryBase implements Listener { try { - if (_connection == null || !_connection.isValid(1)) + if (_connection == null) _connection = DriverManager.getConnection(_connectionString, _userName, _password); } catch (SQLException e) @@ -90,7 +90,7 @@ public abstract class RepositoryBase implements Listener try { - if (_connection == null || !_connection.isValid(1)) + if (_connection == null) _connection = DriverManager.getConnection(_connectionString, _userName, _password); preparedStatement = _connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); @@ -168,7 +168,7 @@ public abstract class RepositoryBase implements Listener try { - if (_connection == null || !_connection.isValid(1)) + if (_connection == null) _connection = DriverManager.getConnection(_connectionString, _userName, _password); preparedStatement = _connection.prepareStatement(query); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/database/RepositoryPoolBase.java b/Plugins/Mineplex.Core/src/mineplex/core/database/RepositoryPoolBase.java new file mode 100644 index 000000000..6fec0158c --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/database/RepositoryPoolBase.java @@ -0,0 +1,257 @@ +package mineplex.core.database; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Iterator; + +import javax.sql.DataSource; + +import mineplex.core.common.util.NautHashMap; +import mineplex.core.database.column.Column; +import mineplex.core.logger.Logger; +import mineplex.core.updater.UpdateType; +import mineplex.core.updater.event.UpdateEvent; + +import org.bukkit.Bukkit; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.plugin.java.JavaPlugin; + +public abstract class RepositoryPoolBase implements Listener +{ + protected static Object _connectionLock = new Object(); + + private DataSource _dataSource = null; + + private static Object _queueLock = new Object(); + + private NautHashMap _failedQueue = new NautHashMap(); + + protected JavaPlugin Plugin; + + public RepositoryPoolBase(JavaPlugin plugin, DataSource dataSource) + { + Plugin = plugin; + _dataSource = dataSource; + + Bukkit.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() + { + public void run() + { + synchronized (_connectionLock) + { + initialize(); + update(); + } + } + }); + + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + protected abstract void initialize(); + + protected abstract void update(); + + protected DataSource getDataSource() + { + return _dataSource; + } + + protected int executeUpdate(String query, Column...columns) + { + return executeInsert(query, null, columns); + } + + protected int executeInsert(String query, ResultSetCallable callable, Column...columns) + { + PreparedStatement preparedStatement = null; + + int affectedRows = 0; + + try (Connection connection = _dataSource.getConnection()) + { + preparedStatement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); + + for (int i=0; i < columns.length; i++) + { + columns[i].setValue(preparedStatement, i+1); + } + + affectedRows = preparedStatement.executeUpdate(); + + if (callable != null) + callable.processResultSet(preparedStatement.getGeneratedKeys()); + } + catch (Exception exception) + { + exception.printStackTrace(); + } + finally + { + if (preparedStatement != null) + { + try + { + preparedStatement.close(); + } + catch (SQLException e) + { + e.printStackTrace(); + } + } + } + + return affectedRows; + } + + protected void executeQuery(PreparedStatement statement, ResultSetCallable callable, Column...columns) + { + ResultSet resultSet = null; + + try + { + for (int i=0; i < columns.length; i++) + { + columns[i].setValue(statement, i+1); + } + + resultSet = statement.executeQuery(); + + callable.processResultSet(resultSet); + } + catch (Exception exception) + { + exception.printStackTrace(); + } + finally + { + if (resultSet != null) + { + try + { + resultSet.close(); + } + catch (SQLException e) + { + e.printStackTrace(); + } + } + } + } + + protected void executeQuery(String query, ResultSetCallable callable, Column...columns) + { + PreparedStatement preparedStatement = null; + + try (Connection connection = _dataSource.getConnection()) + { + preparedStatement = connection.prepareStatement(query); + + executeQuery(preparedStatement, callable, columns); + } + catch (Exception exception) + { + exception.printStackTrace(); + } + finally + { + if (preparedStatement != null) + { + try + { + preparedStatement.close(); + } + catch (SQLException e) + { + e.printStackTrace(); + } + } + } + } + + protected int executeUpdate(PreparedStatement preparedStatement, Column...columns) + { + int affectedRows = 0; + + try + { + for (int i=0; i < columns.length; i++) + { + columns[i].setValue(preparedStatement, i+1); + } + + affectedRows = preparedStatement.executeUpdate(); + } + catch (Exception exception) + { + exception.printStackTrace(); + } + + return affectedRows; + } + + protected void handleDatabaseCall(final DatabaseRunnable databaseRunnable, final String errorMessage) + { + Thread asyncThread = new Thread(new Runnable() + { + public void run() + { + try + { + databaseRunnable.run(); + } + catch (Exception exception) + { + processFailedDatabaseCall(databaseRunnable, exception.getMessage(), errorMessage); + } + } + }); + + asyncThread.start(); + } + + protected void processFailedDatabaseCall(DatabaseRunnable databaseRunnable, String errorPreMessage, String runnableMessage) + { + if (databaseRunnable.getFailedCounts() < 4) + { + databaseRunnable.incrementFailCount(); + + synchronized (_queueLock) + { + _failedQueue.put(databaseRunnable, runnableMessage); + } + + Logger.Instance.log(errorPreMessage + runnableMessage); + } + else + { + Logger.Instance.log("Abandoning database call : " + runnableMessage); + } + } + + @EventHandler + public void processDatabaseQueue(UpdateEvent event) + { + if (event.getType() != UpdateType.MIN_01) + return; + + processFailedQueue(); + } + + private void processFailedQueue() + { + synchronized (_queueLock) + { + for (Iterator runnablesIterator = _failedQueue.keySet().iterator(); runnablesIterator.hasNext();) + { + DatabaseRunnable databaseRunnable = runnablesIterator.next(); + handleDatabaseCall(databaseRunnable, _failedQueue.get(databaseRunnable)); + } + } + } +} diff --git a/Plugins/Mineplex.Core/src/mineplex/core/elo/EloRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/elo/EloRepository.java index ba8f1428a..a2d0a16e8 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/elo/EloRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/elo/EloRepository.java @@ -5,18 +5,19 @@ import java.sql.SQLException; import org.bukkit.plugin.java.JavaPlugin; -import mineplex.core.database.RepositoryBase; +import mineplex.core.database.DBPool; +import mineplex.core.database.RepositoryPoolBase; import mineplex.core.database.column.ColumnInt; import mineplex.core.database.column.ColumnVarChar; -public class EloRepository extends RepositoryBase +public class EloRepository extends RepositoryPoolBase { private static String CREATE_ELO_TABLE = "CREATE TABLE IF NOT EXISTS eloRating (id INT NOT NULL AUTO_INCREMENT, uuid VARCHAR(256), gameType VARCHAR(256), elo INT, PRIMARY KEY (id), UNIQUE INDEX uuid_gameType_index (uuid, gameType));"; private static String INSERT_ELO = "INSERT INTO eloRating (uuid, gameType, elo) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE elo=VALUES(elo);"; public EloRepository(JavaPlugin plugin) { - super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); + super(plugin, DBPool.ACCOUNT); initialize(); } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/friend/data/FriendRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/friend/data/FriendRepository.java index 9b23e121c..e65bdf0b7 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/friend/data/FriendRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/friend/data/FriendRepository.java @@ -7,12 +7,14 @@ import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; import mineplex.core.common.util.NautHashMap; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; +import mineplex.core.database.RepositoryPoolBase; import mineplex.core.database.ResultSetCallable; import mineplex.core.database.column.ColumnVarChar; import mineplex.core.friend.FriendStatusType; -public class FriendRepository extends RepositoryBase +public class FriendRepository extends RepositoryPoolBase { private static String CREATE_FRIEND_TABLE = "CREATE TABLE IF NOT EXISTS accountFriend (id INT NOT NULL AUTO_INCREMENT, uuidSource VARCHAR(100), uuidTarget VARCHAR(100), status VARCHAR(100), PRIMARY KEY (id), UNIQUE INDEX uuidIndex (uuidSource, uuidTarget));"; private static String RETRIEVE_MULTIPLE_FRIEND_RECORDS = "SELECT uuidSource, tA.Name, status, serverName, tA.lastLogin, now() FROM accountFriend INNER Join accounts AS fA ON fA.uuid = uuidSource INNER JOIN accounts AS tA ON tA.uuid = uuidTarget LEFT JOIN playerMap ON tA.name = playerName WHERE uuidSource IN "; @@ -22,7 +24,7 @@ public class FriendRepository extends RepositoryBase public FriendRepository(JavaPlugin plugin) { - super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); + super(plugin, DBPool.ACCOUNT); } @Override diff --git a/Plugins/Mineplex.Core/src/mineplex/core/inventory/data/InventoryRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/inventory/data/InventoryRepository.java index 2a1cb7f27..7ef667ffc 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/inventory/data/InventoryRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/inventory/data/InventoryRepository.java @@ -7,14 +7,16 @@ import java.util.List; import org.bukkit.plugin.java.JavaPlugin; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; +import mineplex.core.database.RepositoryPoolBase; import mineplex.core.database.ResultSetCallable; import mineplex.core.database.column.ColumnInt; import mineplex.core.database.column.ColumnVarChar; import mineplex.core.inventory.ClientInventory; import mineplex.core.inventory.ClientItem; -public class InventoryRepository extends RepositoryBase +public class InventoryRepository extends RepositoryPoolBase { private static String CREATE_INVENTORY_TABLE = "CREATE TABLE IF NOT EXISTS items (id INT NOT NULL AUTO_INCREMENT, name VARCHAR(100), categoryId INT, rarity INT, PRIMARY KEY (id), FOREIGN KEY (categoryId) REFERENCES itemCategories(id), UNIQUE INDEX uniqueNameCategoryIndex (name, categoryId));"; private static String CREATE_INVENTORY_CATEGORY_TABLE = "CREATE TABLE IF NOT EXISTS itemCategories (id INT NOT NULL AUTO_INCREMENT, name VARCHAR(100), PRIMARY KEY (id), UNIQUE INDEX nameIndex (name));"; @@ -31,7 +33,7 @@ public class InventoryRepository extends RepositoryBase public InventoryRepository(JavaPlugin plugin) { - super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); + super(plugin, DBPool.ACCOUNT); } @Override diff --git a/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesManager.java b/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesManager.java index 309cf6d79..5473b2bed 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesManager.java @@ -38,7 +38,7 @@ public class PreferencesManager extends MiniDbClientPlugin setupConfigValues(); - _repository = new PreferencesRepository(plugin, plugin.getConfig().getString("preferences.connectionurl")); + _repository = new PreferencesRepository(plugin); _shop = new PreferencesShop(this, clientManager, donationManager); addCommand(new PreferencesCommand(this)); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesRepository.java index 332f9b2db..9e90f84f9 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesRepository.java @@ -1,5 +1,6 @@ package mineplex.core.preferences; +import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -8,18 +9,19 @@ import java.util.Map.Entry; import org.bukkit.plugin.java.JavaPlugin; import mineplex.core.common.util.NautHashMap; -import mineplex.core.database.RepositoryBase; +import mineplex.core.database.DBPool; +import mineplex.core.database.RepositoryPoolBase; import mineplex.core.database.column.ColumnVarChar; -public class PreferencesRepository extends RepositoryBase +public class PreferencesRepository extends RepositoryPoolBase { private static String CREATE_ACCOUNT_TABLE = "CREATE TABLE IF NOT EXISTS accountPreferences (id INT NOT NULL AUTO_INCREMENT, uuid VARCHAR(256), games BOOL NOT NULL DEFAULT 1, visibility BOOL NOT NULL DEFAULT 1, showChat BOOL NOT NULL DEFAULT 1, friendChat BOOL NOT NULL DEFAULT 1, privateMessaging BOOL NOT NULL DEFAULT 1, partyRequests BOOL NOT NULL DEFAULT 0, invisibility BOOL NOT NULL DEFAULT 0, forcefield BOOL NOT NULL DEFAULT 0, showMacReports BOOL NOT NULL DEFAULT 0, ignoreVelocity BOOL NOT NULL DEFAULT 0, PRIMARY KEY (id), UNIQUE INDEX uuid_index (uuid));"; private static String INSERT_ACCOUNT = "INSERT INTO accountPreferences (uuid) VALUES (?) ON DUPLICATE KEY UPDATE uuid=uuid;"; private static String UPDATE_ACCOUNT_PREFERENCES = "UPDATE accountPreferences SET games = ?, visibility = ?, showChat = ?, friendChat = ?, privateMessaging = ?, partyRequests = ?, invisibility = ?, forcefield = ?, showMacReports = ?, ignoreVelocity = ?, pendingFriendRequests = ? WHERE uuid=?;"; - public PreferencesRepository(JavaPlugin plugin, String connectionString) + public PreferencesRepository(JavaPlugin plugin) { - super(plugin, connectionString, "root", "tAbechAk3wR7tuTh"); + super(plugin, DBPool.ACCOUNT); } @Override @@ -37,9 +39,9 @@ public class PreferencesRepository extends RepositoryBase { PreparedStatement preparedStatement = null; - try + try (Connection connection = getDataSource().getConnection()) { - preparedStatement = getConnection().prepareStatement(UPDATE_ACCOUNT_PREFERENCES); + preparedStatement = connection.prepareStatement(UPDATE_ACCOUNT_PREFERENCES); for (Entry entry : preferences.entrySet()) { diff --git a/Plugins/Mineplex.Core/src/mineplex/core/report/ReportManager.java b/Plugins/Mineplex.Core/src/mineplex/core/report/ReportManager.java index 63479b604..d70c25d8c 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/report/ReportManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/report/ReportManager.java @@ -51,7 +51,7 @@ public class ReportManager { this.activeReports = new HashMap(); // TODO: Get JavaPlugin instance and locate ConnectionString from config? - this.reportSqlRepository = new ReportRepository(ReportPlugin.getPlugin(), "CONNECTION STRING HERE"); + this.reportSqlRepository = new ReportRepository(ReportPlugin.getPlugin()); reportSqlRepository.initialize(); } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/report/ReportRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/report/ReportRepository.java index 968010e9c..c817be290 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/report/ReportRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/report/ReportRepository.java @@ -1,21 +1,13 @@ package mineplex.core.report; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.UUID; -import java.util.Map.Entry; - -import mineplex.core.common.util.NautHashMap; -import mineplex.core.database.RepositoryBase; -import mineplex.core.database.ResultSetCallable; +import mineplex.core.database.DBPool; +import mineplex.core.database.RepositoryPoolBase; import mineplex.core.database.column.ColumnInt; import mineplex.core.database.column.ColumnVarChar; -import mineplex.core.preferences.UserPreferences; import org.bukkit.plugin.java.JavaPlugin; -public class ReportRepository extends RepositoryBase +public class ReportRepository extends RepositoryPoolBase { /* * *ReportTicket @@ -38,9 +30,9 @@ This will be used to determine if staff are handling private static String INSERT_HANDLER = "INSERT INTO reportHandlers (eventDate, reportId, handlerId) VALUES(now(), ?, ?);"; private static String INSERT_SENDER = "INSERT INTO reportSenders (eventDate, reportId, reporterId, reason) VALUES(now(), ?, ?, ?);"; - public ReportRepository(JavaPlugin plugin, String connectionString) + public ReportRepository(JavaPlugin plugin) { - super(plugin, connectionString, "root", "tAbechAk3wR7tuTh"); // TODO: Config file for host/pass? + super(plugin, DBPool.ACCOUNT); // TODO: Config file for host/pass? } @Override diff --git a/Plugins/Mineplex.Core/src/mineplex/core/spawn/SpawnRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/spawn/SpawnRepository.java index 69a5e3b64..24fb70c5b 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/spawn/SpawnRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/spawn/SpawnRepository.java @@ -7,11 +7,13 @@ import java.util.List; import org.bukkit.plugin.java.JavaPlugin; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; +import mineplex.core.database.RepositoryPoolBase; import mineplex.core.database.ResultSetCallable; import mineplex.core.database.column.ColumnVarChar; -public class SpawnRepository extends RepositoryBase +public class SpawnRepository extends RepositoryPoolBase { private static String CREATE_SPAWN_TABLE = "CREATE TABLE IF NOT EXISTS spawns (id INT NOT NULL AUTO_INCREMENT, serverName VARCHAR(100), location VARCHAR(100), PRIMARY KEY (id), INDEX serverNameIndex (serverName));"; private static String RETRIEVE_SPAWNS = "SELECT location FROM spawns WHERE serverName = ?;"; @@ -22,7 +24,7 @@ public class SpawnRepository extends RepositoryBase public SpawnRepository(JavaPlugin plugin, String serverName) { - super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); + super(plugin, DBPool.ACCOUNT); _serverName = serverName; } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/stats/StatsManager.java b/Plugins/Mineplex.Core/src/mineplex/core/stats/StatsManager.java index eb86d46ce..51a51748d 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/stats/StatsManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/stats/StatsManager.java @@ -138,9 +138,7 @@ public class StatsManager extends MiniDbClientPlugin for (String statName : _statUploadQueue.get(player).keySet()) { - int statId = _stats.get(statName); - uploadQueue.get(uploadKey).put(statId, _statUploadQueue.get(player).get(statName)); - System.out.println(player.getName() + " saving stat : " + statName + " adding " + _statUploadQueue.get(player).get(statName)); + uploadQueue.get(uploadKey).put(_stats.get(statName), _statUploadQueue.get(player).get(statName)); } statIterator.remove(); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/stats/StatsRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/stats/StatsRepository.java index 6a34567c8..ca3ad944e 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/stats/StatsRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/stats/StatsRepository.java @@ -1,5 +1,6 @@ package mineplex.core.stats; +import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; @@ -9,7 +10,8 @@ import java.util.List; import org.bukkit.plugin.java.JavaPlugin; import mineplex.core.common.util.NautHashMap; -import mineplex.core.database.RepositoryBase; +import mineplex.core.database.DBPool; +import mineplex.core.database.RepositoryPoolBase; import mineplex.core.database.ResultSetCallable; import mineplex.core.database.column.ColumnVarChar; import mineplex.database.Tables; @@ -21,7 +23,7 @@ import org.jooq.Result; import org.jooq.Update; import org.jooq.impl.DSL; -public class StatsRepository extends RepositoryBase +public class StatsRepository extends RepositoryPoolBase { private static String CREATE_STAT_TABLE = "CREATE TABLE IF NOT EXISTS stats (id INT NOT NULL AUTO_INCREMENT, name VARCHAR(100), PRIMARY KEY (id), UNIQUE INDEX nameIndex (name));"; private static String CREATE_STAT_RELATION_TABLE = "CREATE TABLE IF NOT EXISTS accountStats (id INT NOT NULL AUTO_INCREMENT, accountId INT NOT NULL, statId INT NOT NULL, value INT NOT NULL, PRIMARY KEY (id), FOREIGN KEY (accountId) REFERENCES accounts(id), FOREIGN KEY (statId) REFERENCES stats(id), UNIQUE INDEX accountStatIndex (accountId, statId));"; @@ -31,7 +33,7 @@ public class StatsRepository extends RepositoryBase public StatsRepository(JavaPlugin plugin) { - super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); + super(plugin, DBPool.ACCOUNT); } @Override @@ -79,7 +81,7 @@ public class StatsRepository extends RepositoryBase { try { - DSLContext context = DSL.using(getConnection()); + DSLContext context = DSL.using(getDataSource().getConnection()); List updates = new ArrayList<>(); List inserts = new ArrayList<>(); @@ -130,28 +132,33 @@ public class StatsRepository extends RepositoryBase DSLContext context; - synchronized (this) + try (Connection connection = getDataSource().getConnection()) { - context = DSL.using(getConnection()); - } + context = DSL.using(connection); + - Result> result = context.select(Tables.stats.name, Tables.accountStats.value).from(Tables.accountStats) - .join(Tables.stats) - .on(Tables.stats.id.eq(Tables.accountStats.statId)) - .where(Tables.accountStats.accountId.eq(DSL.select(Tables.accounts.id) - .from(Tables.accounts) - .where(Tables.accounts.name.eq(playerName))) - ).fetch(); - - - if (result.isNotEmpty()) - { - playerStats = new PlayerStats(); - for (Record2 record : result) + Result> result = context.select(Tables.stats.name, Tables.accountStats.value).from(Tables.accountStats) + .join(Tables.stats) + .on(Tables.stats.id.eq(Tables.accountStats.statId)) + .where(Tables.accountStats.accountId.eq(DSL.select(Tables.accounts.id) + .from(Tables.accounts) + .where(Tables.accounts.name.eq(playerName))) + ).fetch(); + + + if (result.isNotEmpty()) { - playerStats.addStat(record.value1(), record.value2()); + playerStats = new PlayerStats(); + for (Record2 record : result) + { + playerStats.addStat(record.value1(), record.value2()); + } } } + catch (Exception exception) + { + exception.printStackTrace(); + } return playerStats; } diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/mail/MailRepository.java b/Plugins/Mineplex.Hub/src/mineplex/hub/mail/MailRepository.java index 59e7e796c..e8c6b8ee5 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/mail/MailRepository.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/mail/MailRepository.java @@ -1,25 +1,29 @@ package mineplex.hub.mail; +import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.util.UUID; import org.bukkit.plugin.java.JavaPlugin; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; +import mineplex.core.database.RepositoryPoolBase; import mineplex.database.Tables; import mineplex.database.tables.records.MailRecord; + import org.jooq.DSLContext; import org.jooq.Result; import org.jooq.impl.DSL; -public class MailRepository extends RepositoryBase +public class MailRepository extends RepositoryPoolBase { private MailManager _manager; public MailRepository(JavaPlugin plugin, MailManager manager) { - super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); + super(plugin, DBPool.ACCOUNT); _manager = manager; } @@ -48,16 +52,23 @@ public class MailRepository extends RepositoryBase { PlayerMailData data = new PlayerMailData(); - DSLContext context = DSL.using(getConnection()); - - Result resultSet = context.selectFrom(Tables.mail).where(Tables.mail.accountId.eq(DSL.select(Tables.accounts.id) - .from(Tables.accounts) - .where(Tables.accounts.uuid.eq(uuid.toString()))) - ).and(Tables.mail.deleted.isFalse()).fetch(); - - for (MailRecord record : resultSet) + try (Connection connection = getDataSource().getConnection()) { - data.getMessages().add(createMessage(record)); + DSLContext context = DSL.using(connection); + + Result resultSet = context.selectFrom(Tables.mail).where(Tables.mail.accountId.eq(DSL.select(Tables.accounts.id) + .from(Tables.accounts) + .where(Tables.accounts.uuid.eq(uuid.toString()))) + ).and(Tables.mail.deleted.isFalse()).fetch(); + + for (MailRecord record : resultSet) + { + data.getMessages().add(createMessage(record)); + } + } + catch (Exception ex) + { + ex.printStackTrace(); } return data; @@ -70,9 +81,18 @@ public class MailRepository extends RepositoryBase public boolean archive(MailMessage mailMessage) { - DSLContext context = DSL.using(getConnection()); + int recordsUpdated = 0; + + try (Connection connection = getDataSource().getConnection()) + { + DSLContext context = DSL.using(connection); - int recordsUpdated = context.update(Tables.mail).set(Tables.mail.archived, (byte) 1).where(Tables.mail.id.eq(mailMessage.getMessageId())).execute(); + recordsUpdated = context.update(Tables.mail).set(Tables.mail.archived, (byte) 1).where(Tables.mail.id.eq(mailMessage.getMessageId())).execute(); + } + catch (Exception exception) + { + exception.printStackTrace(); + } return recordsUpdated == 1; } diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/poll/PollRepository.java b/Plugins/Mineplex.Hub/src/mineplex/hub/poll/PollRepository.java index c6cb8cd4a..aa3cbf82e 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/poll/PollRepository.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/poll/PollRepository.java @@ -8,7 +8,9 @@ import java.util.UUID; import org.bukkit.plugin.java.JavaPlugin; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; +import mineplex.core.database.RepositoryPoolBase; import mineplex.core.database.ResultSetCallable; import mineplex.core.database.column.ColumnInt; import mineplex.core.database.column.ColumnVarChar; @@ -16,7 +18,7 @@ import mineplex.core.database.column.ColumnVarChar; /** * Created by Shaun on 8/16/2014. */ -public class PollRepository extends RepositoryBase +public class PollRepository extends RepositoryPoolBase { private static String CREATE_POLL_TABLE = "CREATE TABLE IF NOT EXISTS polls (id INT NOT NULL AUTO_INCREMENT, enabled BIT(1), question VARCHAR(256) NOT NULL, answerA VARCHAR(256) NOT NULL, answerB VARCHAR(256), answerC VARCHAR(256), answerD VARCHAR(256), coinReward INT NOT NULL, PRIMARY KEY (id));"; private static String CREATE_RELATION_TABLE = "CREATE TABLE IF NOT EXISTS accountPolls (id INT NOT NULL AUTO_INCREMENT, accountId INT NOT NULL, pollId INT NOT NULL, value TINYINT(1) NOT NULL, PRIMARY KEY (id), FOREIGN KEY (accountId) REFERENCES accounts(id), FOREIGN KEY (pollId) REFERENCES polls(id), UNIQUE INDEX accountPollIndex (accountId, pollId));"; @@ -28,7 +30,7 @@ public class PollRepository extends RepositoryBase public PollRepository(JavaPlugin plugin) { - super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); + super(plugin, DBPool.ACCOUNT); } @Override diff --git a/Plugins/Mineplex.StaffServer/src/mineplex/staffServer/password/PasswordRepository.java b/Plugins/Mineplex.StaffServer/src/mineplex/staffServer/password/PasswordRepository.java index 796bde45d..d0bbd42ce 100644 --- a/Plugins/Mineplex.StaffServer/src/mineplex/staffServer/password/PasswordRepository.java +++ b/Plugins/Mineplex.StaffServer/src/mineplex/staffServer/password/PasswordRepository.java @@ -7,11 +7,13 @@ import java.util.List; import org.bukkit.plugin.java.JavaPlugin; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; +import mineplex.core.database.RepositoryPoolBase; import mineplex.core.database.ResultSetCallable; import mineplex.core.database.column.ColumnVarChar; -public class PasswordRepository extends RepositoryBase +public class PasswordRepository extends RepositoryPoolBase { private static String CREATE_SERVER_PASSWORD_TABLE = "CREATE TABLE IF NOT EXISTS serverPassword (id INT NOT NULL AUTO_INCREMENT, server VARCHAR(100), password VARCHAR(100), PRIMARY KEY (id));"; private static String RETRIEVE_SERVER_PASSWORD = "SELECT password FROM serverPassword WHERE server = ?;"; @@ -23,7 +25,7 @@ public class PasswordRepository extends RepositoryBase public PasswordRepository(JavaPlugin plugin, String serverName) { - super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); + super(plugin, DBPool.ACCOUNT); _serverName = serverName; }