From e82c28e5e8ef1d1993579ead92c95aa6473004c8 Mon Sep 17 00:00:00 2001 From: Eric Date: Mon, 22 Jul 2019 16:07:30 +0200 Subject: [PATCH] Improve output of database errors --- src/main/java/de/epiceric/shopchest/ShopChest.java | 2 ++ .../shopchest/command/ShopCommandExecutor.java | 4 +++- .../shopchest/listeners/ShopUpdateListener.java | 8 +++----- .../java/de/epiceric/shopchest/sql/Database.java | 13 +++++++++++-- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/ShopChest.java b/src/main/java/de/epiceric/shopchest/ShopChest.java index d235392..b7eb264 100644 --- a/src/main/java/de/epiceric/shopchest/ShopChest.java +++ b/src/main/java/de/epiceric/shopchest/ShopChest.java @@ -448,6 +448,8 @@ public class ShopChest extends JavaPlugin { @Override public void onError(Throwable throwable) { // Database connection probably failed => disable plugin to prevent more errors + getLogger().severe("No database access. Disabling ShopChest"); + if (throwable != null) getLogger().severe(throwable.getMessage()); getServer().getPluginManager().disablePlugin(ShopChest.this); } }); diff --git a/src/main/java/de/epiceric/shopchest/command/ShopCommandExecutor.java b/src/main/java/de/epiceric/shopchest/command/ShopCommandExecutor.java index b8bf04d..22896c1 100644 --- a/src/main/java/de/epiceric/shopchest/command/ShopCommandExecutor.java +++ b/src/main/java/de/epiceric/shopchest/command/ShopCommandExecutor.java @@ -204,7 +204,9 @@ class ShopCommandExecutor implements CommandExecutor { // Database connection probably failed => disable plugin to prevent more errors sender.sendMessage(LanguageUtils.getMessage(Message.ERROR_OCCURRED, new Replacement(Placeholder.ERROR, "No database access: Disabling ShopChest"))); - Bukkit.getPluginManager().disablePlugin(plugin); + plugin.getLogger().severe("No database access: Disabling ShopChest"); + if (throwable != null) plugin.getLogger().severe(throwable.getMessage()); + plugin.getServer().getPluginManager().disablePlugin(plugin); } }); } diff --git a/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java b/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java index e3505be..bed4ec7 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java @@ -4,7 +4,6 @@ import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.utils.Callback; -import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -53,11 +52,9 @@ public class ShopUpdateListener implements Listener { final Player p = e.getPlayer(); // Wait till the chunk should have loaded on the client - // Update IF worlds are different OR chunks are different (as many teleports are in same chunk) if (!from.getWorld().getName().equals(to.getWorld().getName()) || from.getChunk().getX() != to.getChunk().getX() || from.getChunk().getZ() != to.getChunk().getZ()) { - // Wait for 15 ticks before we actually put it in the queue new BukkitRunnable() { @Override public void run() { @@ -71,7 +68,6 @@ public class ShopUpdateListener implements Listener { shop.getHologram().hidePlayer(p); } } - // so next update will update correctly plugin.getShopUtils().resetPlayerLocation(p); } }); @@ -100,7 +96,9 @@ public class ShopUpdateListener implements Listener { @Override public void onError(Throwable throwable) { // Database connection probably failed => disable plugin to prevent more errors - Bukkit.getPluginManager().disablePlugin(plugin); + plugin.getLogger().severe("No database access. Disabling ShopChest"); + if (throwable != null) plugin.getLogger().severe(throwable.getMessage()); + plugin.getServer().getPluginManager().disablePlugin(plugin); } }); } diff --git a/src/main/java/de/epiceric/shopchest/sql/Database.java b/src/main/java/de/epiceric/shopchest/sql/Database.java index d9684ac..2b26f3f 100644 --- a/src/main/java/de/epiceric/shopchest/sql/Database.java +++ b/src/main/java/de/epiceric/shopchest/sql/Database.java @@ -255,10 +255,18 @@ public abstract class Database { public void run() { disconnect(); - dataSource = getDataSource(); + try { + dataSource = getDataSource(); + } catch (Exception e) { + callback.onError(e); + plugin.debug(e); + return; + } if (dataSource == null) { - callback.onError(new SQLException("Failed to get data source")); + Exception e = new IllegalStateException("Data source is null"); + callback.onError(e); + plugin.debug(e); return; } @@ -719,6 +727,7 @@ public abstract class Database { public void disconnect() { if (dataSource != null) { dataSource.close(); + dataSource = null; } }