auto-reconnect

Former-commit-id: 5a2d243da5cbd98e54280e223e3d8b68ad41131f [formerly 54c8f0cc1710dc99bece4ab09e5f9217fc9d349c]
Former-commit-id: ded2541870debf59540a7bd390c466fbda8b4e38
This commit is contained in:
Ghost_chu committed 2018-11-16 18:21:24 +08:00
1 parent 1bca75895e
commit 41422cab86
4 files changed
+110 -30

No files matched your search

@@ -692,6 +692,15 @@ public class QS implements CommandExecutor {
loc.setPitch(pitch * 180f / (float) Math.PI);
return loc;
}
public void sendDebugInfomation(CommandSender s) {
s.sendMessage("Running "+plugin.getDescription().getVersion()+" on server "+plugin.getServer().getVersion()+" for Bukkit "+ plugin.getServer().getBukkitVersion());
try {
s.sendMessage("Database: "+plugin.getDB().getConnection().getMetaData().getDatabaseProductName()+" @ "+plugin.getDB().getConnection().getMetaData().getDatabaseProductVersion());
} catch (SQLException e) {
s.sendMessage("Database: A error happed when getting data.");
e.printStackTrace();
}
}
public void sendHelp(CommandSender s) {
s.sendMessage(MsgUtil.getMessage("command.description.title"));
@@ -8,8 +8,11 @@ import org.bukkit.inventory.ItemStack;
import org.maxgamer.quickshop.QuickShop;
import org.maxgamer.quickshop.Util.Util;
import com.sun.xml.internal.bind.v2.model.annotation.Quick;
public class DatabaseHelper {
public static void setup(Database db) throws SQLException {
connectCheck();
if (!db.hasTable("shops")) {
createShopsTable(db);
}
@@ -29,6 +32,7 @@ public class DatabaseHelper {
* If the connection is invalid.
*/
public static void createScheduleTable(Database db) throws SQLException {
connectCheck();
Statement st = db.getConnection().createStatement();
//String createTable = "CREATE TABLE shops (" + "owner TEXT(32) NOT NULL, " + "price double(32, 2) NOT NULL, " + "itemConfig TEXT CHARSET utf8 NOT NULL, " + "x INTEGER(32) NOT NULL, " + "y INTEGER(32) NOT NULL, " + "z INTEGER(32) NOT NULL, " + "world VARCHAR(32) NOT NULL, " + "unlimited boolean, " + "type boolean, " + "PRIMARY KEY (x, y, z, world) " + ");";
String createTable ="CREATE TABLE schedule (owner TEXT(32) NOT NULL, world VARCHAR(32) NOT NULL, x INT NOT NULL, y INT NOT NULL, z INT NOT NULL, timestamp INT NOT NULL, PRIMARY KEY (owner, world, x, y, z, timestamp) );";
@@ -39,6 +43,7 @@ public class DatabaseHelper {
* Verifies that all required columns exist.
*/
public static void checkColumns(Database db) {
connectCheck();
PreparedStatement ps = null;
try {
// V3.4.2
@@ -63,6 +68,7 @@ public class DatabaseHelper {
* If the connection is invalid.
*/
public static void createShopsTable(Database db) throws SQLException {
connectCheck();
Statement st = db.getConnection().createStatement();
String createTable = "CREATE TABLE shops (owner VARCHAR(32) NOT NULL, price double(32, 2) NOT NULL, itemConfig TEXT CHARSET utf8 NOT NULL, x INTEGER(32) NOT NULL, y INTEGER(32) NOT NULL, z INTEGER(32) NOT NULL, world VARCHAR(32) NOT NULL, unlimited boolean, type boolean, PRIMARY KEY (x, y, z, world) );";
st.execute(createTable);
@@ -75,17 +81,20 @@ public class DatabaseHelper {
* If the connection is invalid
*/
public static void createMessagesTable(Database db) throws SQLException {
connectCheck();
Statement st = db.getConnection().createStatement();
String createTable = "CREATE TABLE messages (owner VARCHAR(32) NOT NULL, message TEXT(25) NOT NULL, time BIGINT(32) NOT NULL );";
st.execute(createTable);
}
public static ResultSet selectAllShops(Database db) throws SQLException {
connectCheck();
PreparedStatement ps = db.getConnection().prepareStatement("SELECT * FROM shops");
return ps.executeQuery();
}
public static void removeShop(Database db, int x, int y, int z, String worldName) throws SQLException {
connectCheck();
db.getConnection().createStatement()
.executeUpdate("DELETE FROM shops WHERE x = " + x + " AND y = " + y + " AND z = " + z
+ " AND world = \"" + worldName + "\""
@@ -96,11 +105,13 @@ public class DatabaseHelper {
+ (db.getCore() instanceof MySQLCore ? " LIMIT 1" : ""));
}
public static void insertSchedule(String argUUID, String world, int x, int y, int z, long l) {
connectCheck();
String scheduleq = "INSERT INTO schedule (owner, world, x, y, z, timestamp) VALUES (?, ?, ?, ?, ?, ?)";
QuickShop.instance.getDB().execute(scheduleq , argUUID, world, x, y, z, System.currentTimeMillis());
}
public static void updateOwner2UUID(String ownerUUID, int x, int y, int z, String worldName) throws SQLException {
connectCheck();
QuickShop.instance.getDB().getConnection().createStatement()
.executeUpdate("UPDATE shops SET owner = \"" + ownerUUID.toString()
+ "\" WHERE x = " + x + " AND y = " + y + " AND z = " + z
@@ -110,11 +121,13 @@ public class DatabaseHelper {
public static void updateShop(Database db, String owner, ItemStack item, int unlimited, int shopType,
double price, int x, int y, int z, String world) {
connectCheck();
String q = "UPDATE shops SET owner = ?, itemConfig = ?, unlimited = ?, type = ?, price = ? WHERE x = ? AND y = ? and z = ? and world = ?";
db.execute(q, owner, Util.serialize(item), unlimited, shopType, price, x, y, z, world);
}
public static void createShop(String owner, double price, ItemStack item, int unlimited, int shopType, String world, int x, int y, int z) {
connectCheck();
String q = "INSERT INTO shops (owner, price, itemConfig, x, y, z, world, unlimited, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
QuickShop.instance.getDB().execute(q, owner, price, Util.serialize(item), x, y, z, world, unlimited, shopType);
// Reremake write in schedule data
@@ -122,4 +135,43 @@ public class DatabaseHelper {
QuickShop.instance.getDB().execute(scheduleq , owner, world,x,y,z, System.currentTimeMillis());
}
public static void connectCheck(){
if(!QuickShop.instance.getConfig().getBoolean("database.reconnect")) {
return;
}
try {
if(QuickShop.instance.getDB().getConnection().isClosed())
QuickShop.instance.getLogger().severe("Database connection lost, Reconnecting...");
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
if(QuickShop.instance.getDB().getConnection().isReadOnly()) {
QuickShop.instance.getLogger().severe("Database is read-only, QSRR can't write in data!");
return;
}
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
try {
if (!QuickShop.instance.getDB().getConnection().isValid(10)) {
boolean result = QuickShop.instance.setupDatabase(); // Reconnect
if(result) {
//Failed reconnect.
QuickShop.instance.getLogger().severe("Failed to reconnect.");
}else {
QuickShop.instance.getLogger().severe("Reconnected.");
connectCheck(); //Recall to check
}
}
} catch (AbstractMethodError e) {
// You don't need to validate this core.
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@@ -94,6 +94,7 @@ public class QuickShop extends JavaPlugin {
MultiverseCore mPlugin = null;
private int displayItemCheckTicks;
private boolean noopDisable;
private boolean setupDBonEnableding = false;
/** The plugin metrics from Hidendra */
// public Metrics getMetrics(){ return metrics; }
public int getShopLimit(Player p) {
@@ -227,35 +228,9 @@ public class QuickShop extends JavaPlugin {
}
getLogger().info(limits.toString());
}
try {
ConfigurationSection dbCfg = getConfig().getConfigurationSection("database");
DatabaseCore dbCore;
if (dbCfg.getBoolean("mysql")) {
// MySQL database - Required database be created first.
String user = dbCfg.getString("user");
String pass = dbCfg.getString("password");
String host = dbCfg.getString("host");
String port = dbCfg.getString("port");
String database = dbCfg.getString("database");
dbCore = new MySQLCore(host, user, pass, database, port);
} else {
// SQLite database - Doing this handles file creation
dbCore = new SQLiteCore(new File(this.getDataFolder(), "shops.db"));
}
this.database = new Database(dbCore);
// Make the database up to date
DatabaseHelper.setup(getDB());
} catch (ConnectionException e) {
e.printStackTrace();
getLogger().severe("Error connecting to database. Aborting plugin load.");
getServer().getPluginManager().disablePlugin(this);
return;
} catch (SQLException e) {
e.printStackTrace();
getLogger().severe("Error setting up database. Aborting plugin load.");
getServer().getPluginManager().disablePlugin(this);
return;
}
setupDBonEnableding = true;
setupDatabase();
setupDBonEnableding = false;
/* Load shops from database to memory */
int count = 0; // Shops count
try {
@@ -699,6 +674,48 @@ public class QuickShop extends JavaPlugin {
UpdateWatcher.init();
}
public boolean setupDatabase() {
try {
ConfigurationSection dbCfg = getConfig().getConfigurationSection("database");
DatabaseCore dbCore;
if (dbCfg.getBoolean("mysql")) {
// MySQL database - Required database be created first.
String user = dbCfg.getString("user");
String pass = dbCfg.getString("password");
String host = dbCfg.getString("host");
String port = dbCfg.getString("port");
String database = dbCfg.getString("database");
dbCore = new MySQLCore(host, user, pass, database, port);
} else {
// SQLite database - Doing this handles file creation
dbCore = new SQLiteCore(new File(this.getDataFolder(), "shops.db"));
}
this.database = new Database(dbCore);
// Make the database up to date
DatabaseHelper.setup(getDB());
} catch (ConnectionException e) {
e.printStackTrace();
if(setupDBonEnableding) {
getLogger().severe("Error connecting to database. Aborting plugin load.");
getServer().getPluginManager().disablePlugin(this);
}else {
getLogger().severe("Error connecting to database.");
}
return false;
} catch (SQLException e) {
e.printStackTrace();
getServer().getPluginManager().disablePlugin(this);
if(setupDBonEnableding) {
getLogger().severe("Error setting up database. Aborting plugin load.");
getServer().getPluginManager().disablePlugin(this);
}else {
getLogger().severe("Error setting up database.");
}
return false;
}
return true;
}
public String boolean2String(boolean bool) {
if (bool) {
return "Enabled";