Browse Source

ensure database connection

Kajetan Johannes Hammerle 3 years ago
parent
commit
5d473bed5c

+ 1 - 1
.gitignore

@@ -1,4 +1,4 @@
 .vscode
 build
 lib
-uploadMod.sh
+upload.sh

+ 81 - 13
src/me/hammerle/kp/Database.java

@@ -3,31 +3,42 @@ package me.hammerle.kp;
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.PreparedStatement;
+import java.sql.ResultSet;
 import java.sql.SQLException;
-import java.sql.Statement;
 
 public class Database {
     private static Connection connection = null;
+    private static String user;
+    private static String password;
+    private static int tries = 0;
 
-    public static boolean connect(String user, String password) {
+    private static boolean connect() {
         try {
+            tries++;
+            if(tries > 10) {
+                KajetansPlugin.warn("Too many database connect fails");
+                return false;
+            }
+            if(connection != null) {
+                connection.close();
+            }
             connection = DriverManager
                     .getConnection("jdbc:mysql://localhost/minecraft?useSSL=false", user, password);
             KajetansPlugin.log("Connection to database etablished");
-            KajetansPlugin.scheduleRepeatingTask(() -> {
-                try(Statement stmt = connection.createStatement()) {
-                    stmt.executeUpdate("SHOW TABLES IN minecraft");
-                } catch(SQLException ex) {
-                    KajetansPlugin.log("reconnect was done");
-                }
-            }, 100, 12000); // doing this every 10 minutes
+            tries--;
             return true;
         } catch(Exception ex) {
-            KajetansPlugin.log(ex.getMessage());
+            KajetansPlugin.warn(ex.getMessage());
             return false;
         }
     }
 
+    public static boolean connect(String user, String password) {
+        Database.user = user;
+        Database.password = password;
+        return connect();
+    }
+
     public static void close() {
         if(connection == null) {
             return;
@@ -36,11 +47,68 @@ public class Database {
             connection.close();
             KajetansPlugin.log("Connection to database was closed");
         } catch(SQLException ex) {
-            KajetansPlugin.log(ex.getMessage());
+            KajetansPlugin.warn(ex.getMessage());
+        }
+    }
+
+    public static class SafeStatement implements AutoCloseable {
+        private String query;
+        private PreparedStatement statement;
+
+        public SafeStatement(String query) throws SQLException {
+            this.query = query;
+            statement = connection.prepareStatement(query);
+        }
+
+        private void reconnect() throws SQLException {
+            if(statement.isClosed() && connection.isClosed()) {
+                connect();
+                statement = connection.prepareStatement(query);
+            }
+        }
+
+        @Override
+        public void close() throws SQLException {
+            statement.close();
+        }
+
+        public void setInt(int index, int i) throws SQLException {
+            reconnect();
+            statement.setInt(index, i);
+        }
+
+        public void setLong(int index, long l) throws SQLException {
+            reconnect();
+            statement.setLong(index, l);
+        }
+
+        public void setDouble(int index, double d) throws SQLException {
+            reconnect();
+            statement.setDouble(index, d);
+        }
+
+        public void setString(int index, String s) throws SQLException {
+            reconnect();
+            statement.setString(index, s);
+        }
+
+        public void setBoolean(int index, boolean b) throws SQLException {
+            reconnect();
+            statement.setBoolean(index, b);
+        }
+
+        public ResultSet executeQuery() throws SQLException {
+            reconnect();
+            return statement.executeQuery();
+        }
+
+        public void execute() throws SQLException {
+            reconnect();
+            statement.execute();
         }
     }
 
-    public static PreparedStatement prepare(String query) throws SQLException {
-        return connection.prepareStatement(query);
+    public static SafeStatement prepare(String query) throws SQLException {
+        return new SafeStatement(query);
     }
 }

+ 3 - 0
src/me/hammerle/kp/snuviscript/CommandScript.java

@@ -61,6 +61,9 @@ public class CommandScript extends KajetanCommand {
                 try {
                     if(args[1].equals("all")) {
                         KajetansPlugin.scriptManager.removeScripts();
+                        CommandManager.clearCustom();
+                        CommandManager.clearIgnored();
+                        CommandManager.clearCustomNodes();
                         sendMessage(cs, "All active scripts were terminated.");
                         return;
                     }

+ 5 - 0
src/me/hammerle/kp/snuviscript/commands/CommandCommands.java

@@ -5,6 +5,7 @@ import me.hammerle.kp.snuviscript.CommandManager;
 import com.mojang.brigadier.arguments.*;
 import com.mojang.brigadier.builder.*;
 import org.bukkit.Bukkit;
+import org.bukkit.entity.Player;
 import net.minecraft.commands.CommandDispatcher;
 import net.minecraft.commands.CommandListenerWrapper;
 import net.minecraft.commands.arguments.ArgumentEnchantment;
@@ -134,6 +135,10 @@ public class CommandCommands {
             return arg;
         });
         KajetansPlugin.scriptManager.registerConsumer("command.sendhelp", (sc, in) -> {
+            if(in.length > 0) {
+                CommandManager.send((Player) in[0].get(sc));
+                return;
+            }
             Bukkit.getServer().getOnlinePlayers().forEach(p -> CommandManager.send(p));
         });
         KajetansPlugin.scriptManager.registerConsumer("command.addhelpalias", (sc, in) -> {

+ 11 - 9
src/me/hammerle/kp/snuviscript/commands/DatabaseCommands.java

@@ -1,6 +1,5 @@
 package me.hammerle.kp.snuviscript.commands;
 
-import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import me.hammerle.kp.Database;
@@ -10,26 +9,29 @@ import me.hammerle.snuviscript.exceptions.StackTrace;
 public class DatabaseCommands {
     public static void registerFunctions() {
         KajetansPlugin.scriptManager.registerFunction("databank.prepare", (sc, in) -> {
-            PreparedStatement p = Database.prepare(in[0].getString(sc));
+            Database.SafeStatement p = Database.prepare(in[0].getString(sc));
             if(in.length <= 1 || in[1].getBoolean(sc)) {
                 sc.addCloseable(p);
             }
             return p;
         });
         KajetansPlugin.scriptManager.registerConsumer("databank.setint", (sc, in) -> {
-            ((PreparedStatement) in[0].get(sc)).setInt(in[1].getInt(sc), in[2].getInt(sc));
+            ((Database.SafeStatement) in[0].get(sc)).setInt(in[1].getInt(sc), in[2].getInt(sc));
         });
         KajetansPlugin.scriptManager.registerConsumer("databank.setlong", (sc, in) -> {
-            ((PreparedStatement) in[0].get(sc)).setLong(in[1].getInt(sc), in[2].getLong(sc));
+            ((Database.SafeStatement) in[0].get(sc)).setLong(in[1].getInt(sc), in[2].getLong(sc));
         });
         KajetansPlugin.scriptManager.registerConsumer("databank.setdouble", (sc, in) -> {
-            ((PreparedStatement) in[0].get(sc)).setDouble(in[1].getInt(sc), in[2].getDouble(sc));
+            ((Database.SafeStatement) in[0].get(sc)).setDouble(in[1].getInt(sc),
+                    in[2].getDouble(sc));
         });
         KajetansPlugin.scriptManager.registerConsumer("databank.setstring", (sc, in) -> {
-            ((PreparedStatement) in[0].get(sc)).setString(in[1].getInt(sc), in[2].getString(sc));
+            ((Database.SafeStatement) in[0].get(sc)).setString(in[1].getInt(sc),
+                    in[2].getString(sc));
         });
         KajetansPlugin.scriptManager.registerConsumer("databank.setbool", (sc, in) -> {
-            ((PreparedStatement) in[0].get(sc)).setBoolean(in[1].getInt(sc), in[2].getBoolean(sc));
+            ((Database.SafeStatement) in[0].get(sc)).setBoolean(in[1].getInt(sc),
+                    in[2].getBoolean(sc));
         });
         KajetansPlugin.scriptManager.registerFunction("databank.getint",
                 (sc, in) -> (double) ((ResultSet) in[0].get(sc)).getInt(in[1].getInt(sc)));
@@ -42,9 +44,9 @@ public class DatabaseCommands {
         KajetansPlugin.scriptManager.registerFunction("databank.getbool",
                 (sc, in) -> ((ResultSet) in[0].get(sc)).getBoolean(in[1].getInt(sc)));
         KajetansPlugin.scriptManager.registerFunction("databank.execute",
-                (sc, in) -> ((PreparedStatement) in[0].get(sc)).executeQuery());
+                (sc, in) -> ((Database.SafeStatement) in[0].get(sc)).executeQuery());
         KajetansPlugin.scriptManager.registerConsumer("databank.workerexecute", (sc, in) -> {
-            final PreparedStatement p = (PreparedStatement) in[0].get(sc);
+            final Database.SafeStatement p = (Database.SafeStatement) in[0].get(sc);
             StackTrace lines = sc.getStackTrace();
             String function = "databank.workerexecute";
             KajetansPlugin.scheduleAsyncTask(() -> {