From f7313be8469ae2b6a5efc142bfbd540f91f76be3 Mon Sep 17 00:00:00 2001 From: montlikadani Date: Thu, 5 Nov 2020 18:26:27 +0100 Subject: [PATCH] Call some database things in async to do not cause server freeze on startup Fixes #981 --- src/main/java/com/gamingmesh/jobs/Jobs.java | 53 ++++++++++--------- .../java/com/gamingmesh/jobs/dao/JobsDAO.java | 40 +++++++------- .../com/gamingmesh/jobs/dao/JobsSQLite.java | 10 ++-- 3 files changed, 54 insertions(+), 49 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/Jobs.java b/src/main/java/com/gamingmesh/jobs/Jobs.java index 016c141f..0857d513 100644 --- a/src/main/java/com/gamingmesh/jobs/Jobs.java +++ b/src/main/java/com/gamingmesh/jobs/Jobs.java @@ -63,6 +63,7 @@ import java.io.File; import java.io.IOException; import java.sql.SQLException; import java.util.*; +import java.util.concurrent.CompletableFuture; import java.util.logging.Logger; public class Jobs extends JavaPlugin { @@ -465,31 +466,35 @@ public class Jobs extends JavaPlugin { } public static void loadAllPlayersData() { - long time = System.currentTimeMillis(); - // Cloning to avoid issues - HashMap temp = new HashMap<>(getPlayerManager().getPlayersInfoUUIDMap()); - HashMap> playersJobs = dao.getAllJobs(); - HashMap playersPoints = dao.getAllPoints(); - HashMap> playersLogs = dao.getAllLogs(); - HashMap playersArchives = dao.getAllArchivedJobs(); - HashMap playersLimits = dao.loadPlayerLimits(); - for (Iterator it = temp.values().iterator(); it.hasNext();) { - PlayerInfo one = it.next(); - int id = one.getID(); - JobsPlayer jPlayer = getPlayerManager().getJobsPlayerOffline( - one, - playersJobs.get(id), - playersPoints.get(id), - playersLogs.get(id), - playersArchives.get(id), - playersLimits.get(id)); - if (jPlayer != null) - getPlayerManager().addPlayerToCache(jPlayer); - } + CompletableFuture.supplyAsync(() -> { + long time = System.currentTimeMillis(); + // Cloning to avoid issues + HashMap temp = new HashMap<>(getPlayerManager().getPlayersInfoUUIDMap()); + HashMap> playersJobs = dao.getAllJobs(); + HashMap playersPoints = dao.getAllPoints(); + HashMap> playersLogs = dao.getAllLogs(); + HashMap playersArchives = dao.getAllArchivedJobs(); + HashMap playersLimits = dao.loadPlayerLimits(); + for (Iterator it = temp.values().iterator(); it.hasNext();) { + PlayerInfo one = it.next(); + int id = one.getID(); + JobsPlayer jPlayer = getPlayerManager().getJobsPlayerOffline( + one, + playersJobs.get(id), + playersPoints.get(id), + playersLogs.get(id), + playersArchives.get(id), + playersLimits.get(id)); + if (jPlayer != null) + getPlayerManager().addPlayerToCache(jPlayer); + } - if (!getPlayerManager().getPlayersCache().isEmpty()) - consoleMsg("&e[Jobs] Preloaded " + getPlayerManager().getPlayersCache().size() + " players data in " + - ((int) (((System.currentTimeMillis() - time) / 1000d) * 100) / 100D)); + return time; + }).thenAccept(t -> { + if (!getPlayerManager().getPlayersCache().isEmpty()) + consoleMsg("&e[Jobs] Preloaded " + getPlayerManager().getPlayersCache().size() + " players data in " + + ((int) (((System.currentTimeMillis() - t) / 1000d) * 100) / 100D)); + }); } /** diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java index 2f131f6b..cc6ca566 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java @@ -73,7 +73,7 @@ public abstract class JobsDAO { @Override public String getCollumn() { - return this.name(); + return name(); } @Override @@ -105,7 +105,7 @@ public abstract class JobsDAO { @Override public String getCollumn() { - return this.name(); + return name(); } @Override @@ -134,7 +134,7 @@ public abstract class JobsDAO { @Override public String getCollumn() { - return this.name(); + return name(); } @Override @@ -163,7 +163,7 @@ public abstract class JobsDAO { @Override public String getCollumn() { - return this.name(); + return name(); } @Override @@ -193,7 +193,7 @@ public abstract class JobsDAO { @Override public String getCollumn() { - return this.name(); + return name(); } @Override @@ -224,7 +224,7 @@ public abstract class JobsDAO { @Override public String getCollumn() { - return this.name(); + return name(); } @Override @@ -253,7 +253,7 @@ public abstract class JobsDAO { @Override public String getCollumn() { - return this.name(); + return name(); } @Override @@ -285,7 +285,7 @@ public abstract class JobsDAO { @Override public String getCollumn() { - return this.name(); + return name(); } @Override @@ -312,7 +312,7 @@ public abstract class JobsDAO { @Override public String getCollumn() { - return this.name(); + return name(); } @Override @@ -342,7 +342,7 @@ public abstract class JobsDAO { @Override public String getCollumn() { - return this.name(); + return name(); } @Override @@ -403,9 +403,9 @@ public abstract class JobsDAO { private String getQR() { switch (dbType) { case MySQL: - return this.mySQL.replace("[tableName]", prefix + this.tableName); + return mySQL.replace("[tableName]", prefix + tableName); case SqLite: - return this.sQlite.replace("[tableName]", this.tableName); + return sQlite.replace("[tableName]", tableName); default: break; } @@ -415,7 +415,7 @@ public abstract class JobsDAO { public String getQuery() { String rp = ""; List uniques = new ArrayList<>(); - for (JobsTableInterface one : this.getInterface()) { + for (JobsTableInterface one : getInterface()) { if (one.isUnique()) { uniques.add(one); } @@ -449,7 +449,7 @@ public abstract class JobsDAO { } public JobsTableInterface[] getInterface() { - return this.c; + return c; } public String getTableName() { @@ -536,10 +536,10 @@ public abstract class JobsDAO { } private boolean createDefaultTable(DBTables table) { - if (this.isTable(table.getTableName())) + if (isTable(table.getTableName())) return true; try { - this.createTable(table.getQuery()); + createTable(table.getQuery()); return true; } catch (SQLException e) { e.printStackTrace(); @@ -550,9 +550,9 @@ public abstract class JobsDAO { private boolean checkDefaultCollumns() { for (DBTables one : DBTables.values()) { for (JobsTableInterface oneT : one.getInterface()) { - if (this.isCollumn(one.getTableName(), oneT.getCollumn())) + if (isCollumn(one.getTableName(), oneT.getCollumn())) continue; - this.addCollumn(one.getTableName(), oneT.getCollumn(), oneT.getType()); + addCollumn(one.getTableName(), oneT.getCollumn(), oneT.getType()); } } @@ -561,7 +561,7 @@ public abstract class JobsDAO { public void truncateAllTables() { for (DBTables one : DBTables.values()) { - this.truncate(one.getTableName()); + truncate(one.getTableName()); } } @@ -1487,7 +1487,7 @@ public abstract class JobsDAO { } public void continueConvertions(List list) throws SQLException { - JobsConnection conns = this.getConnection(); + JobsConnection conns = getConnection(); if (conns == null) return; PreparedStatement insert = null; diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsSQLite.java b/src/main/java/com/gamingmesh/jobs/dao/JobsSQLite.java index 4fc4e2ae..92b3ce98 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsSQLite.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsSQLite.java @@ -28,7 +28,7 @@ public class JobsSQLite extends JobsDAO { super(plugin, "org.sqlite.JDBC", "jdbc:sqlite:" + new File(file, "jobs.sqlite.db").getPath(), null, null, ""); if (!file.exists()) file.mkdirs(); - this.setDbType(DataBaseType.SqLite); + setDbType(DataBaseType.SqLite); } @Override @@ -89,7 +89,7 @@ public class JobsSQLite extends JobsDAO { public boolean isTable(String table) { DatabaseMetaData md = null; try { - md = this.getConnection().getMetaData(); + md = getConnection().getMetaData(); ResultSet tables = md.getTables(null, null, table, null); if (tables.next()) { tables.close(); @@ -107,7 +107,7 @@ public class JobsSQLite extends JobsDAO { public boolean isCollumn(String table, String collumn) { DatabaseMetaData md = null; try { - md = this.getConnection().getMetaData(); + md = getConnection().getMetaData(); ResultSet tables = md.getColumns(null, null, table, collumn); if (tables.next()) { tables.close(); @@ -145,7 +145,7 @@ public class JobsSQLite extends JobsDAO { Statement statement = null; String query = null; try { - if (!this.isTable(table)) { + if (!isTable(table)) { Jobs.consoleMsg("&cTable \"" + table + "\" does not exist."); return false; } @@ -168,7 +168,7 @@ public class JobsSQLite extends JobsDAO { Statement statement = null; String query = null; try { - if (!this.isTable(table)) { + if (!isTable(table)) { Jobs.consoleMsg("&cTable \"" + table + "\" does not exist."); return false; }