From adfe5fb74e3ec0a8bcdb72601d12bea000b5b1bc Mon Sep 17 00:00:00 2001 From: Ty Sayers Date: Sat, 7 Mar 2015 19:36:54 -0500 Subject: [PATCH 1/3] Implement usage of SQL connection pooling in RepositoryBase to prevent unnecessary idle connections. --- .../account/repository/AccountRepository.java | 3 +- .../benefit/BenefitManagerRepository.java | 3 +- .../core/database/RepositoryBase.java | 250 ++++-------------- .../repository/DonationRepository.java | 3 +- .../inventory/data/InventoryRepository.java | 3 +- .../leaderboard/StatEventsRepository.java | 3 +- .../core/report/ReportRepository.java | 3 +- .../mineplex/core/spawn/SpawnRepository.java | 4 +- .../mineplex/core/stats/StatsRepository.java | 9 +- .../enjinTranslator/TempRepository.java | 3 +- .../fields/repository/FieldRepository.java | 3 +- .../src/mineplex/hub/mail/MailRepository.java | 6 +- .../password/PasswordRepository.java | 4 +- 13 files changed, 84 insertions(+), 213 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/account/repository/AccountRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/account/repository/AccountRepository.java index ae3ce09c2..24b3b6635 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/account/repository/AccountRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/account/repository/AccountRepository.java @@ -19,6 +19,7 @@ import mineplex.core.common.Rank; import mineplex.core.common.util.Callback; import mineplex.core.common.util.NautHashMap; import mineplex.core.common.util.UUIDFetcher; +import mineplex.core.database.DBPool; import mineplex.core.database.DatabaseRunnable; import mineplex.core.database.RepositoryBase; import mineplex.core.database.ResultSetCallable; @@ -43,7 +44,7 @@ public class AccountRepository extends RepositoryBase public AccountRepository(JavaPlugin plugin, String webAddress) { - super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10&allowMultiQueries=true", "root", "tAbechAk3wR7tuTh"); + super(plugin, DBPool.ACCOUNT); _webAddress = webAddress; diff --git a/Plugins/Mineplex.Core/src/mineplex/core/benefit/BenefitManagerRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/benefit/BenefitManagerRepository.java index 2fa7140aa..5affc2a0b 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/benefit/BenefitManagerRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/benefit/BenefitManagerRepository.java @@ -3,6 +3,7 @@ package mineplex.core.benefit; import java.sql.ResultSet; import java.sql.SQLException; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.column.ColumnVarChar; @@ -16,7 +17,7 @@ public class BenefitManagerRepository extends RepositoryBase public BenefitManagerRepository(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/database/RepositoryBase.java b/Plugins/Mineplex.Core/src/mineplex/core/database/RepositoryBase.java index 88d3686bf..338945433 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/database/RepositoryBase.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/database/RepositoryBase.java @@ -22,58 +22,29 @@ import org.bukkit.plugin.java.JavaPlugin; public abstract class RepositoryBase implements Listener { - protected static Object _connectionLock = new Object(); - - private Connection _connection = null; - + // Queue for failed processes private static Object _queueLock = new Object(); - private NautHashMap _failedQueue = new NautHashMap(); - private String _connectionString; - private String _userName; - private String _password; - - protected JavaPlugin Plugin; - protected DataSource DataSource; - - public RepositoryBase(JavaPlugin plugin, String connectionString, String username, String password) - { - Plugin = plugin; - - _connectionString = connectionString; - _userName = username; - _password = password; - - Bukkit.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() - { - public void run() - { - synchronized (_connectionLock) - { - initialize(); - update(); - } - } - }); - - plugin.getServer().getPluginManager().registerEvents(this, plugin); - } + private DataSource _dataSource; // Connection pool + protected JavaPlugin Plugin; // Plugin responsible for this repository + /** + * Constructor + * @param plugin - the {@link JavaPlugin} module responsible for this repository. + * @param dataSource - the {@link DataSource} responsible for providing the connection pool to this repository. + */ public RepositoryBase(JavaPlugin plugin, DataSource dataSource) { Plugin = plugin; - DataSource = dataSource; + _dataSource = dataSource; Bukkit.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() { public void run() { - synchronized (_connectionLock) - { - initialize(); - update(); - } + initialize(); + update(); } }); @@ -84,34 +55,39 @@ public abstract class RepositoryBase implements Listener protected abstract void update(); + /** + * @return the {@link DataSource} used by the repository for connection pooling. + */ + protected DataSource getConnectionPool() + { + return _dataSource; + } + + /** + * Requirements: {@link Connection}s must be closed after usage so they may be returned to the pool! + * @see Connection#close() + * @return a newly fetched {@link Connection} from the connection pool, if a connection can be made, null otherwise. + */ protected Connection getConnection() { - return getConnection(false); - } - - protected Connection getConnection(boolean validate) - { - synchronized (_connectionLock) + try { - try - { - if (_connection == null || (validate && !_connection.isValid(2))) - { - if (DataSource != null) - _connection = DataSource.getConnection(); - else - _connection = DriverManager.getConnection(_connectionString, _userName, _password); - } - } - catch (SQLException e) - { - e.printStackTrace(); - } + return _dataSource.getConnection(); + } + catch (SQLException e) + { + e.printStackTrace(); + // TODO: Log connection failures? + return null; } - - return _connection; } + /** + * Execute a query against the repository. + * @param query - the concatenated query to execute in string form. + * @param columns - the column data values used for insertion into the query. + * @return the number of rows affected by this query in the repository. + */ protected int executeUpdate(String query, Column...columns) { return executeInsert(query, null, columns); @@ -119,16 +95,14 @@ public abstract class RepositoryBase implements Listener protected int executeInsert(String query, ResultSetCallable callable, Column...columns) { - PreparedStatement preparedStatement = null; - int affectedRows = 0; - try + // Automatic resource management for handling/closing objects. + try ( + Connection connection = getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS) + ) { - getConnection(false); - - preparedStatement = _connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); - for (int i=0; i < columns.length; i++) { columns[i].setValue(preparedStatement, i+1); @@ -137,49 +111,24 @@ public abstract class RepositoryBase implements Listener affectedRows = preparedStatement.executeUpdate(); if (callable != null) + { callable.processResultSet(preparedStatement.getGeneratedKeys()); + } } catch (SQLException exception) { - getConnection(true); executeInsert(query, callable, columns); } catch (Exception exception) { exception.printStackTrace(); } - finally - { - if (preparedStatement != null) - { - try - { - preparedStatement.close(); - } - catch (SQLException e) - { - e.printStackTrace(); - } - } - - try - { - if (DataSource != null) - _connection.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++) @@ -187,118 +136,41 @@ public abstract class RepositoryBase implements Listener columns[i].setValue(statement, i+1); } - resultSet = statement.executeQuery(); - - callable.processResultSet(resultSet); + try (ResultSet resultSet = statement.executeQuery()) + { + callable.processResultSet(resultSet); + } } catch (SQLException exception) { - getConnection(true); executeQuery(statement, callable, columns); } catch (Exception exception) { exception.printStackTrace(); } - finally - { - if (resultSet != null) - { - try - { - resultSet.close(); - } - catch (SQLException e) - { - e.printStackTrace(); - } - } - - try - { - if (DataSource != null) - _connection.close(); - } - catch (SQLException e) - { - e.printStackTrace(); - } - } } protected void executeQuery(String query, ResultSetCallable callable, Column...columns) { - PreparedStatement preparedStatement = null; - - try + // Automatic resource management for handling/closing objects. + try ( + Connection connection = getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(query) + ) { - getConnection(false); - - preparedStatement = _connection.prepareStatement(query); - executeQuery(preparedStatement, callable, columns); } catch (SQLException exception) { - getConnection(true); executeQuery(query, callable, columns); } catch (Exception exception) { exception.printStackTrace(); } - finally - { - if (preparedStatement != null) - { - try - { - preparedStatement.close(); - } - catch (SQLException e) - { - e.printStackTrace(); - } - } - - try - { - if (DataSource != null) - _connection.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 (SQLException exception) - { - getConnection(true); - executeUpdate(preparedStatement, columns); - } - catch (Exception exception) - { - exception.printStackTrace(); - } - - return affectedRows; - } - protected void handleDatabaseCall(final DatabaseRunnable databaseRunnable, final String errorMessage) { Thread asyncThread = new Thread(new Runnable() @@ -344,19 +216,7 @@ public abstract class RepositoryBase implements Listener @EventHandler public void validateConnection(UpdateEvent event) { - if (event.getType() != UpdateType.MIN_02 || DataSource != null) - return; - - Bukkit.getScheduler().runTaskAsynchronously(Plugin, new Runnable() - { - public void run() - { - synchronized (_connectionLock) - { - getConnection(true); - } - } - }); + // Utilizes DataSource connection pools, so there is no connection to validate. } private void processFailedQueue() diff --git a/Plugins/Mineplex.Core/src/mineplex/core/donation/repository/DonationRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/donation/repository/DonationRepository.java index 818c95df9..b7246e8be 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/donation/repository/DonationRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/donation/repository/DonationRepository.java @@ -8,6 +8,7 @@ import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; import mineplex.core.common.util.Callback; +import mineplex.core.database.DBPool; import mineplex.core.database.DatabaseRunnable; import mineplex.core.database.RepositoryBase; import mineplex.core.database.column.ColumnInt; @@ -33,7 +34,7 @@ public class DonationRepository extends RepositoryBase public DonationRepository(JavaPlugin plugin, String webAddress) { - super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); + super(plugin, DBPool.ACCOUNT); _webAddress = webAddress; } 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..444e98bbf 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/inventory/data/InventoryRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/inventory/data/InventoryRepository.java @@ -7,6 +7,7 @@ import java.util.List; import org.bukkit.plugin.java.JavaPlugin; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.ResultSetCallable; import mineplex.core.database.column.ColumnInt; @@ -31,7 +32,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/leaderboard/StatEventsRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/leaderboard/StatEventsRepository.java index 2e0888f1e..1679bc36d 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/leaderboard/StatEventsRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/leaderboard/StatEventsRepository.java @@ -1,5 +1,6 @@ package mineplex.core.leaderboard; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.column.ColumnInt; import mineplex.core.database.column.ColumnVarChar; @@ -35,7 +36,7 @@ public class StatEventsRepository extends RepositoryBase */ public StatEventsRepository(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/report/ReportRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/report/ReportRepository.java index 968010e9c..4a9f91215 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/report/ReportRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/report/ReportRepository.java @@ -7,6 +7,7 @@ import java.util.UUID; import java.util.Map.Entry; import mineplex.core.common.util.NautHashMap; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.ResultSetCallable; import mineplex.core.database.column.ColumnInt; @@ -40,7 +41,7 @@ This will be used to determine if staff are handling public ReportRepository(JavaPlugin plugin, String connectionString) { - super(plugin, connectionString, "root", "tAbechAk3wR7tuTh"); // TODO: Config file for host/pass? + super(plugin, DBPool.ACCOUNT); } @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..eb85bae9d 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/spawn/SpawnRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/spawn/SpawnRepository.java @@ -7,6 +7,7 @@ import java.util.List; import org.bukkit.plugin.java.JavaPlugin; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.ResultSetCallable; import mineplex.core.database.column.ColumnVarChar; @@ -22,8 +23,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/StatsRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/stats/StatsRepository.java index 827b3605c..58c81c9f8 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/stats/StatsRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/stats/StatsRepository.java @@ -9,6 +9,7 @@ import java.util.List; 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.ResultSetCallable; import mineplex.core.database.column.ColumnVarChar; @@ -18,6 +19,7 @@ import org.jooq.DSLContext; import org.jooq.Insert; import org.jooq.Record2; import org.jooq.Result; +import org.jooq.SQLDialect; import org.jooq.Update; import org.jooq.impl.DSL; @@ -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,8 +81,7 @@ public class StatsRepository extends RepositoryBase { try { - - DSLContext context = DSL.using(getConnection(true)); + DSLContext context = DSL.using(getConnectionPool(), SQLDialect.MYSQL); List updates = new ArrayList<>(); List inserts = new ArrayList<>(); @@ -133,7 +134,7 @@ public class StatsRepository extends RepositoryBase synchronized (this) { - context = DSL.using(getConnection()); + context = DSL.using(getConnectionPool(), SQLDialect.MYSQL); } Result> result = context.select(Tables.stats.name, Tables.accountStats.value).from(Tables.accountStats) diff --git a/Plugins/Mineplex.EnjinTranslator/src/mineplex/enjinTranslator/TempRepository.java b/Plugins/Mineplex.EnjinTranslator/src/mineplex/enjinTranslator/TempRepository.java index e052ee72e..ba4cd2729 100644 --- a/Plugins/Mineplex.EnjinTranslator/src/mineplex/enjinTranslator/TempRepository.java +++ b/Plugins/Mineplex.EnjinTranslator/src/mineplex/enjinTranslator/TempRepository.java @@ -2,6 +2,7 @@ package mineplex.enjinTranslator; import org.bukkit.plugin.java.JavaPlugin; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.column.ColumnInt; import mineplex.core.database.column.ColumnVarChar; @@ -12,7 +13,7 @@ public class TempRepository extends RepositoryBase public TempRepository(JavaPlugin plugin) { - super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); + super(plugin, DBPool.ACCOUNT); } public void addGemBooster(String name, int amount) diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/fields/repository/FieldRepository.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/fields/repository/FieldRepository.java index cad9425ef..f5be7a077 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/fields/repository/FieldRepository.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/fields/repository/FieldRepository.java @@ -7,6 +7,7 @@ import java.util.List; import org.bukkit.plugin.java.JavaPlugin; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.ResultSetCallable; import mineplex.core.database.column.ColumnByte; @@ -31,7 +32,7 @@ public class FieldRepository extends RepositoryBase public FieldRepository(JavaPlugin plugin) { - super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); + super(plugin, DBPool.ACCOUNT); } public List getFieldBlocks(String server) diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/mail/MailRepository.java b/Plugins/Mineplex.Hub/src/mineplex/hub/mail/MailRepository.java index 59e7e796c..af43ab9f5 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/mail/MailRepository.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/mail/MailRepository.java @@ -6,9 +6,11 @@ import java.util.UUID; import org.bukkit.plugin.java.JavaPlugin; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.database.Tables; import mineplex.database.tables.records.MailRecord; + import org.jooq.DSLContext; import org.jooq.Result; import org.jooq.impl.DSL; @@ -19,8 +21,8 @@ public class MailRepository extends RepositoryBase 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; } diff --git a/Plugins/Mineplex.StaffServer/src/mineplex/staffServer/password/PasswordRepository.java b/Plugins/Mineplex.StaffServer/src/mineplex/staffServer/password/PasswordRepository.java index 796bde45d..20cb041e0 100644 --- a/Plugins/Mineplex.StaffServer/src/mineplex/staffServer/password/PasswordRepository.java +++ b/Plugins/Mineplex.StaffServer/src/mineplex/staffServer/password/PasswordRepository.java @@ -7,6 +7,7 @@ import java.util.List; import org.bukkit.plugin.java.JavaPlugin; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.ResultSetCallable; import mineplex.core.database.column.ColumnVarChar; @@ -23,8 +24,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; } From 215ec7b00dff89b0fbb2646de67531d6fe4406d2 Mon Sep 17 00:00:00 2001 From: Ty Sayers Date: Sat, 7 Mar 2015 19:38:06 -0500 Subject: [PATCH 2/3] Revert "Implement usage of SQL connection pooling in RepositoryBase to prevent unnecessary idle connections." This reverts commit adfe5fb74e3ec0a8bcdb72601d12bea000b5b1bc. --- .../account/repository/AccountRepository.java | 3 +- .../benefit/BenefitManagerRepository.java | 3 +- .../core/database/RepositoryBase.java | 326 +++++++++++++----- .../repository/DonationRepository.java | 3 +- .../inventory/data/InventoryRepository.java | 3 +- .../leaderboard/StatEventsRepository.java | 3 +- .../core/report/ReportRepository.java | 3 +- .../mineplex/core/spawn/SpawnRepository.java | 4 +- .../mineplex/core/stats/StatsRepository.java | 9 +- .../enjinTranslator/TempRepository.java | 3 +- .../fields/repository/FieldRepository.java | 3 +- .../src/mineplex/hub/mail/MailRepository.java | 6 +- .../password/PasswordRepository.java | 4 +- 13 files changed, 251 insertions(+), 122 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/account/repository/AccountRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/account/repository/AccountRepository.java index 24b3b6635..ae3ce09c2 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/account/repository/AccountRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/account/repository/AccountRepository.java @@ -19,7 +19,6 @@ import mineplex.core.common.Rank; import mineplex.core.common.util.Callback; import mineplex.core.common.util.NautHashMap; import mineplex.core.common.util.UUIDFetcher; -import mineplex.core.database.DBPool; import mineplex.core.database.DatabaseRunnable; import mineplex.core.database.RepositoryBase; import mineplex.core.database.ResultSetCallable; @@ -44,7 +43,7 @@ public class AccountRepository extends RepositoryBase public AccountRepository(JavaPlugin plugin, String webAddress) { - super(plugin, DBPool.ACCOUNT); + super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10&allowMultiQueries=true", "root", "tAbechAk3wR7tuTh"); _webAddress = webAddress; diff --git a/Plugins/Mineplex.Core/src/mineplex/core/benefit/BenefitManagerRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/benefit/BenefitManagerRepository.java index 5affc2a0b..2fa7140aa 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/benefit/BenefitManagerRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/benefit/BenefitManagerRepository.java @@ -3,7 +3,6 @@ package mineplex.core.benefit; import java.sql.ResultSet; import java.sql.SQLException; -import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.column.ColumnVarChar; @@ -17,7 +16,7 @@ public class BenefitManagerRepository extends RepositoryBase public BenefitManagerRepository(JavaPlugin plugin) { - super(plugin, DBPool.ACCOUNT); + super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); } @Override diff --git a/Plugins/Mineplex.Core/src/mineplex/core/database/RepositoryBase.java b/Plugins/Mineplex.Core/src/mineplex/core/database/RepositoryBase.java index 338945433..88d3686bf 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/database/RepositoryBase.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/database/RepositoryBase.java @@ -22,29 +22,58 @@ import org.bukkit.plugin.java.JavaPlugin; public abstract class RepositoryBase implements Listener { - // Queue for failed processes + protected static Object _connectionLock = new Object(); + + private Connection _connection = null; + private static Object _queueLock = new Object(); + private NautHashMap _failedQueue = new NautHashMap(); - private DataSource _dataSource; // Connection pool - protected JavaPlugin Plugin; // Plugin responsible for this repository + private String _connectionString; + private String _userName; + private String _password; - /** - * Constructor - * @param plugin - the {@link JavaPlugin} module responsible for this repository. - * @param dataSource - the {@link DataSource} responsible for providing the connection pool to this repository. - */ - public RepositoryBase(JavaPlugin plugin, DataSource dataSource) + protected JavaPlugin Plugin; + protected DataSource DataSource; + + public RepositoryBase(JavaPlugin plugin, String connectionString, String username, String password) { Plugin = plugin; - _dataSource = dataSource; + + _connectionString = connectionString; + _userName = username; + _password = password; Bukkit.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() { public void run() { - initialize(); - update(); + synchronized (_connectionLock) + { + initialize(); + update(); + } + } + }); + + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + public RepositoryBase(JavaPlugin plugin, DataSource dataSource) + { + Plugin = plugin; + DataSource = dataSource; + + Bukkit.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() + { + public void run() + { + synchronized (_connectionLock) + { + initialize(); + update(); + } } }); @@ -55,39 +84,34 @@ public abstract class RepositoryBase implements Listener protected abstract void update(); - /** - * @return the {@link DataSource} used by the repository for connection pooling. - */ - protected DataSource getConnectionPool() - { - return _dataSource; - } - - /** - * Requirements: {@link Connection}s must be closed after usage so they may be returned to the pool! - * @see Connection#close() - * @return a newly fetched {@link Connection} from the connection pool, if a connection can be made, null otherwise. - */ protected Connection getConnection() { - try - { - return _dataSource.getConnection(); - } - catch (SQLException e) - { - e.printStackTrace(); - // TODO: Log connection failures? - return null; - } + return getConnection(false); + } + + protected Connection getConnection(boolean validate) + { + synchronized (_connectionLock) + { + try + { + if (_connection == null || (validate && !_connection.isValid(2))) + { + if (DataSource != null) + _connection = DataSource.getConnection(); + else + _connection = DriverManager.getConnection(_connectionString, _userName, _password); + } + } + catch (SQLException e) + { + e.printStackTrace(); + } + } + + return _connection; } - /** - * Execute a query against the repository. - * @param query - the concatenated query to execute in string form. - * @param columns - the column data values used for insertion into the query. - * @return the number of rows affected by this query in the repository. - */ protected int executeUpdate(String query, Column...columns) { return executeInsert(query, null, columns); @@ -95,14 +119,16 @@ public abstract class RepositoryBase implements Listener protected int executeInsert(String query, ResultSetCallable callable, Column...columns) { + PreparedStatement preparedStatement = null; + int affectedRows = 0; - // Automatic resource management for handling/closing objects. - try ( - Connection connection = getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS) - ) + try { + getConnection(false); + + preparedStatement = _connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); + for (int i=0; i < columns.length; i++) { columns[i].setValue(preparedStatement, i+1); @@ -111,66 +137,168 @@ public abstract class RepositoryBase implements Listener affectedRows = preparedStatement.executeUpdate(); if (callable != null) - { callable.processResultSet(preparedStatement.getGeneratedKeys()); - } } catch (SQLException exception) { + getConnection(true); executeInsert(query, callable, columns); } catch (Exception exception) { exception.printStackTrace(); } + finally + { + if (preparedStatement != null) + { + try + { + preparedStatement.close(); + } + catch (SQLException e) + { + e.printStackTrace(); + } + } + + try + { + if (DataSource != null) + _connection.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 (SQLException exception) + { + getConnection(true); + executeQuery(statement, callable, columns); + } + catch (Exception exception) + { + exception.printStackTrace(); + } + finally + { + if (resultSet != null) + { + try + { + resultSet.close(); + } + catch (SQLException e) + { + e.printStackTrace(); + } + } + + try + { + if (DataSource != null) + _connection.close(); + } + catch (SQLException e) + { + e.printStackTrace(); + } + } + } + + protected void executeQuery(String query, ResultSetCallable callable, Column...columns) + { + PreparedStatement preparedStatement = null; + + try + { + getConnection(false); + + preparedStatement = _connection.prepareStatement(query); + + executeQuery(preparedStatement, callable, columns); + } + catch (SQLException exception) + { + getConnection(true); + executeQuery(query, callable, columns); + } + catch (Exception exception) + { + exception.printStackTrace(); + } + finally + { + if (preparedStatement != null) + { + try + { + preparedStatement.close(); + } + catch (SQLException e) + { + e.printStackTrace(); + } + } + + try + { + if (DataSource != null) + _connection.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 (SQLException exception) + { + getConnection(true); + executeUpdate(preparedStatement, columns); + } + catch (Exception exception) + { + exception.printStackTrace(); + } return affectedRows; } - protected void executeQuery(PreparedStatement statement, ResultSetCallable callable, Column...columns) - { - try - { - for (int i=0; i < columns.length; i++) - { - columns[i].setValue(statement, i+1); - } - - try (ResultSet resultSet = statement.executeQuery()) - { - callable.processResultSet(resultSet); - } - } - catch (SQLException exception) - { - executeQuery(statement, callable, columns); - } - catch (Exception exception) - { - exception.printStackTrace(); - } - } - - protected void executeQuery(String query, ResultSetCallable callable, Column...columns) - { - // Automatic resource management for handling/closing objects. - try ( - Connection connection = getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(query) - ) - { - executeQuery(preparedStatement, callable, columns); - } - catch (SQLException exception) - { - executeQuery(query, callable, columns); - } - catch (Exception exception) - { - exception.printStackTrace(); - } - } - protected void handleDatabaseCall(final DatabaseRunnable databaseRunnable, final String errorMessage) { Thread asyncThread = new Thread(new Runnable() @@ -216,7 +344,19 @@ public abstract class RepositoryBase implements Listener @EventHandler public void validateConnection(UpdateEvent event) { - // Utilizes DataSource connection pools, so there is no connection to validate. + if (event.getType() != UpdateType.MIN_02 || DataSource != null) + return; + + Bukkit.getScheduler().runTaskAsynchronously(Plugin, new Runnable() + { + public void run() + { + synchronized (_connectionLock) + { + getConnection(true); + } + } + }); } private void processFailedQueue() diff --git a/Plugins/Mineplex.Core/src/mineplex/core/donation/repository/DonationRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/donation/repository/DonationRepository.java index b7246e8be..818c95df9 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/donation/repository/DonationRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/donation/repository/DonationRepository.java @@ -8,7 +8,6 @@ import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; import mineplex.core.common.util.Callback; -import mineplex.core.database.DBPool; import mineplex.core.database.DatabaseRunnable; import mineplex.core.database.RepositoryBase; import mineplex.core.database.column.ColumnInt; @@ -34,7 +33,7 @@ public class DonationRepository extends RepositoryBase public DonationRepository(JavaPlugin plugin, String webAddress) { - super(plugin, DBPool.ACCOUNT); + super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); _webAddress = webAddress; } 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 444e98bbf..2a1cb7f27 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/inventory/data/InventoryRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/inventory/data/InventoryRepository.java @@ -7,7 +7,6 @@ import java.util.List; import org.bukkit.plugin.java.JavaPlugin; -import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.ResultSetCallable; import mineplex.core.database.column.ColumnInt; @@ -32,7 +31,7 @@ public class InventoryRepository extends RepositoryBase public InventoryRepository(JavaPlugin plugin) { - super(plugin, DBPool.ACCOUNT); + super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); } @Override diff --git a/Plugins/Mineplex.Core/src/mineplex/core/leaderboard/StatEventsRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/leaderboard/StatEventsRepository.java index 1679bc36d..2e0888f1e 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/leaderboard/StatEventsRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/leaderboard/StatEventsRepository.java @@ -1,6 +1,5 @@ package mineplex.core.leaderboard; -import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.column.ColumnInt; import mineplex.core.database.column.ColumnVarChar; @@ -36,7 +35,7 @@ public class StatEventsRepository extends RepositoryBase */ public StatEventsRepository(JavaPlugin plugin) { - super(plugin, DBPool.ACCOUNT); + super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); } @Override diff --git a/Plugins/Mineplex.Core/src/mineplex/core/report/ReportRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/report/ReportRepository.java index 4a9f91215..968010e9c 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/report/ReportRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/report/ReportRepository.java @@ -7,7 +7,6 @@ import java.util.UUID; import java.util.Map.Entry; import mineplex.core.common.util.NautHashMap; -import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.ResultSetCallable; import mineplex.core.database.column.ColumnInt; @@ -41,7 +40,7 @@ This will be used to determine if staff are handling public ReportRepository(JavaPlugin plugin, String connectionString) { - super(plugin, DBPool.ACCOUNT); + super(plugin, connectionString, "root", "tAbechAk3wR7tuTh"); // 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 eb85bae9d..69a5e3b64 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/spawn/SpawnRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/spawn/SpawnRepository.java @@ -7,7 +7,6 @@ import java.util.List; import org.bukkit.plugin.java.JavaPlugin; -import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.ResultSetCallable; import mineplex.core.database.column.ColumnVarChar; @@ -23,7 +22,8 @@ public class SpawnRepository extends RepositoryBase public SpawnRepository(JavaPlugin plugin, String serverName) { - super(plugin, DBPool.ACCOUNT); + super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); + _serverName = serverName; } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/stats/StatsRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/stats/StatsRepository.java index 58c81c9f8..827b3605c 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/stats/StatsRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/stats/StatsRepository.java @@ -9,7 +9,6 @@ import java.util.List; 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.ResultSetCallable; import mineplex.core.database.column.ColumnVarChar; @@ -19,7 +18,6 @@ import org.jooq.DSLContext; import org.jooq.Insert; import org.jooq.Record2; import org.jooq.Result; -import org.jooq.SQLDialect; import org.jooq.Update; import org.jooq.impl.DSL; @@ -33,7 +31,7 @@ public class StatsRepository extends RepositoryBase public StatsRepository(JavaPlugin plugin) { - super(plugin, DBPool.ACCOUNT); + super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); } @Override @@ -81,7 +79,8 @@ public class StatsRepository extends RepositoryBase { try { - DSLContext context = DSL.using(getConnectionPool(), SQLDialect.MYSQL); + + DSLContext context = DSL.using(getConnection(true)); List updates = new ArrayList<>(); List inserts = new ArrayList<>(); @@ -134,7 +133,7 @@ public class StatsRepository extends RepositoryBase synchronized (this) { - context = DSL.using(getConnectionPool(), SQLDialect.MYSQL); + context = DSL.using(getConnection()); } Result> result = context.select(Tables.stats.name, Tables.accountStats.value).from(Tables.accountStats) diff --git a/Plugins/Mineplex.EnjinTranslator/src/mineplex/enjinTranslator/TempRepository.java b/Plugins/Mineplex.EnjinTranslator/src/mineplex/enjinTranslator/TempRepository.java index ba4cd2729..e052ee72e 100644 --- a/Plugins/Mineplex.EnjinTranslator/src/mineplex/enjinTranslator/TempRepository.java +++ b/Plugins/Mineplex.EnjinTranslator/src/mineplex/enjinTranslator/TempRepository.java @@ -2,7 +2,6 @@ package mineplex.enjinTranslator; import org.bukkit.plugin.java.JavaPlugin; -import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.column.ColumnInt; import mineplex.core.database.column.ColumnVarChar; @@ -13,7 +12,7 @@ public class TempRepository extends RepositoryBase public TempRepository(JavaPlugin plugin) { - super(plugin, DBPool.ACCOUNT); + super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); } public void addGemBooster(String name, int amount) diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/fields/repository/FieldRepository.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/fields/repository/FieldRepository.java index f5be7a077..cad9425ef 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/fields/repository/FieldRepository.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/fields/repository/FieldRepository.java @@ -7,7 +7,6 @@ import java.util.List; import org.bukkit.plugin.java.JavaPlugin; -import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.ResultSetCallable; import mineplex.core.database.column.ColumnByte; @@ -32,7 +31,7 @@ public class FieldRepository extends RepositoryBase public FieldRepository(JavaPlugin plugin) { - super(plugin, DBPool.ACCOUNT); + super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); } public List getFieldBlocks(String server) diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/mail/MailRepository.java b/Plugins/Mineplex.Hub/src/mineplex/hub/mail/MailRepository.java index af43ab9f5..59e7e796c 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/mail/MailRepository.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/mail/MailRepository.java @@ -6,11 +6,9 @@ import java.util.UUID; import org.bukkit.plugin.java.JavaPlugin; -import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.database.Tables; import mineplex.database.tables.records.MailRecord; - import org.jooq.DSLContext; import org.jooq.Result; import org.jooq.impl.DSL; @@ -21,8 +19,8 @@ public class MailRepository extends RepositoryBase public MailRepository(JavaPlugin plugin, MailManager manager) { - super(plugin, DBPool.ACCOUNT); - + super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); + _manager = manager; } diff --git a/Plugins/Mineplex.StaffServer/src/mineplex/staffServer/password/PasswordRepository.java b/Plugins/Mineplex.StaffServer/src/mineplex/staffServer/password/PasswordRepository.java index 20cb041e0..796bde45d 100644 --- a/Plugins/Mineplex.StaffServer/src/mineplex/staffServer/password/PasswordRepository.java +++ b/Plugins/Mineplex.StaffServer/src/mineplex/staffServer/password/PasswordRepository.java @@ -7,7 +7,6 @@ import java.util.List; import org.bukkit.plugin.java.JavaPlugin; -import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.ResultSetCallable; import mineplex.core.database.column.ColumnVarChar; @@ -24,7 +23,8 @@ public class PasswordRepository extends RepositoryBase public PasswordRepository(JavaPlugin plugin, String serverName) { - super(plugin, DBPool.ACCOUNT); + super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); + _serverName = serverName; } From 626bfc35fc4dc462618239520a51d888da98b932 Mon Sep 17 00:00:00 2001 From: Jonathan Williams Date: Wed, 11 Mar 2015 18:05:13 -0700 Subject: [PATCH 3/3] Revert "Revert "Implement usage of SQL connection pooling in RepositoryBase to prevent unnecessary idle connections."" This reverts commit 215ec7b00dff89b0fbb2646de67531d6fe4406d2. Enabled multi-queries on db pool. Cleaned up some code using new pooling getConnection inappropriately --- .../account/repository/AccountRepository.java | 65 +---- .../benefit/BenefitManagerRepository.java | 3 +- .../src/mineplex/core/database/DBPool.java | 5 +- .../core/database/RepositoryBase.java | 255 ++++-------------- .../repository/DonationRepository.java | 3 +- .../inventory/data/InventoryRepository.java | 3 +- .../leaderboard/StatEventsRepository.java | 3 +- .../preferences/PreferencesRepository.java | 27 +- .../core/report/ReportRepository.java | 3 +- .../mineplex/core/spawn/SpawnRepository.java | 4 +- .../mineplex/core/stats/StatsRepository.java | 9 +- .../enjinTranslator/TempRepository.java | 3 +- .../fields/repository/FieldRepository.java | 3 +- .../src/mineplex/hub/mail/MailRepository.java | 6 +- .../password/PasswordRepository.java | 4 +- 15 files changed, 98 insertions(+), 298 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/account/repository/AccountRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/account/repository/AccountRepository.java index ae3ce09c2..602e2fd62 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/account/repository/AccountRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/account/repository/AccountRepository.java @@ -1,5 +1,6 @@ package mineplex.core.account.repository; +import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @@ -19,6 +20,7 @@ import mineplex.core.common.Rank; import mineplex.core.common.util.Callback; import mineplex.core.common.util.NautHashMap; import mineplex.core.common.util.UUIDFetcher; +import mineplex.core.database.DBPool; import mineplex.core.database.DatabaseRunnable; import mineplex.core.database.RepositoryBase; import mineplex.core.database.ResultSetCallable; @@ -43,34 +45,9 @@ public class AccountRepository extends RepositoryBase public AccountRepository(JavaPlugin plugin, String webAddress) { - super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10&allowMultiQueries=true", "root", "tAbechAk3wR7tuTh"); + super(plugin, DBPool.ACCOUNT); _webAddress = webAddress; - - Statement statement = null; - - try - { - statement = getConnection().createStatement(); - } - catch (Exception exception) - { - exception.printStackTrace(); - } - finally - { - if (statement != null) - { - try - { - statement.close(); - } - catch (SQLException e) - { - e.printStackTrace(); - } - } - } } @Override @@ -81,13 +58,11 @@ public class AccountRepository extends RepositoryBase public void login(NautHashMap loginProcessors, String uuid, String name) { - Statement statement = null; - ResultSet resultSet = null; - - try + try ( + Connection connection = getConnection(); + Statement statement = connection.createStatement() + ) { - statement = getConnection().createStatement(); - /* boolean statementStatus = statement.execute( "UPDATE accounts SET name='" + name + "', lastLogin=now() WHERE accounts.uuid = '" + uuid + "';" @@ -152,32 +127,6 @@ public class AccountRepository extends RepositoryBase { exception.printStackTrace(); } - finally - { - if (statement != null) - { - try - { - statement.close(); - } - catch (SQLException e) - { - e.printStackTrace(); - } - } - - if (resultSet != null) - { - try - { - resultSet.close(); - } - catch (SQLException e) - { - e.printStackTrace(); - } - } - } } public String GetClient(String name, UUID uuid, String ipAddress) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/benefit/BenefitManagerRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/benefit/BenefitManagerRepository.java index 2fa7140aa..5affc2a0b 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/benefit/BenefitManagerRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/benefit/BenefitManagerRepository.java @@ -3,6 +3,7 @@ package mineplex.core.benefit; import java.sql.ResultSet; import java.sql.SQLException; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.column.ColumnVarChar; @@ -16,7 +17,7 @@ public class BenefitManagerRepository extends RepositoryBase public BenefitManagerRepository(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/database/DBPool.java b/Plugins/Mineplex.Core/src/mineplex/core/database/DBPool.java index 8dacde695..d7747d086 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/database/DBPool.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/database/DBPool.java @@ -16,6 +16,7 @@ public final class DBPool { BasicDataSource source = new BasicDataSource(); source.addConnectionProperty("autoReconnect", "true"); + source.addConnectionProperty("allowMultiQueries", "true"); source.setDefaultAutoCommit(true); source.setEnableAutoCommitOnReturn(true); source.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); @@ -23,8 +24,8 @@ public final class DBPool source.setUrl(url); source.setUsername(username); source.setPassword(password); - source.setMaxTotal(2); - source.setMaxIdle(2); + source.setMaxTotal(3); + source.setMaxIdle(3); 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 88d3686bf..eab9840f2 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/database/RepositoryBase.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/database/RepositoryBase.java @@ -1,7 +1,6 @@ package mineplex.core.database; import java.sql.Connection; -import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -22,58 +21,29 @@ import org.bukkit.plugin.java.JavaPlugin; public abstract class RepositoryBase implements Listener { - protected static Object _connectionLock = new Object(); - - private Connection _connection = null; - + // Queue for failed processes private static Object _queueLock = new Object(); - private NautHashMap _failedQueue = new NautHashMap(); - private String _connectionString; - private String _userName; - private String _password; - - protected JavaPlugin Plugin; - protected DataSource DataSource; - - public RepositoryBase(JavaPlugin plugin, String connectionString, String username, String password) - { - Plugin = plugin; - - _connectionString = connectionString; - _userName = username; - _password = password; - - Bukkit.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() - { - public void run() - { - synchronized (_connectionLock) - { - initialize(); - update(); - } - } - }); - - plugin.getServer().getPluginManager().registerEvents(this, plugin); - } + private DataSource _dataSource; // Connection pool + protected JavaPlugin Plugin; // Plugin responsible for this repository + /** + * Constructor + * @param plugin - the {@link JavaPlugin} module responsible for this repository. + * @param dataSource - the {@link DataSource} responsible for providing the connection pool to this repository. + */ public RepositoryBase(JavaPlugin plugin, DataSource dataSource) { Plugin = plugin; - DataSource = dataSource; + _dataSource = dataSource; Bukkit.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() { public void run() { - synchronized (_connectionLock) - { - initialize(); - update(); - } + initialize(); + update(); } }); @@ -84,34 +54,39 @@ public abstract class RepositoryBase implements Listener protected abstract void update(); + /** + * @return the {@link DataSource} used by the repository for connection pooling. + */ + protected DataSource getConnectionPool() + { + return _dataSource; + } + + /** + * Requirements: {@link Connection}s must be closed after usage so they may be returned to the pool! + * @see Connection#close() + * @return a newly fetched {@link Connection} from the connection pool, if a connection can be made, null otherwise. + */ protected Connection getConnection() { - return getConnection(false); - } - - protected Connection getConnection(boolean validate) - { - synchronized (_connectionLock) + try { - try - { - if (_connection == null || (validate && !_connection.isValid(2))) - { - if (DataSource != null) - _connection = DataSource.getConnection(); - else - _connection = DriverManager.getConnection(_connectionString, _userName, _password); - } - } - catch (SQLException e) - { - e.printStackTrace(); - } + return _dataSource.getConnection(); + } + catch (SQLException e) + { + e.printStackTrace(); + // TODO: Log connection failures? + return null; } - - return _connection; } + /** + * Execute a query against the repository. + * @param query - the concatenated query to execute in string form. + * @param columns - the column data values used for insertion into the query. + * @return the number of rows affected by this query in the repository. + */ protected int executeUpdate(String query, Column...columns) { return executeInsert(query, null, columns); @@ -119,16 +94,14 @@ public abstract class RepositoryBase implements Listener protected int executeInsert(String query, ResultSetCallable callable, Column...columns) { - PreparedStatement preparedStatement = null; - int affectedRows = 0; - try + // Automatic resource management for handling/closing objects. + try ( + Connection connection = getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS) + ) { - getConnection(false); - - preparedStatement = _connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); - for (int i=0; i < columns.length; i++) { columns[i].setValue(preparedStatement, i+1); @@ -137,49 +110,24 @@ public abstract class RepositoryBase implements Listener affectedRows = preparedStatement.executeUpdate(); if (callable != null) + { callable.processResultSet(preparedStatement.getGeneratedKeys()); + } } catch (SQLException exception) { - getConnection(true); executeInsert(query, callable, columns); } catch (Exception exception) { exception.printStackTrace(); } - finally - { - if (preparedStatement != null) - { - try - { - preparedStatement.close(); - } - catch (SQLException e) - { - e.printStackTrace(); - } - } - - try - { - if (DataSource != null) - _connection.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++) @@ -187,118 +135,41 @@ public abstract class RepositoryBase implements Listener columns[i].setValue(statement, i+1); } - resultSet = statement.executeQuery(); - - callable.processResultSet(resultSet); + try (ResultSet resultSet = statement.executeQuery()) + { + callable.processResultSet(resultSet); + } } catch (SQLException exception) { - getConnection(true); executeQuery(statement, callable, columns); } catch (Exception exception) { exception.printStackTrace(); } - finally - { - if (resultSet != null) - { - try - { - resultSet.close(); - } - catch (SQLException e) - { - e.printStackTrace(); - } - } - - try - { - if (DataSource != null) - _connection.close(); - } - catch (SQLException e) - { - e.printStackTrace(); - } - } } protected void executeQuery(String query, ResultSetCallable callable, Column...columns) { - PreparedStatement preparedStatement = null; - - try + // Automatic resource management for handling/closing objects. + try ( + Connection connection = getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(query) + ) { - getConnection(false); - - preparedStatement = _connection.prepareStatement(query); - executeQuery(preparedStatement, callable, columns); } catch (SQLException exception) { - getConnection(true); executeQuery(query, callable, columns); } catch (Exception exception) { exception.printStackTrace(); } - finally - { - if (preparedStatement != null) - { - try - { - preparedStatement.close(); - } - catch (SQLException e) - { - e.printStackTrace(); - } - } - - try - { - if (DataSource != null) - _connection.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 (SQLException exception) - { - getConnection(true); - executeUpdate(preparedStatement, columns); - } - catch (Exception exception) - { - exception.printStackTrace(); - } - - return affectedRows; - } - protected void handleDatabaseCall(final DatabaseRunnable databaseRunnable, final String errorMessage) { Thread asyncThread = new Thread(new Runnable() @@ -341,24 +212,6 @@ public abstract class RepositoryBase implements Listener processFailedQueue(); } - @EventHandler - public void validateConnection(UpdateEvent event) - { - if (event.getType() != UpdateType.MIN_02 || DataSource != null) - return; - - Bukkit.getScheduler().runTaskAsynchronously(Plugin, new Runnable() - { - public void run() - { - synchronized (_connectionLock) - { - getConnection(true); - } - } - }); - } - private void processFailedQueue() { synchronized (_queueLock) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/donation/repository/DonationRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/donation/repository/DonationRepository.java index 818c95df9..b7246e8be 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/donation/repository/DonationRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/donation/repository/DonationRepository.java @@ -8,6 +8,7 @@ import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; import mineplex.core.common.util.Callback; +import mineplex.core.database.DBPool; import mineplex.core.database.DatabaseRunnable; import mineplex.core.database.RepositoryBase; import mineplex.core.database.column.ColumnInt; @@ -33,7 +34,7 @@ public class DonationRepository extends RepositoryBase public DonationRepository(JavaPlugin plugin, String webAddress) { - super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); + super(plugin, DBPool.ACCOUNT); _webAddress = webAddress; } 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..444e98bbf 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/inventory/data/InventoryRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/inventory/data/InventoryRepository.java @@ -7,6 +7,7 @@ import java.util.List; import org.bukkit.plugin.java.JavaPlugin; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.ResultSetCallable; import mineplex.core.database.column.ColumnInt; @@ -31,7 +32,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/leaderboard/StatEventsRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/leaderboard/StatEventsRepository.java index 2e0888f1e..1679bc36d 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/leaderboard/StatEventsRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/leaderboard/StatEventsRepository.java @@ -1,5 +1,6 @@ package mineplex.core.leaderboard; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.column.ColumnInt; import mineplex.core.database.column.ColumnVarChar; @@ -35,7 +36,7 @@ public class StatEventsRepository extends RepositoryBase */ public StatEventsRepository(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/PreferencesRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesRepository.java index a451cea77..44852c23e 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; @@ -14,7 +15,7 @@ import mineplex.core.database.column.ColumnVarChar; public class PreferencesRepository extends RepositoryBase { - 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 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 = ?, friendDisplayInventoryUI = ? WHERE uuid=?;"; @@ -36,12 +37,12 @@ public class PreferencesRepository extends RepositoryBase public void saveUserPreferences(NautHashMap preferences) { - PreparedStatement preparedStatement = null; - - try + try + ( + Connection connection = getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_ACCOUNT_PREFERENCES); + ) { - preparedStatement = getConnection().prepareStatement(UPDATE_ACCOUNT_PREFERENCES); - for (Entry entry : preferences.entrySet()) { preparedStatement.setBoolean(1, entry.getValue().HubGames); @@ -93,20 +94,6 @@ public class PreferencesRepository extends RepositoryBase { exception.printStackTrace(); } - finally - { - if (preparedStatement != null) - { - try - { - preparedStatement.close(); - } - catch (SQLException e) - { - e.printStackTrace(); - } - } - } } public UserPreferences loadClientInformation(final ResultSet resultSet) throws SQLException diff --git a/Plugins/Mineplex.Core/src/mineplex/core/report/ReportRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/report/ReportRepository.java index 968010e9c..4a9f91215 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/report/ReportRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/report/ReportRepository.java @@ -7,6 +7,7 @@ import java.util.UUID; import java.util.Map.Entry; import mineplex.core.common.util.NautHashMap; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.ResultSetCallable; import mineplex.core.database.column.ColumnInt; @@ -40,7 +41,7 @@ This will be used to determine if staff are handling public ReportRepository(JavaPlugin plugin, String connectionString) { - super(plugin, connectionString, "root", "tAbechAk3wR7tuTh"); // TODO: Config file for host/pass? + super(plugin, DBPool.ACCOUNT); } @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..eb85bae9d 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/spawn/SpawnRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/spawn/SpawnRepository.java @@ -7,6 +7,7 @@ import java.util.List; import org.bukkit.plugin.java.JavaPlugin; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.ResultSetCallable; import mineplex.core.database.column.ColumnVarChar; @@ -22,8 +23,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/StatsRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/stats/StatsRepository.java index 827b3605c..58c81c9f8 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/stats/StatsRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/stats/StatsRepository.java @@ -9,6 +9,7 @@ import java.util.List; 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.ResultSetCallable; import mineplex.core.database.column.ColumnVarChar; @@ -18,6 +19,7 @@ import org.jooq.DSLContext; import org.jooq.Insert; import org.jooq.Record2; import org.jooq.Result; +import org.jooq.SQLDialect; import org.jooq.Update; import org.jooq.impl.DSL; @@ -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,8 +81,7 @@ public class StatsRepository extends RepositoryBase { try { - - DSLContext context = DSL.using(getConnection(true)); + DSLContext context = DSL.using(getConnectionPool(), SQLDialect.MYSQL); List updates = new ArrayList<>(); List inserts = new ArrayList<>(); @@ -133,7 +134,7 @@ public class StatsRepository extends RepositoryBase synchronized (this) { - context = DSL.using(getConnection()); + context = DSL.using(getConnectionPool(), SQLDialect.MYSQL); } Result> result = context.select(Tables.stats.name, Tables.accountStats.value).from(Tables.accountStats) diff --git a/Plugins/Mineplex.EnjinTranslator/src/mineplex/enjinTranslator/TempRepository.java b/Plugins/Mineplex.EnjinTranslator/src/mineplex/enjinTranslator/TempRepository.java index e052ee72e..ba4cd2729 100644 --- a/Plugins/Mineplex.EnjinTranslator/src/mineplex/enjinTranslator/TempRepository.java +++ b/Plugins/Mineplex.EnjinTranslator/src/mineplex/enjinTranslator/TempRepository.java @@ -2,6 +2,7 @@ package mineplex.enjinTranslator; import org.bukkit.plugin.java.JavaPlugin; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.column.ColumnInt; import mineplex.core.database.column.ColumnVarChar; @@ -12,7 +13,7 @@ public class TempRepository extends RepositoryBase public TempRepository(JavaPlugin plugin) { - super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); + super(plugin, DBPool.ACCOUNT); } public void addGemBooster(String name, int amount) diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/fields/repository/FieldRepository.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/fields/repository/FieldRepository.java index cad9425ef..f5be7a077 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/fields/repository/FieldRepository.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/fields/repository/FieldRepository.java @@ -7,6 +7,7 @@ import java.util.List; import org.bukkit.plugin.java.JavaPlugin; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.ResultSetCallable; import mineplex.core.database.column.ColumnByte; @@ -31,7 +32,7 @@ public class FieldRepository extends RepositoryBase public FieldRepository(JavaPlugin plugin) { - super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh"); + super(plugin, DBPool.ACCOUNT); } public List getFieldBlocks(String server) diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/mail/MailRepository.java b/Plugins/Mineplex.Hub/src/mineplex/hub/mail/MailRepository.java index 59e7e796c..af43ab9f5 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/mail/MailRepository.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/mail/MailRepository.java @@ -6,9 +6,11 @@ import java.util.UUID; import org.bukkit.plugin.java.JavaPlugin; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.database.Tables; import mineplex.database.tables.records.MailRecord; + import org.jooq.DSLContext; import org.jooq.Result; import org.jooq.impl.DSL; @@ -19,8 +21,8 @@ public class MailRepository extends RepositoryBase 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; } diff --git a/Plugins/Mineplex.StaffServer/src/mineplex/staffServer/password/PasswordRepository.java b/Plugins/Mineplex.StaffServer/src/mineplex/staffServer/password/PasswordRepository.java index 796bde45d..20cb041e0 100644 --- a/Plugins/Mineplex.StaffServer/src/mineplex/staffServer/password/PasswordRepository.java +++ b/Plugins/Mineplex.StaffServer/src/mineplex/staffServer/password/PasswordRepository.java @@ -7,6 +7,7 @@ import java.util.List; import org.bukkit.plugin.java.JavaPlugin; +import mineplex.core.database.DBPool; import mineplex.core.database.RepositoryBase; import mineplex.core.database.ResultSetCallable; import mineplex.core.database.column.ColumnVarChar; @@ -23,8 +24,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; }