From 274bc6e56162b6c862eece597c117649ce86bc36 Mon Sep 17 00:00:00 2001 From: montlikadani Date: Sat, 12 Sep 2020 20:46:05 +0200 Subject: [PATCH] Do connecting to database thread safe --- .../gamingmesh/jobs/CMILib/CMIMaterial.java | 6 +-- .../com/gamingmesh/jobs/CMILib/Version.java | 1 + src/main/java/com/gamingmesh/jobs/Jobs.java | 5 +- .../jobs/dao/JobsConnectionPool.java | 32 ++++++++---- .../java/com/gamingmesh/jobs/dao/JobsDAO.java | 49 +++++++++---------- .../com/gamingmesh/jobs/dao/JobsMySQL.java | 30 ++---------- .../com/gamingmesh/jobs/dao/JobsSQLite.java | 27 ++-------- 7 files changed, 58 insertions(+), 92 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/CMILib/CMIMaterial.java b/src/main/java/com/gamingmesh/jobs/CMILib/CMIMaterial.java index 4975e6b3..5f1f3993 100644 --- a/src/main/java/com/gamingmesh/jobs/CMILib/CMIMaterial.java +++ b/src/main/java/com/gamingmesh/jobs/CMILib/CMIMaterial.java @@ -1217,7 +1217,7 @@ public enum CMIMaterial { public void updateMaterial() { if (mat == null) { for (Material one : Material.class.getEnumConstants()) { - if (!one.name().replace("LEGACY_", "").replace("_", "").equalsIgnoreCase(this.name().replace("_", ""))) + if (!one.name().replaceAll("LEGACY_|_", "").equalsIgnoreCase(this.name().replace("_", ""))) continue; mat = one; break; @@ -1225,7 +1225,7 @@ public enum CMIMaterial { } if (mat == null) { for (Material one : Material.class.getEnumConstants()) { - if (!one.name().replace("LEGACY_", "").replace("_", "").equalsIgnoreCase(this.getName().replace(" ", ""))) + if (!one.name().replaceAll("LEGACY_|_", "").equalsIgnoreCase(this.getName().replace(" ", ""))) continue; mat = one; break; @@ -1234,7 +1234,7 @@ public enum CMIMaterial { if (mat == null && !this.getLegacyNames().isEmpty()) { main: for (Material one : Material.class.getEnumConstants()) { for (String oneL : this.getLegacyNames()) { - if (!one.name().replace("LEGACY_", "").replace("_", "").equalsIgnoreCase(oneL.replace(" ", "").replace("_", ""))) + if (!one.name().replaceAll("LEGACY_|_", "").equalsIgnoreCase(oneL.replace(" ", "").replace("_", ""))) continue main; } mat = one; diff --git a/src/main/java/com/gamingmesh/jobs/CMILib/Version.java b/src/main/java/com/gamingmesh/jobs/CMILib/Version.java index f009113e..098d7803 100644 --- a/src/main/java/com/gamingmesh/jobs/CMILib/Version.java +++ b/src/main/java/com/gamingmesh/jobs/CMILib/Version.java @@ -21,6 +21,7 @@ public enum Version { v1_15_R1, v1_16_R1, v1_16_R2, + v1_16_R3, v1_17_R1, v1_17_R2, v1_18_R1, diff --git a/src/main/java/com/gamingmesh/jobs/Jobs.java b/src/main/java/com/gamingmesh/jobs/Jobs.java index 9aab73c8..2342d435 100644 --- a/src/main/java/com/gamingmesh/jobs/Jobs.java +++ b/src/main/java/com/gamingmesh/jobs/Jobs.java @@ -428,10 +428,9 @@ public class Jobs extends JavaPlugin { public void startup() { reload(true); loadAllPlayersData(); + // add all online players - for (Player online : Bukkit.getServer().getOnlinePlayers()) { - getPlayerManager().playerJoin(online); - } + Bukkit.getServer().getOnlinePlayers().forEach(getPlayerManager()::playerJoin); } public static void loadAllPlayersData() { diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsConnectionPool.java b/src/main/java/com/gamingmesh/jobs/dao/JobsConnectionPool.java index 49341293..6958de42 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsConnectionPool.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsConnectionPool.java @@ -2,6 +2,7 @@ package com.gamingmesh.jobs.dao; import java.sql.DriverManager; import java.sql.SQLException; +import java.util.concurrent.CompletableFuture; public class JobsConnectionPool { @@ -24,20 +25,31 @@ public class JobsConnectionPool { connection = null; } - if (connection == null) { - connection = new JobsConnection(DriverManager.getConnection(url, username, password)); - } + CompletableFuture.supplyAsync(() -> { + if (connection == null) { + try { + connection = new JobsConnection(DriverManager.getConnection(url, username, password)); + } catch (SQLException e) { + } + } + + return true; + }); return connection; } - public synchronized void closeConnection() { - if (connection != null) { - try { - connection.closeConnection(); - } catch (SQLException e) { - e.printStackTrace(); + public void closeConnection() { + CompletableFuture.supplyAsync(() -> { + if (connection != null) { + try { + connection.closeConnection(); + } catch (SQLException e) { + e.printStackTrace(); + } } - } + + return true; + }); } } diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java index 1e848518..b7290a1a 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java @@ -12,6 +12,7 @@ import java.util.List; import java.util.Map.Entry; import java.util.Set; import java.util.UUID; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import org.bukkit.Bukkit; @@ -472,24 +473,25 @@ public abstract class JobsDAO { pool = new JobsConnectionPool(url, username, password); } - public final synchronized void setUp() throws SQLException { - if (getConnection() == null) - return; + public final synchronized void setUp() { + CompletableFuture.supplyAsync(() -> { + if (getConnection() == null) + return null; - vacuum(); - setupConfig(); + vacuum(); - try { - for (DBTables one : DBTables.values()) { - createDefaultTable(one); + try { + for (DBTables one : DBTables.values()) { + createDefaultTable(one); + } + + checkDefaultCollumns(); + } finally { } - checkDefaultCollumns(); - } finally { - } - } - @Deprecated - protected abstract void setupConfig() throws SQLException; + return null; + }); + } protected abstract void checkUpdate() throws SQLException; @@ -886,9 +888,6 @@ public abstract class JobsDAO { } } - int convertSchedId = -1; - boolean converted = true; - public void recordNewWorld(String worldName) { JobsConnection conn = getConnection(); if (conn == null) @@ -944,13 +943,12 @@ public abstract class JobsDAO { } } + private boolean converted = true; + public void triggerTableIdUpdate() { // Lets convert old fields - if (convertSchedId > 0) - Bukkit.getServer().getScheduler().cancelTask(convertSchedId); - if (!converted) { - convertSchedId = Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () -> { + Bukkit.getServer().getScheduler().runTaskLater(plugin, () -> { Jobs.consoleMsg("&6[Jobs] Converting to new database format"); convertID(); Jobs.consoleMsg("&6[Jobs] Converted to new database format"); @@ -2556,6 +2554,10 @@ public abstract class JobsDAO { */ public void executeSQL(String sql) throws SQLException { JobsConnection conn = getConnection(); + if (conn == null) { + return; + } + Statement stmt = null; try { stmt = conn.createStatement(); @@ -2565,11 +2567,6 @@ public abstract class JobsDAO { } } - /** - * Get a database connection - * @return DBConnection object - * @throws SQLException - */ protected JobsConnection getConnection() { try { return isConnected() ? pool.getConnection() : null; diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsMySQL.java b/src/main/java/com/gamingmesh/jobs/dao/JobsMySQL.java index 0090d8f8..e2003361 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsMySQL.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsMySQL.java @@ -18,37 +18,15 @@ public class JobsMySQL extends JobsDAO { } public void initialize() { - try { - setUp(); - } catch (SQLException e) { - e.printStackTrace(); - } + setUp(); } public JobsMySQL initialize(Jobs plugin, String hostname, String database, String username, String password, String prefix, boolean certificate, boolean ssl, boolean autoReconnect) { JobsMySQL dao = new JobsMySQL(plugin, hostname, database, username, password, prefix, certificate, ssl, autoReconnect); - try { - dao.setUp(); - } catch (SQLException e) { - e.printStackTrace(); - } + dao.setUp(); return dao; } - @Override - protected synchronized void setupConfig() throws SQLException { - JobsConnection conn = getConnection(); - if (conn == null) { - Jobs.consoleMsg("&cCould not run database updates! Could not connect to MySQL!"); - return; - } - - String name = getPrefix() + "config"; - if (isTable(name)) { - drop(name); - } - } - @Override protected synchronized void checkUpdate() throws SQLException { JobsConnection conn = getConnection(); @@ -162,7 +140,7 @@ public class JobsMySQL extends JobsDAO { statement.executeQuery("SELECT `" + collumn + "` FROM `" + table + "`;"); return true; } catch (SQLException e) { - Jobs.consoleMsg("Not a culumn |" + "SELECT " + collumn + " FROM " + table + "|"); + Jobs.consoleMsg("Not a collumn |" + "SELECT " + collumn + " FROM " + table + "|"); return false; } finally { close(statement); @@ -179,7 +157,7 @@ public class JobsMySQL extends JobsDAO { return false; } try { - Jobs.consoleMsg("Creating culumn |" + "ALTER TABLE `" + table + "` ADD COLUMN `" + collumn + "` " + type + ";" + "|"); + Jobs.consoleMsg("Creating collumn |" + "ALTER TABLE `" + table + "` ADD COLUMN `" + collumn + "` " + type + ";" + "|"); statement.executeUpdate("ALTER TABLE `" + table + "` ADD COLUMN `" + collumn + "` " + type + ";"); return true; } catch (SQLException e) { diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsSQLite.java b/src/main/java/com/gamingmesh/jobs/dao/JobsSQLite.java index 0c4cd3d7..4fc4e2ae 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsSQLite.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsSQLite.java @@ -13,21 +13,14 @@ import com.gamingmesh.jobs.dao.JobsManager.DataBaseType; public class JobsSQLite extends JobsDAO { public void initialize() { - try { - this.setUp(); - } catch (SQLException e) { - e.printStackTrace(); - } + setUp(); } public JobsSQLite initialize(File dir) { if (!dir.exists()) dir.mkdirs(); - try { - this.setUp(); - } catch (SQLException e) { - e.printStackTrace(); - } + + setUp(); return this; } @@ -38,20 +31,6 @@ public class JobsSQLite extends JobsDAO { this.setDbType(DataBaseType.SqLite); } - @Override - protected synchronized void setupConfig() throws SQLException { - JobsConnection conn = getConnection(); - if (conn == null) { - Jobs.consoleMsg("&cCould not run database updates! Could not connect to MySQL!"); - return; - } - - String name = getPrefix() + "config"; - if (isTable(name)) { - drop(name); - } - } - @Override protected synchronized void checkUpdate() throws SQLException { JobsConnection conn = getConnection();