From 200ff01cd9d8e2931cba7c620d6ce379273599f3 Mon Sep 17 00:00:00 2001 From: DNx5 Date: Sun, 6 Sep 2015 22:24:23 +0700 Subject: [PATCH] never return null, just throw exception. --- src/main/java/fr/xephi/authme/AuthMe.java | 7 ++- .../java/fr/xephi/authme/ConsoleLogger.java | 7 ++- .../xephi/authme/datasource/DataSource.java | 5 +-- .../fr/xephi/authme/datasource/MySQL.java | 43 ++++++++----------- 4 files changed, 26 insertions(+), 36 deletions(-) diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 131991dcf..9f7834abf 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -626,9 +626,8 @@ public class AuthMe extends JavaPlugin { if (multiverse != null && Settings.multiverse) { try { return multiverse.getMVWorldManager().getMVWorld(world).getSpawnLocation(); - } catch (NullPointerException npe) { - } catch (ClassCastException cce) { - } catch (NoClassDefFoundError ncdfe) { + } catch (Exception e) { + e.printStackTrace(); } } return null; @@ -792,7 +791,7 @@ public class AuthMe extends JavaPlugin { /** * Get Player real IP through VeryGames method * - * @param Player + * @param player * player */ @Deprecated diff --git a/src/main/java/fr/xephi/authme/ConsoleLogger.java b/src/main/java/fr/xephi/authme/ConsoleLogger.java index 3a73ab5c1..010b66192 100644 --- a/src/main/java/fr/xephi/authme/ConsoleLogger.java +++ b/src/main/java/fr/xephi/authme/ConsoleLogger.java @@ -1,5 +1,8 @@ package fr.xephi.authme; +import fr.xephi.authme.settings.Settings; +import org.bukkit.Bukkit; + import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; @@ -8,10 +11,6 @@ import java.text.DateFormat; import java.util.Calendar; import java.util.logging.Logger; -import org.bukkit.Bukkit; - -import fr.xephi.authme.settings.Settings; - public class ConsoleLogger { private static final Logger log = Logger.getLogger("AuthMe"); diff --git a/src/main/java/fr/xephi/authme/datasource/DataSource.java b/src/main/java/fr/xephi/authme/datasource/DataSource.java index ef445dc4c..41dc8923d 100644 --- a/src/main/java/fr/xephi/authme/datasource/DataSource.java +++ b/src/main/java/fr/xephi/authme/datasource/DataSource.java @@ -1,13 +1,12 @@ package fr.xephi.authme.datasource; -import java.util.List; - import fr.xephi.authme.cache.auth.PlayerAuth; +import java.util.List; + public interface DataSource { public enum DataSourceType { - MYSQL, FILE, SQLITE diff --git a/src/main/java/fr/xephi/authme/datasource/MySQL.java b/src/main/java/fr/xephi/authme/datasource/MySQL.java index 24e37dbcd..15e5eb8e2 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQL.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQL.java @@ -1,24 +1,18 @@ package fr.xephi.authme.datasource; -import java.sql.Blob; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.List; - import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.pool.PoolInitializationException; - import fr.xephi.authme.AuthMe; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.security.HashAlgorithm; import fr.xephi.authme.settings.Settings; +import java.sql.*; +import java.util.ArrayList; +import java.util.List; + public class MySQL implements DataSource { private String host; @@ -43,6 +37,7 @@ public class MySQL implements DataSource { private List columnOthers; private HikariDataSource ds; private String columnRealName; + private Connection connection; public MySQL() throws ClassNotFoundException, SQLException, PoolInitializationException { this.host = Settings.getMySQLHost; @@ -135,35 +130,30 @@ public class MySQL implements DataSource { config.setPassword(this.password); config.addDataSourceProperty("cachePrepStmts", "true"); config.addDataSourceProperty("prepStmtCacheSize", "250"); - config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); + config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); config.addDataSourceProperty("autoReconnect", false); config.setInitializationFailFast(true); // Don't start the plugin if the database is unavariable config.setMaxLifetime(180000); // 3 Min config.setIdleTimeout(60000); // 1 Min - config.setMaximumPoolSize(50); // 50 (including idle connections) + config.setMaximumPoolSize(50); // 50 (including idle connections) ds = new HikariDataSource(config); ConsoleLogger.info("Connection arguments loaded, Hikari ConnectionPool ready!"); } private synchronized void reloadArguments() throws ClassNotFoundException, IllegalArgumentException { - if (ds != null){ + if (ds != null) { ds.close(); } setConnectionArguments(); ConsoleLogger.info("Hikari ConnectionPool arguments reloaded!"); } - private synchronized Connection getConnection() { - Connection con = null; - while(con == null){ - try { - con = ds.getConnection(); - } catch (SQLException ce) { - return null; - } + private synchronized Connection getConnection() throws SQLException { + if (connection == null || connection.isClosed()) { + connection = ds.getConnection(); } - return con; + return connection; } private synchronized void setupConnection() throws SQLException { @@ -268,7 +258,8 @@ public class MySQL implements DataSource { if (!columnSalt.isEmpty()) { if (!columnGroup.isEmpty()) pAuth = new PlayerAuth(rs.getString(columnName).toLowerCase(), rs.getString(columnPassword), rs.getString(columnSalt), rs.getInt(columnGroup), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); - else pAuth = new PlayerAuth(rs.getString(columnName).toLowerCase(), rs.getString(columnPassword), rs.getString(columnSalt), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); + else + pAuth = new PlayerAuth(rs.getString(columnName).toLowerCase(), rs.getString(columnPassword), rs.getString(columnSalt), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); } else { pAuth = new PlayerAuth(rs.getString(columnName).toLowerCase(), rs.getString(columnPassword), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); } @@ -1047,7 +1038,8 @@ public class MySQL implements DataSource { if (!columnSalt.isEmpty()) { if (!columnGroup.isEmpty()) pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), rs.getString(columnSalt), rs.getInt(columnGroup), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); - else pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), rs.getString(columnSalt), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); + else + pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), rs.getString(columnSalt), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); } else { pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); } @@ -1098,7 +1090,8 @@ public class MySQL implements DataSource { if (!columnSalt.isEmpty()) { if (!columnGroup.isEmpty()) pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), rs.getString(columnSalt), rs.getInt(columnGroup), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); - else pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), rs.getString(columnSalt), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); + else + pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), rs.getString(columnSalt), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); } else { pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); }