From f39b1d145ce1ecb4dbfacd7070f25de759b19133 Mon Sep 17 00:00:00 2001 From: Vladislav <42325421+dayofpay@users.noreply.github.com> Date: Sun, 3 Sep 2023 10:41:42 +0300 Subject: [PATCH] Fix MySQL Error com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was ... milliseconds ago --- src/subside/plugins/koth/datatable/MySQL.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/subside/plugins/koth/datatable/MySQL.java b/src/subside/plugins/koth/datatable/MySQL.java index 937b3ce..4dc7fea 100644 --- a/src/subside/plugins/koth/datatable/MySQL.java +++ b/src/subside/plugins/koth/datatable/MySQL.java @@ -15,21 +15,23 @@ public class MySQL implements IDatabase { public MySQL(KothPlugin plugin){ this.plugin = plugin; } - + public Connection getConnection() throws SQLException { - if(connection != null && !connection.isClosed()) + if (connection != null && !connection.isClosed()) return connection; - + try { Class.forName("com.mysql.jdbc.Driver"); ConfigHandler.Database cDB = plugin.getConfigHandler().getDatabase(); - String url = "//" + cDB.getHost() + ":" + cDB.getPort() + "/" + cDB.getDatabase(); - this.connection = DriverManager.getConnection("jdbc:mysql:" + url, cDB.getUsername(), cDB.getPassword()); + String url = "//" + cDB.getHost() + ":" + cDB.getPort() + "/" + cDB.getDatabase(); + // Add autoReconnect=true to the JDBC URL + this.connection = DriverManager.getConnection("jdbc:mysql:" + url + "?autoReconnect=true", cDB.getUsername(), cDB.getPassword()); return this.connection; } catch (ClassNotFoundException e) { plugin.getLogger().log(Level.SEVERE, "Couldn't find the MySQL library!", e); } - + return null; } + }