diff --git a/src/main/java/net/simplyvanilla/simplyrank/SimplyRankPlugin.java b/src/main/java/net/simplyvanilla/simplyrank/SimplyRankPlugin.java index 929420a..07811fd 100644 --- a/src/main/java/net/simplyvanilla/simplyrank/SimplyRankPlugin.java +++ b/src/main/java/net/simplyvanilla/simplyrank/SimplyRankPlugin.java @@ -3,6 +3,7 @@ import com.google.common.reflect.TypeToken; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.TextColor; import net.simplyvanilla.simplyrank.addresswhitelist.AddressWhitelistService; @@ -46,6 +47,8 @@ public class SimplyRankPlugin extends JavaPlugin { private MySqlClient mySqlClient = null; + private ScheduledTask cleanerTask; + @Override public void onEnable() { @@ -93,7 +96,7 @@ public void onEnable() { this.playerDataService = new PlayerDataService(mySqlRepository, mySqlRepository); proxyService = new ProxyService(mySqlRepository, new ProxyCheckProvider(this.getConfig().getString("proxycheck-api-url", "https://proxycheck.io/v2/%s&vpn=1"))); - Bukkit.getAsyncScheduler().runAtFixedRate(this, new ProxyTtlCleanupTask(proxyService, this.getConfig().getInt("proxycache-ttl", 720)), 1, 10, TimeUnit.SECONDS); + this.cleanerTask = Bukkit.getAsyncScheduler().runAtFixedRate(this, new ProxyTtlCleanupTask(proxyService, this.getConfig().getInt("proxycache-ttl", 720)), 1, 10, TimeUnit.SECONDS); if (!this.playerDataService.groupExists("default")) { GroupData defaultData = new GroupData(NamedTextColor.GRAY, "Member "); @@ -156,6 +159,7 @@ public void onEnable() { @Override public void onDisable() { instance = null; + if (this.cleanerTask != null) this.cleanerTask.cancel(); if (this.mySqlClient != null) { this.mySqlClient.close(); } diff --git a/src/main/java/net/simplyvanilla/simplyrank/database/sql/MySqlClient.java b/src/main/java/net/simplyvanilla/simplyrank/database/sql/MySqlClient.java index e0c4a47..cc3a65e 100644 --- a/src/main/java/net/simplyvanilla/simplyrank/database/sql/MySqlClient.java +++ b/src/main/java/net/simplyvanilla/simplyrank/database/sql/MySqlClient.java @@ -9,6 +9,9 @@ public class MySqlClient { public static final String TABLE_PLAYERS_NAME = "player"; public static final String TABLE_GROUPS_NAME = "group"; + private static final int MAX_TRIES = 10; + private static final long WAIT_TIME = 3000; + private final String url; private final String username; private final String password; @@ -24,6 +27,27 @@ public MySqlClient(String url, String user, String password) { initTables(); } + public void testConnection() { + for (int i = 0; i < MAX_TRIES; i++) { + if (testConnectionSingle()) return; + try { + Thread.sleep(WAIT_TIME); + } catch (InterruptedException ignored) { + break; + } + } + throw new RuntimeException(String.format("It was not possible to recover Database connection after %s tries", MAX_TRIES)); + } + + private boolean testConnectionSingle() { + try { + connection = DriverManager.getConnection(url, username, password); + return connection.isValid(1); + } catch (SQLException e) { + return false; + } + } + public void connect() { try { connection = DriverManager.getConnection(url, username, password); @@ -41,11 +65,13 @@ public void close() { } public void update(PreparedStatement statement) throws SQLException { + testConnection(); statement.executeUpdate(); statement.close(); } public ResultSet query(PreparedStatement statement) throws SQLException { + testConnection(); ResultSet rs; rs = statement.executeQuery(); @@ -55,6 +81,7 @@ public ResultSet query(PreparedStatement statement) throws SQLException { } private void executeRawStatement(String cmd) throws SQLException { + testConnection(); try (Statement st = connection.createStatement()) { st.execute(cmd); } @@ -127,6 +154,7 @@ PRIMARY KEY (`id`), } public PreparedStatement prepareStatement(String qry) throws SQLException { + testConnection(); return connection.prepareStatement(qry); }