sqlite fix, load AuthMe faster.

This commit is contained in:
DNx5 2015-09-15 16:23:56 +07:00
parent c82aaa303d
commit 6f22aa779b
2 changed files with 58 additions and 134 deletions

View File

@ -3,7 +3,6 @@ package fr.xephi.authme;
import com.earth2me.essentials.Essentials; import com.earth2me.essentials.Essentials;
import com.maxmind.geoip.LookupService; import com.maxmind.geoip.LookupService;
import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.MultiverseCore;
import com.zaxxer.hikari.pool.PoolInitializationException;
import fr.xephi.authme.api.API; import fr.xephi.authme.api.API;
import fr.xephi.authme.api.NewAPI; import fr.xephi.authme.api.NewAPI;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
@ -42,7 +41,6 @@ import org.mcstats.Metrics;
import java.io.*; import java.io.*;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.sql.SQLException;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
@ -216,8 +214,9 @@ public class AuthMe extends JavaPlugin {
// Connect to the database and setup tables // Connect to the database and setup tables
try { try {
setupDatabase(); setupDatabase();
} catch (Exception ex) { } catch (Exception e) {
ConsoleLogger.writeStackTrace(ex); ConsoleLogger.writeStackTrace(e);
ConsoleLogger.showError(e.getMessage());
ConsoleLogger.showError("Fatal error occurred during database connection! Authme initialization ABORTED!"); ConsoleLogger.showError("Fatal error occurred during database connection! Authme initialization ABORTED!");
stopOrUnload(); stopOrUnload();
return; return;
@ -353,9 +352,9 @@ public class AuthMe extends JavaPlugin {
} }
// Initialize and setup the database // Initialize and setup the database
public void setupDatabase() throws ClassNotFoundException, PoolInitializationException, SQLException { public void setupDatabase() throws Exception {
// Backend MYSQL - FILE - SQLITE - SQLITEHIKARI // Backend MYSQL - FILE - SQLITE - SQLITEHIKARI
int accounts; boolean isSQLite = false;
switch (Settings.getDataSource) { switch (Settings.getDataSource) {
case FILE: case FILE:
database = new FlatFile(); database = new FlatFile();
@ -365,18 +364,25 @@ public class AuthMe extends JavaPlugin {
break; break;
case SQLITE: case SQLITE:
database = new SQLite(); database = new SQLite();
accounts = database.getAccountsRegistered(); isSQLite = true;
if (accounts >= 4000)
ConsoleLogger.showError("YOU'RE USING THE SQLITE DATABASE WITH " + accounts + "+ ACCOUNTS, FOR BETTER PERFORMANCES, PLEASE UPGRADE TO MYSQL!!");
break; break;
case SQLITEHIKARI: case SQLITEHIKARI:
database = new SQLite_HIKARI(); database = new SQLite_HIKARI();
accounts = database.getAccountsRegistered(); isSQLite = true;
if (accounts >= 8000)
ConsoleLogger.showError("YOU'RE USING THE SQLITE DATABASE WITH " + accounts + "+ ACCOUNTS, FOR BETTER PERFORMANCES, PLEASE UPGRADE TO MYSQL!!");
break; break;
} }
if (isSQLite) {
server.getScheduler().runTaskAsynchronously(this, new Runnable() {
@Override
public void run() {
int accounts = database.getAccountsRegistered();
if (accounts >= 4000)
ConsoleLogger.showError("YOU'RE USING THE SQLITE DATABASE WITH " + accounts + "+ ACCOUNTS, FOR BETTER PERFORMANCES, PLEASE UPGRADE TO MYSQL!!");
}
});
}
if (Settings.isCachingEnabled) { if (Settings.isCachingEnabled) {
database = new CacheDataSource(this, database); database = new CacheDataSource(this, database);
} }

View File

@ -1,22 +1,16 @@
package fr.xephi.authme.datasource; package fr.xephi.authme.datasource;
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.HikariConfig;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.pool.PoolInitializationException;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class SQLite_HIKARI implements DataSource { public class SQLite_HIKARI implements DataSource {
private String database; private String database;
@ -37,7 +31,7 @@ public class SQLite_HIKARI implements DataSource {
private String columnLogged; private String columnLogged;
private String columnRealName; private String columnRealName;
public SQLite_HIKARI() throws ClassNotFoundException, SQLException, PoolInitializationException { public SQLite_HIKARI() throws ClassNotFoundException, SQLException {
this.database = Settings.getMySQLDatabase; this.database = Settings.getMySQLDatabase;
this.tableName = Settings.getMySQLTablename; this.tableName = Settings.getMySQLTablename;
this.columnName = Settings.getMySQLColumnName; this.columnName = Settings.getMySQLColumnName;
@ -55,61 +49,22 @@ public class SQLite_HIKARI implements DataSource {
this.columnLogged = Settings.getMySQLColumnLogged; this.columnLogged = Settings.getMySQLColumnLogged;
this.columnRealName = Settings.getMySQLColumnRealName; this.columnRealName = Settings.getMySQLColumnRealName;
// Set the connection arguments (and check if connection is ok) // Set the connection arguments
try { try {
this.setConnectionArguments(); this.setConnectionArguments();
} catch (ClassNotFoundException ne) { } catch (RuntimeException rt) {
ConsoleLogger.showError(ne.getMessage()); ConsoleLogger.showError("Can't use the Hikari Connection Pool! Please, report this error to the developer!");
ConsoleLogger.showError("Can't use the Hikari Connection Pool! Please, report this error to the developer! SHUTDOWN..."); throw rt;
this.close();
if (Settings.isStopEnabled) {
AuthMe.getInstance().getServer().shutdown();
AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance());
} else {
AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance());
}
throw new ClassNotFoundException(ne.getMessage());
} catch (IllegalArgumentException ae) { // This means that there are problems with the hikaricp pool arguments!
ConsoleLogger.showError(ae.getMessage());
ConsoleLogger.showError("Invalid database arguments! Please check your configuration!");
ConsoleLogger.showError("If this error persists, please report it to the developer! SHUTDOWN...");
this.close();
if (Settings.isStopEnabled) {
AuthMe.getInstance().getServer().shutdown();
AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance());
} else {
AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance());
}
throw new IllegalArgumentException(ae);
} catch (PoolInitializationException ie) { // Can't initialize the connection pool!
ConsoleLogger.showError(ie.getMessage());
ConsoleLogger.showError("Can't connect to the SQLite database! Please check your configuration!");
ConsoleLogger.showError("If this error persists, please report it to the developer! SHUTDOWN...");
this.close();
if (Settings.isStopEnabled) {
AuthMe.getInstance().getServer().shutdown();
AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance());
} else {
AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance());
}
throw new PoolInitializationException(ie);
} }
// Initialize the database // Initialize the database
try { try {
this.setupConnection(); this.setupConnection();
} catch (SQLException e) { } catch (SQLException e) {
ConsoleLogger.showError(e.getMessage()); this.close();
ConsoleLogger.showError("Can't initialize the SQLite database... Please check your database settings in the config.yml file! SHUTDOWN..."); ConsoleLogger.showError("Can't initialize the SQLite database... Please check your database settings in the config.yml file! SHUTDOWN...");
ConsoleLogger.showError("If this error persists, please report it to the developer! SHUTDOWN..."); ConsoleLogger.showError("If this error persists, please report it to the developer! SHUTDOWN...");
this.close(); throw e;
if (Settings.isStopEnabled) {
AuthMe.getInstance().getServer().shutdown();
AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance());
} else {
AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance());
}
throw new SQLException(e);
} }
} }
@ -118,11 +73,10 @@ public class SQLite_HIKARI implements DataSource {
return DataSourceType.SQLITEHIKARI; return DataSourceType.SQLITEHIKARI;
} }
private synchronized void setConnectionArguments() private synchronized void setConnectionArguments() throws RuntimeException {
throws ClassNotFoundException, IllegalArgumentException {
HikariConfig config = new HikariConfig(); HikariConfig config = new HikariConfig();
config.setPoolName("AuthMeSQLitePool"); config.setPoolName("AuthMeSQLitePool");
config.setDriverClassName("org.sqlite.JDBC"); config.setDriverClassName("org.sqlite.JDBC"); // RuntimeException
config.setJdbcUrl("jdbc:sqlite:plugins/AuthMe/" + database + ".db"); config.setJdbcUrl("jdbc:sqlite:plugins/AuthMe/" + database + ".db");
config.setConnectionTestQuery("SELECT 1"); config.setConnectionTestQuery("SELECT 1");
config.setMaxLifetime(180000); // 3 Min config.setMaxLifetime(180000); // 3 Min
@ -134,7 +88,7 @@ public class SQLite_HIKARI implements DataSource {
private synchronized void reloadArguments() private synchronized void reloadArguments()
throws ClassNotFoundException, IllegalArgumentException { throws ClassNotFoundException, IllegalArgumentException {
if (ds != null){ if (ds != null) {
ds.close(); ds.close();
} }
setConnectionArguments(); setConnectionArguments();
@ -142,9 +96,7 @@ public class SQLite_HIKARI implements DataSource {
} }
private synchronized Connection getConnection() throws SQLException { private synchronized Connection getConnection() throws SQLException {
Connection con = null; return ds.getConnection();
con = ds.getConnection();
return con;
} }
private synchronized void setupConnection() throws SQLException { private synchronized void setupConnection() throws SQLException {
@ -357,7 +309,7 @@ public class SQLite_HIKARI implements DataSource {
Connection con = null; Connection con = null;
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null; ResultSet rs = null;
List<String> list = new ArrayList<String>(); List<String> list = new ArrayList<>();
try { try {
con = getConnection(); con = getConnection();
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnLastLogin + "<?;"); pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnLastLogin + "<?;");
@ -369,7 +321,7 @@ public class SQLite_HIKARI implements DataSource {
return list; return list;
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return new ArrayList<String>(); return new ArrayList<>();
} finally { } finally {
close(rs); close(rs);
close(pst); close(pst);
@ -492,7 +444,7 @@ public class SQLite_HIKARI implements DataSource {
Connection con = null; Connection con = null;
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null; ResultSet rs = null;
List<String> countIp = new ArrayList<String>(); List<String> countIp = new ArrayList<>();
try { try {
con = getConnection(); con = getConnection();
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;"); pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;");
@ -504,9 +456,9 @@ public class SQLite_HIKARI implements DataSource {
return countIp; return countIp;
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return new ArrayList<String>(); return new ArrayList<>();
} catch (NullPointerException npe) { } catch (NullPointerException npe) {
return new ArrayList<String>(); return new ArrayList<>();
} finally { } finally {
close(rs); close(rs);
close(pst); close(pst);
@ -519,7 +471,7 @@ public class SQLite_HIKARI implements DataSource {
Connection con = null; Connection con = null;
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null; ResultSet rs = null;
List<String> countIp = new ArrayList<String>(); List<String> countIp = new ArrayList<>();
try { try {
con = getConnection(); con = getConnection();
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;"); pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;");
@ -531,9 +483,9 @@ public class SQLite_HIKARI implements DataSource {
return countIp; return countIp;
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return new ArrayList<String>(); return new ArrayList<>();
} catch (NullPointerException npe) { } catch (NullPointerException npe) {
return new ArrayList<String>(); return new ArrayList<>();
} finally { } finally {
close(rs); close(rs);
close(pst); close(pst);
@ -546,7 +498,7 @@ public class SQLite_HIKARI implements DataSource {
Connection con = null; Connection con = null;
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null; ResultSet rs = null;
List<String> countEmail = new ArrayList<String>(); List<String> countEmail = new ArrayList<>();
try { try {
con = getConnection(); con = getConnection();
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnEmail + "=?;"); pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnEmail + "=?;");
@ -558,9 +510,9 @@ public class SQLite_HIKARI implements DataSource {
return countEmail; return countEmail;
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return new ArrayList<String>(); return new ArrayList<>();
} catch (NullPointerException npe) { } catch (NullPointerException npe) {
return new ArrayList<String>(); return new ArrayList<>();
} finally { } finally {
close(rs); close(rs);
close(pst); close(pst);
@ -622,12 +574,10 @@ public class SQLite_HIKARI implements DataSource {
pst.executeUpdate(); pst.executeUpdate();
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return;
} finally { } finally {
close(pst); close(pst);
close(con); close(con);
} }
return;
} }
@Override @Override
@ -643,12 +593,10 @@ public class SQLite_HIKARI implements DataSource {
pst.executeUpdate(); pst.executeUpdate();
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return;
} finally { } finally {
close(pst); close(pst);
close(con); close(con);
} }
return;
} }
@Override @Override
@ -663,19 +611,17 @@ public class SQLite_HIKARI implements DataSource {
pst.executeUpdate(); pst.executeUpdate();
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return;
} finally { } finally {
close(pst); close(pst);
close(con); close(con);
} }
return;
} }
@Override @Override
public int getAccountsRegistered() { public int getAccountsRegistered() {
Connection con = null; Connection con = null;
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null; ResultSet rs;
int result = 0; int result = 0;
try { try {
con = getConnection(); con = getConnection();
@ -706,26 +652,24 @@ public class SQLite_HIKARI implements DataSource {
pst.executeUpdate(); pst.executeUpdate();
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return;
} finally { } finally {
close(pst); close(pst);
close(con); close(con);
} }
return;
} }
@Override @Override
public List<PlayerAuth> getAllAuths() { public List<PlayerAuth> getAllAuths() {
Connection con = null; Connection con = null;
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null; ResultSet rs;
List<PlayerAuth> auths = new ArrayList<PlayerAuth>(); List<PlayerAuth> auths = new ArrayList<>();
try { try {
con = getConnection(); con = getConnection();
pst = con.prepareStatement("SELECT * FROM " + tableName + ";"); pst = con.prepareStatement("SELECT * FROM " + tableName + ";");
rs = pst.executeQuery(); rs = pst.executeQuery();
while (rs.next()) { while (rs.next()) {
PlayerAuth pAuth = null; PlayerAuth pAuth;
if (rs.getString(columnIp).isEmpty()) { if (rs.getString(columnIp).isEmpty()) {
pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), "127.0.0.1", rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), "127.0.0.1", rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName));
} else { } else {
@ -735,8 +679,7 @@ public class SQLite_HIKARI implements DataSource {
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)); 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));
} }
} }
if (pAuth != null) auths.add(pAuth);
auths.add(pAuth);
} }
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
@ -752,14 +695,14 @@ public class SQLite_HIKARI implements DataSource {
public List<PlayerAuth> getLoggedPlayers() { public List<PlayerAuth> getLoggedPlayers() {
Connection con = null; Connection con = null;
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null; ResultSet rs;
List<PlayerAuth> auths = new ArrayList<PlayerAuth>(); List<PlayerAuth> auths = new ArrayList<>();
try { try {
con = getConnection(); con = getConnection();
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnLogged + "=1;"); pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnLogged + "=1;");
rs = pst.executeQuery(); rs = pst.executeQuery();
while (rs.next()) { while (rs.next()) {
PlayerAuth pAuth = null; PlayerAuth pAuth;
if (rs.getString(columnIp).isEmpty()) { if (rs.getString(columnIp).isEmpty()) {
pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), "127.0.0.1", rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), "127.0.0.1", rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName));
} else { } else {
@ -769,12 +712,10 @@ public class SQLite_HIKARI implements DataSource {
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)); 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));
} }
} }
if (pAuth != null) auths.add(pAuth);
auths.add(pAuth);
} }
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return auths;
} finally { } finally {
close(pst); close(pst);
close(con); close(con);
@ -799,37 +740,14 @@ public class SQLite_HIKARI implements DataSource {
@Override @Override
public synchronized void close() { public synchronized void close() {
try { if (ds != null)
if (ds != null) ds.close();
ds.close();
} catch (Exception e) {
}
} }
private void close(Statement st) { private void close(AutoCloseable o) {
if (st != null) { if (o != null) {
try { try {
st.close(); o.close();
} catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage());
}
}
}
private void close(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage());
}
}
}
private void close(Connection con) {
if (con != null) {
try {
con.close();
} catch (Exception ex) { } catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
} }