From 3c0e6e06c70dc9af1b5c34c49ba24a4cefc99139 Mon Sep 17 00:00:00 2001 From: vanhec_a Date: Mon, 12 Oct 2015 14:57:56 +0200 Subject: [PATCH] Fix some case when connection is not available --- src/main/java/fr/xephi/authme/AuthMe.java | 10 +- .../fr/xephi/authme/datasource/MySQL.java | 100 +++++++++++++----- 2 files changed, 76 insertions(+), 34 deletions(-) diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index be4c104b..98b8f1c2 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -342,11 +342,6 @@ public class AuthMe extends JavaPlugin { } } - // Close the database - if (database != null) { - database.close(); - } - // Do backup on stop if enabled if (Settings.isBackupActivated && Settings.isBackupOnStop) { boolean Backup = new PerformBackup(this).doBackup(); @@ -358,6 +353,11 @@ public class AuthMe extends JavaPlugin { // Unload modules moduleManager.unloadModules(); + // Close the database + if (database != null) { + database.close(); + } + // Disabled correctly ConsoleLogger.info("AuthMe " + this.getDescription().getVersion() + " disabled!"); } diff --git a/src/main/java/fr/xephi/authme/datasource/MySQL.java b/src/main/java/fr/xephi/authme/datasource/MySQL.java index 8e5d4cc1..30eba235 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQL.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQL.java @@ -13,6 +13,8 @@ import java.sql.*; import java.util.ArrayList; import java.util.List; +import org.bukkit.Bukkit; + public class MySQL implements DataSource { private String host; @@ -122,9 +124,24 @@ public class MySQL implements DataSource { } private synchronized Connection getConnection() throws SQLException { - Connection con; - con = ds.getConnection(); - return con; + if (!ds.isClosed()) + { + Connection con; + con = ds.getConnection(); + return con; + } + ConsoleLogger.showError("Can't open a database connection!"); + if (Settings.isStopEnabled) + { + ConsoleLogger.showError("Server will ShuttingDown for Security reason"); + Bukkit.getScheduler().scheduleSyncDelayedTask(AuthMe.getInstance(), new Runnable(){ + @Override + public void run() { + AuthMe.getInstance().getServer().shutdown(); + } + }); + } + return null; } private synchronized void setupConnection() throws SQLException { @@ -132,7 +149,8 @@ public class MySQL implements DataSource { Statement st = null; ResultSet rs = null; try { - con = getConnection(); + if ((con = getConnection()) == null) + return; st = con.createStatement(); st.executeUpdate("CREATE TABLE IF NOT EXISTS " + tableName + " (" + columnID + " INTEGER AUTO_INCREMENT," + columnName + " VARCHAR(255) NOT NULL UNIQUE," + columnPassword + " VARCHAR(255) NOT NULL," + columnIp + " VARCHAR(40) NOT NULL DEFAULT '127.0.0.1'," + columnLastLogin + " BIGINT NOT NULL DEFAULT '" + System.currentTimeMillis() + "'," + lastlocX + " DOUBLE NOT NULL DEFAULT '0.0'," + lastlocY + " DOUBLE NOT NULL DEFAULT '0.0'," + lastlocZ + " DOUBLE NOT NULL DEFAULT '0.0'," + lastlocWorld + " VARCHAR(255) NOT NULL DEFAULT '" + Settings.defaultWorld + "'," + columnEmail + " VARCHAR(255) DEFAULT 'your@email.com'," + columnLogged + " SMALLINT NOT NULL DEFAULT '0'," + "CONSTRAINT table_const_prim PRIMARY KEY (" + columnID + "));"); rs = con.getMetaData().getColumns(null, null, tableName, columnPassword); @@ -193,7 +211,8 @@ public class MySQL implements DataSource { PreparedStatement pst = null; ResultSet rs = null; try { - con = getConnection(); + if ((con = getConnection()) == null) + return true; pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + columnName + ")=LOWER(?);"); pst.setString(1, user); rs = pst.executeQuery(); @@ -216,7 +235,8 @@ public class MySQL implements DataSource { PlayerAuth pAuth = null; int id; try { - con = getConnection(); + if ((con = getConnection()) == null) + return null; pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + columnName + ")=LOWER(?);"); pst.setString(1, user); rs = pst.executeQuery(); @@ -266,7 +286,8 @@ public class MySQL implements DataSource { PreparedStatement pst = null; ResultSet rs = null; try { - con = getConnection(); + if ((con = getConnection()) == null) + return false; if ((columnSalt == null || columnSalt.isEmpty()) || (auth.getSalt() == null || auth.getSalt().isEmpty())) { pst = con.prepareStatement("INSERT INTO " + tableName + "(" + columnName + "," + columnPassword + "," + columnIp + "," + columnLastLogin + "," + columnRealName + ") VALUES (?,?,?,?,?);"); pst.setString(1, auth.getNickname()); @@ -472,7 +493,8 @@ public class MySQL implements DataSource { PreparedStatement pst = null; ResultSet rs = null; try { - con = getConnection(); + if ((con = getConnection()) == null) + return false; pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnPassword + "=? WHERE LOWER(" + columnName + ")=?;"); pst.setString(1, auth.getHash()); pst.setString(2, auth.getNickname()); @@ -515,7 +537,8 @@ public class MySQL implements DataSource { Connection con = null; PreparedStatement pst = null; try { - con = getConnection(); + if ((con = getConnection()) == null) + return false; pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnIp + "=?, " + columnLastLogin + "=?, " + columnRealName + "=? WHERE LOWER(" + columnName + ")=?;"); pst.setString(1, auth.getIp()); pst.setLong(2, auth.getLastLogin()); @@ -537,7 +560,8 @@ public class MySQL implements DataSource { Connection con = null; PreparedStatement pst = null; try { - con = getConnection(); + if ((con = getConnection()) == null) + return 0; pst = con.prepareStatement("DELETE FROM " + tableName + " WHERE " + columnLastLogin + " list = new ArrayList<>(); try { - con = getConnection(); + if ((con = getConnection()) == null) + return list; pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnLastLogin + " countIp = new ArrayList<>(); try { - con = getConnection(); + if ((con = getConnection()) == null) + return countIp; pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;"); pst.setString(1, auth.getIp()); rs = pst.executeQuery(); @@ -769,7 +800,8 @@ public class MySQL implements DataSource { ResultSet rs = null; List countIp = new ArrayList<>(); try { - con = getConnection(); + if ((con = getConnection()) == null) + return countIp; pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;"); pst.setString(1, ip); rs = pst.executeQuery(); @@ -794,7 +826,8 @@ public class MySQL implements DataSource { ResultSet rs = null; List countEmail = new ArrayList<>(); try { - con = getConnection(); + if ((con = getConnection()) == null) + return countEmail; pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnEmail + "=?;"); pst.setString(1, email); rs = pst.executeQuery(); @@ -817,8 +850,9 @@ public class MySQL implements DataSource { Connection con = null; PreparedStatement pst = null; try { + if ((con = getConnection()) == null) + return; for (String name : banned) { - con = getConnection(); pst = con.prepareStatement("DELETE FROM " + tableName + " WHERE LOWER(" + columnName + ")=?;"); pst.setString(1, name); pst.executeUpdate(); @@ -842,7 +876,8 @@ public class MySQL implements DataSource { PreparedStatement pst = null; ResultSet rs = null; try { - con = getConnection(); + if ((con = getConnection()) == null) + return false; pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + columnName + ")=?;"); pst.setString(1, user); rs = pst.executeQuery(); @@ -864,7 +899,8 @@ public class MySQL implements DataSource { Connection con = null; PreparedStatement pst = null; try { - con = getConnection(); + if ((con = getConnection()) == null) + return; pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnLogged + "=? WHERE LOWER(" + columnName + ")=?;"); pst.setInt(1, 1); pst.setString(2, user); @@ -883,7 +919,8 @@ public class MySQL implements DataSource { PreparedStatement pst = null; if (user != null) try { - con = getConnection(); + if ((con = getConnection()) == null) + return; pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnLogged + "=? WHERE LOWER(" + columnName + ")=?;"); pst.setInt(1, 0); pst.setString(2, user); @@ -901,7 +938,8 @@ public class MySQL implements DataSource { Connection con = null; PreparedStatement pst = null; try { - con = getConnection(); + if ((con = getConnection()) == null) + return; pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnLogged + "=? WHERE " + columnLogged + "=?;"); pst.setInt(1, 0); pst.setInt(2, 1); @@ -921,7 +959,8 @@ public class MySQL implements DataSource { PreparedStatement pst = null; ResultSet rs; try { - con = getConnection(); + if ((con = getConnection()) == null) + return result; pst = con.prepareStatement("SELECT COUNT(*) FROM " + tableName + ";"); rs = pst.executeQuery(); if (rs != null && rs.next()) { @@ -942,7 +981,8 @@ public class MySQL implements DataSource { Connection con = null; PreparedStatement pst = null; try { - con = getConnection(); + if ((con = getConnection()) == null) + return; pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnName + "=? WHERE LOWER(" + columnName + ")=?;"); pst.setString(1, newone); pst.setString(2, oldone); @@ -962,7 +1002,8 @@ public class MySQL implements DataSource { PreparedStatement pst = null; ResultSet rs = null; try { - con = getConnection(); + if ((con = getConnection()) == null) + return auths; pst = con.prepareStatement("SELECT * FROM " + tableName + ";"); rs = pst.executeQuery(); while (rs.next()) { @@ -1012,7 +1053,8 @@ public class MySQL implements DataSource { PreparedStatement pst = null; ResultSet rs = null; try { - con = getConnection(); + if ((con = getConnection()) == null) + return auths; pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnLogged + "=1;"); rs = pst.executeQuery(); while (rs.next()) {