From 312e7c9dcabe4c7a806cc9376fffc9df4e6a11bf Mon Sep 17 00:00:00 2001 From: Eric Date: Sat, 6 Aug 2016 12:17:53 +0200 Subject: [PATCH] Disconnect from database when plugin disables + Reconnect to database on shop reload or server reload + A few debug messages were added on SQLExceptions --- .../java/de/epiceric/shopchest/ShopChest.java | 2 ++ .../de/epiceric/shopchest/sql/Database.java | 34 ++++++++++++++----- .../epiceric/shopchest/utils/ShopUtils.java | 3 +- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/ShopChest.java b/src/main/java/de/epiceric/shopchest/ShopChest.java index b8c4983..1536db3 100644 --- a/src/main/java/de/epiceric/shopchest/ShopChest.java +++ b/src/main/java/de/epiceric/shopchest/ShopChest.java @@ -346,6 +346,8 @@ public class ShopChest extends JavaPlugin { } } + database.disconnect(); + if (fw != null && config.enable_debug_log) { try { fw.close(); diff --git a/src/main/java/de/epiceric/shopchest/sql/Database.java b/src/main/java/de/epiceric/shopchest/sql/Database.java index 6914f29..f661895 100644 --- a/src/main/java/de/epiceric/shopchest/sql/Database.java +++ b/src/main/java/de/epiceric/shopchest/sql/Database.java @@ -3,7 +3,6 @@ package de.epiceric.shopchest.sql; import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.shop.Shop.ShopType; -import de.epiceric.shopchest.utils.ShopUtils; import de.epiceric.shopchest.utils.Utils; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -21,22 +20,24 @@ public abstract class Database { public Database(ShopChest plugin) { this.plugin = plugin; - initialize(); } /** - * @return Connection to the database + * @return New connection to the database */ public abstract Connection getConnection(); /** - * Initializes the database.
+ * (Re-)Connects to the the database and initializes it.
* Creates the table (if doesn't exist) and tests the connection */ - private void initialize() { - connection = getConnection(); - + public void connect() { try { + disconnect(); + + plugin.debug("Connecting to database..."); + connection = getConnection(); + String queryCreateTable = "CREATE TABLE IF NOT EXISTS shop_list (" + "`id` int(11) NOT NULL," + "`vendor` tinytext NOT NULL," + @@ -64,10 +65,11 @@ public abstract class Database { } plugin.debug("Initialized database with " + count + " entries"); - close(ps, rs); } catch (SQLException ex) { + plugin.debug("Failed to connect to database"); + plugin.debug(ex); ex.printStackTrace(); } } @@ -289,6 +291,22 @@ public abstract class Database { } } + /** + * Closes the connection to the database + */ + public void disconnect() { + try { + if (connection != null) { + plugin.debug("Disconnecting from database..."); + connection.close(); + } + } catch (SQLException e) { + plugin.debug("Failed to disconnect from database"); + plugin.debug(e); + e.printStackTrace(); + } + } + public enum ShopInfo { SHOP, VENDOR, diff --git a/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java b/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java index 8fc6f72..bdef077 100644 --- a/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java +++ b/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java @@ -13,7 +13,6 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.InventoryHolder; import java.util.*; -import java.util.logging.Level; public class ShopUtils { @@ -207,6 +206,8 @@ public class ShopUtils { public int reloadShops(boolean reloadConfig) { plugin.debug("Reloading shops..."); + plugin.getShopDatabase().connect(); + if (reloadConfig) plugin.getShopChestConfig().reload(false, true); int highestId = plugin.getShopDatabase().getHighestID();