From aa2051e9cca9aafbaeff731c726c9bfdc73b212f Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 16 Aug 2016 12:26:35 +0200 Subject: [PATCH] Added configurable ping interval to MySQL --- .../java/de/epiceric/shopchest/ShopChest.java | 10 ++++++++++ .../java/de/epiceric/shopchest/config/Config.java | 6 +++++- .../java/de/epiceric/shopchest/sql/MySQL.java | 15 ++++++++++++++- src/main/resources/config.yml | 5 +++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/ShopChest.java b/src/main/java/de/epiceric/shopchest/ShopChest.java index 4710358..6f9cf3d 100644 --- a/src/main/java/de/epiceric/shopchest/ShopChest.java +++ b/src/main/java/de/epiceric/shopchest/ShopChest.java @@ -219,6 +219,16 @@ public class ShopChest extends JavaPlugin { debug("Using database type: MySQL"); getLogger().info("Using MySQL"); database = new MySQL(this); + if (config.database_mysql_ping_interval > 0) { + Bukkit.getScheduler().runTaskTimer(this, new Runnable() { + @Override + public void run() { + if (database instanceof MySQL) { + ((MySQL) database).ping(); + } + } + }, config.database_mysql_ping_interval * 20L, config.database_mysql_ping_interval * 20L); + } } if (config.auto_reload_time > 0) { diff --git a/src/main/java/de/epiceric/shopchest/config/Config.java b/src/main/java/de/epiceric/shopchest/config/Config.java index 3012676..827cb04 100644 --- a/src/main/java/de/epiceric/shopchest/config/Config.java +++ b/src/main/java/de/epiceric/shopchest/config/Config.java @@ -40,6 +40,9 @@ public class Config { /** The database type used for ShopChest. **/ public Database.DatabaseType database_type; + /** The interval in seconds, a ping is sent to the MySQL server **/ + public int database_mysql_ping_interval; + /** *

The minimum prices for certain items

* This returns a key set, which contains e.g "STONE", "STONE:1", of the minimum-prices section in ShopChest's config. @@ -264,6 +267,7 @@ public class Config { * Reload the configuration values from config.yml */ public void reload(boolean firstLoad, boolean langReload) { + database_mysql_ping_interval = plugin.getConfig().getInt("database.mysql.ping-interval"); database_mysql_host = plugin.getConfig().getString("database.mysql.hostname"); database_mysql_port = plugin.getConfig().getInt("database.mysql.port"); database_mysql_database = plugin.getConfig().getString("database.mysql.database"); @@ -271,7 +275,7 @@ public class Config { database_mysql_password = plugin.getConfig().getString("database.mysql.password"); database_type = Database.DatabaseType.valueOf(plugin.getConfig().getString("database.type")); minimum_prices = (plugin.getConfig().getConfigurationSection("minimum-prices") == null) ? new HashSet() : plugin.getConfig().getConfigurationSection("minimum-prices").getKeys(true); - allow_broken_items = (plugin.getConfig().getBoolean("allow-broken-items")); + allow_broken_items = plugin.getConfig().getBoolean("allow-broken-items"); shopLimits_group = (plugin.getConfig().getConfigurationSection("shop-limits.group") == null) ? new HashSet() : plugin.getConfig().getConfigurationSection("shop-limits.group").getKeys(true); shopLimits_player = (plugin.getConfig().getConfigurationSection("shop-limits.player") == null) ? new HashSet() : plugin.getConfig().getConfigurationSection("shop-limits.player").getKeys(true); blacklist = (plugin.getConfig().getStringList("blacklist") == null) ? new ArrayList() : plugin.getConfig().getStringList("blacklist"); diff --git a/src/main/java/de/epiceric/shopchest/sql/MySQL.java b/src/main/java/de/epiceric/shopchest/sql/MySQL.java index 6b71b95..7b9b961 100644 --- a/src/main/java/de/epiceric/shopchest/sql/MySQL.java +++ b/src/main/java/de/epiceric/shopchest/sql/MySQL.java @@ -4,6 +4,8 @@ import de.epiceric.shopchest.ShopChest; import java.sql.Connection; import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.SQLException; public class MySQL extends Database { @@ -20,7 +22,7 @@ public class MySQL extends Database { Class.forName("com.mysql.jdbc.Driver"); - String connectUrl = "jdbc:mysql://" + plugin.getShopChestConfig().database_mysql_host + ":" + plugin.getShopChestConfig().database_mysql_port + "/" + plugin.getShopChestConfig().database_mysql_database + "?autoReconnect=true"; + String connectUrl = "jdbc:mysql://" + plugin.getShopChestConfig().database_mysql_host + ":" + plugin.getShopChestConfig().database_mysql_port + "/" + plugin.getShopChestConfig().database_mysql_database; plugin.debug("Connecting to MySQL Server \"" + connectUrl + "\" as user \"" + plugin.getShopChestConfig().database_mysql_username + "\""); connection = DriverManager.getConnection(connectUrl, plugin.getShopChestConfig().database_mysql_username, plugin.getShopChestConfig().database_mysql_password); @@ -34,4 +36,15 @@ public class MySQL extends Database { return null; } + + public void ping() { + try (PreparedStatement ps = connection.prepareStatement("/* ping */ SELECT 1")) { + plugin.debug("Pinging to MySQL server..."); + ps.executeQuery(); + } catch (SQLException ex) { + plugin.getLogger().severe("Failed to ping to MySQL server. Trying to reconnect..."); + plugin.debug("Failed to ping to MySQL server. Trying to reconnect..."); + connect(); + } + } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 011740e..cdc62c9 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -98,6 +98,11 @@ database: # (You can leave this empty if you're using SQLite) mysql: + # ...interval in seconds, when the database should be pinged, to keep the connection alive + # This should be lower than the 'connect_timeout' variable in your MySQL server + # You can set this to '0' to disable the ping interval + ping-interval: 3600 + # ...hostname where the database is accessible hostname: ""