From 56aca7f473beb6bc8e146a86e74cc0e66f5a7d55 Mon Sep 17 00:00:00 2001 From: DNx5 Date: Tue, 3 Nov 2015 07:37:00 +0700 Subject: [PATCH] this is enough. --- .../fr/xephi/authme/datasource/MySQL.java | 98 +++++++------------ src/main/resources/config.yml | 2 +- 2 files changed, 37 insertions(+), 63 deletions(-) diff --git a/src/main/java/fr/xephi/authme/datasource/MySQL.java b/src/main/java/fr/xephi/authme/datasource/MySQL.java index 45c0f036..8fdc2371 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQL.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQL.java @@ -1,26 +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 org.bukkit.Bukkit; - 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; @@ -108,19 +100,18 @@ public class MySQL implements DataSource { config.setJdbcUrl("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database); config.setUsername(this.username); config.setPassword(this.password); - if (Settings.isMySQLWebsite) - { + if (Settings.isMySQLWebsite) { config.addDataSourceProperty("cachePrepStmts", "false"); - } - else { + } else { config.addDataSourceProperty("cachePrepStmts", "true"); config.addDataSourceProperty("prepStmtCacheSize", "250"); config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); } config.addDataSourceProperty("autoReconnect", false); - config.setInitializationFailFast(true); // Don't start the plugin if the database is unavariable + config.setInitializationFailFast(true); // Don't start the plugin if the database is unavailable config.setMaxLifetime(180000); // 3 Min config.setIdleTimeout(60000); // 1 Min + config.setMinimumIdle(2); config.setMaximumPoolSize(maxConnections); ds = new HikariDataSource(config); ConsoleLogger.info("Connection arguments loaded, Hikari ConnectionPool ready!"); @@ -136,24 +127,7 @@ public class MySQL implements DataSource { } private synchronized Connection getConnection() throws SQLException { - 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; + return ds.getConnection(); } private synchronized void setupConnection() throws SQLException { @@ -162,7 +136,7 @@ public class MySQL implements DataSource { ResultSet rs = null; try { if ((con = getConnection()) == null) - return; + 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); @@ -210,7 +184,7 @@ public class MySQL implements DataSource { st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + columnRealName + " VARCHAR(255) NOT NULL DEFAULT 'Player' AFTER " + columnLogged + ";"); } if (Settings.isMySQLWebsite) - st.execute("SET GLOBAL query_cache_size = 0; SET GLOBAL query_cache_type = 0;"); + st.execute("SET GLOBAL query_cache_size = 0; SET GLOBAL query_cache_type = 0;"); } finally { close(rs); close(st); @@ -226,7 +200,7 @@ public class MySQL implements DataSource { ResultSet rs = null; try { if ((con = getConnection()) == null) - return true; + return true; pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + columnName + ")=LOWER(?);"); pst.setString(1, user); rs = pst.executeQuery(); @@ -250,7 +224,7 @@ public class MySQL implements DataSource { int id; try { if ((con = getConnection()) == null) - return null; + return null; pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + columnName + ")=LOWER(?);"); pst.setString(1, user); rs = pst.executeQuery(); @@ -301,7 +275,7 @@ public class MySQL implements DataSource { ResultSet rs = null; try { if ((con = getConnection()) == null) - return false; + 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()); @@ -508,7 +482,7 @@ public class MySQL implements DataSource { ResultSet rs = null; try { if ((con = getConnection()) == null) - return false; + return false; pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnPassword + "=? WHERE LOWER(" + columnName + ")=?;"); pst.setString(1, auth.getHash()); pst.setString(2, auth.getNickname()); @@ -552,7 +526,7 @@ public class MySQL implements DataSource { PreparedStatement pst = null; try { if ((con = getConnection()) == null) - return false; + return false; pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnIp + "=?, " + columnLastLogin + "=?, " + columnRealName + "=? WHERE LOWER(" + columnName + ")=?;"); pst.setString(1, auth.getIp()); pst.setLong(2, auth.getLastLogin()); @@ -575,7 +549,7 @@ public class MySQL implements DataSource { PreparedStatement pst = null; try { if ((con = getConnection()) == null) - return 0; + return 0; pst = con.prepareStatement("DELETE FROM " + tableName + " WHERE " + columnLastLogin + " list = new ArrayList<>(); try { if ((con = getConnection()) == null) - return list; + return list; pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnLastLogin + " countIp = new ArrayList<>(); try { if ((con = getConnection()) == null) - return countIp; + return countIp; pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;"); pst.setString(1, auth.getIp()); rs = pst.executeQuery(); @@ -815,7 +789,7 @@ public class MySQL implements DataSource { List countIp = new ArrayList<>(); try { if ((con = getConnection()) == null) - return countIp; + return countIp; pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;"); pst.setString(1, ip); rs = pst.executeQuery(); @@ -841,7 +815,7 @@ public class MySQL implements DataSource { List countEmail = new ArrayList<>(); try { if ((con = getConnection()) == null) - return countEmail; + return countEmail; pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnEmail + "=?;"); pst.setString(1, email); rs = pst.executeQuery(); @@ -865,7 +839,7 @@ public class MySQL implements DataSource { PreparedStatement pst = null; try { if ((con = getConnection()) == null) - return; + return; for (String name : banned) { pst = con.prepareStatement("DELETE FROM " + tableName + " WHERE LOWER(" + columnName + ")=?;"); pst.setString(1, name); @@ -891,7 +865,7 @@ public class MySQL implements DataSource { ResultSet rs = null; try { if ((con = getConnection()) == null) - return false; + return false; pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + columnName + ")=?;"); pst.setString(1, user); rs = pst.executeQuery(); @@ -914,7 +888,7 @@ public class MySQL implements DataSource { PreparedStatement pst = null; try { if ((con = getConnection()) == null) - return; + return; pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnLogged + "=? WHERE LOWER(" + columnName + ")=?;"); pst.setInt(1, 1); pst.setString(2, user); @@ -934,7 +908,7 @@ public class MySQL implements DataSource { if (user != null) try { if ((con = getConnection()) == null) - return; + return; pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnLogged + "=? WHERE LOWER(" + columnName + ")=?;"); pst.setInt(1, 0); pst.setString(2, user); @@ -953,7 +927,7 @@ public class MySQL implements DataSource { PreparedStatement pst = null; try { if ((con = getConnection()) == null) - return; + return; pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnLogged + "=? WHERE " + columnLogged + "=?;"); pst.setInt(1, 0); pst.setInt(2, 1); @@ -974,7 +948,7 @@ public class MySQL implements DataSource { ResultSet rs; try { if ((con = getConnection()) == null) - return result; + return result; pst = con.prepareStatement("SELECT COUNT(*) FROM " + tableName + ";"); rs = pst.executeQuery(); if (rs != null && rs.next()) { @@ -996,7 +970,7 @@ public class MySQL implements DataSource { PreparedStatement pst = null; try { if ((con = getConnection()) == null) - return; + return; pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnName + "=? WHERE LOWER(" + columnName + ")=?;"); pst.setString(1, newone); pst.setString(2, oldone); @@ -1017,7 +991,7 @@ public class MySQL implements DataSource { ResultSet rs = null; try { if ((con = getConnection()) == null) - return auths; + return auths; pst = con.prepareStatement("SELECT * FROM " + tableName + ";"); rs = pst.executeQuery(); while (rs.next()) { @@ -1068,7 +1042,7 @@ public class MySQL implements DataSource { ResultSet rs = null; try { if ((con = getConnection()) == null) - return auths; + return auths; pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnLogged + "=1;"); rs = pst.executeQuery(); while (rs.next()) { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index e991efc0..671abc50 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -10,7 +10,7 @@ DataSource: # Database Port mySQLPort: '3306' # MySql Max Connections - mySQLMaxConections: 25 + mySQLMaxConections: 8 # Username about Database Connection Infos mySQLUsername: authme # Password about Database Connection Infos