Added configurable ping interval to MySQL

This commit is contained in:
Eric 2016-08-16 12:26:35 +02:00
parent 51584a3d04
commit aa2051e9cc
4 changed files with 34 additions and 2 deletions

View File

@ -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) {

View File

@ -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;
/**
* <p>The minimum prices for certain items</p>
* This returns a key set, which contains e.g "STONE", "STONE:1", of the <i>minimum-prices</i> 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<String>() : 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<String>() : plugin.getConfig().getConfigurationSection("shop-limits.group").getKeys(true);
shopLimits_player = (plugin.getConfig().getConfigurationSection("shop-limits.player") == null) ? new HashSet<String>() : plugin.getConfig().getConfigurationSection("shop-limits.player").getKeys(true);
blacklist = (plugin.getConfig().getStringList("blacklist") == null) ? new ArrayList<String>() : plugin.getConfig().getStringList("blacklist");

View File

@ -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();
}
}
}

View File

@ -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: ""