Disconnect from database when plugin disables

+ Reconnect to database on shop reload or server reload
+ A few debug messages were added on SQLExceptions
This commit is contained in:
Eric 2016-08-06 12:17:53 +02:00
parent d99f63a8e5
commit 312e7c9dca
3 changed files with 30 additions and 9 deletions

View File

@ -346,6 +346,8 @@ public class ShopChest extends JavaPlugin {
} }
} }
database.disconnect();
if (fw != null && config.enable_debug_log) { if (fw != null && config.enable_debug_log) {
try { try {
fw.close(); fw.close();

View File

@ -3,7 +3,6 @@ package de.epiceric.shopchest.sql;
import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.shop.Shop.ShopType; import de.epiceric.shopchest.shop.Shop.ShopType;
import de.epiceric.shopchest.utils.ShopUtils;
import de.epiceric.shopchest.utils.Utils; import de.epiceric.shopchest.utils.Utils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
@ -21,22 +20,24 @@ public abstract class Database {
public Database(ShopChest plugin) { public Database(ShopChest plugin) {
this.plugin = plugin; this.plugin = plugin;
initialize();
} }
/** /**
* @return Connection to the database * @return New connection to the database
*/ */
public abstract Connection getConnection(); public abstract Connection getConnection();
/** /**
* Initializes the database. <br> * (Re-)Connects to the the database and initializes it. <br>
* Creates the table (if doesn't exist) and tests the connection * Creates the table (if doesn't exist) and tests the connection
*/ */
private void initialize() { public void connect() {
try {
disconnect();
plugin.debug("Connecting to database...");
connection = getConnection(); connection = getConnection();
try {
String queryCreateTable = "CREATE TABLE IF NOT EXISTS shop_list (" + String queryCreateTable = "CREATE TABLE IF NOT EXISTS shop_list (" +
"`id` int(11) NOT NULL," + "`id` int(11) NOT NULL," +
"`vendor` tinytext NOT NULL," + "`vendor` tinytext NOT NULL," +
@ -64,10 +65,11 @@ public abstract class Database {
} }
plugin.debug("Initialized database with " + count + " entries"); plugin.debug("Initialized database with " + count + " entries");
close(ps, rs); close(ps, rs);
} catch (SQLException ex) { } catch (SQLException ex) {
plugin.debug("Failed to connect to database");
plugin.debug(ex);
ex.printStackTrace(); 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 { public enum ShopInfo {
SHOP, SHOP,
VENDOR, VENDOR,

View File

@ -13,7 +13,6 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.InventoryHolder;
import java.util.*; import java.util.*;
import java.util.logging.Level;
public class ShopUtils { public class ShopUtils {
@ -207,6 +206,8 @@ public class ShopUtils {
public int reloadShops(boolean reloadConfig) { public int reloadShops(boolean reloadConfig) {
plugin.debug("Reloading shops..."); plugin.debug("Reloading shops...");
plugin.getShopDatabase().connect();
if (reloadConfig) plugin.getShopChestConfig().reload(false, true); if (reloadConfig) plugin.getShopChestConfig().reload(false, true);
int highestId = plugin.getShopDatabase().getHighestID(); int highestId = plugin.getShopDatabase().getHighestID();