Fix some case when connection is not available

This commit is contained in:
vanhec_a 2015-10-12 14:57:56 +02:00
parent 8ad2fde798
commit 3c0e6e06c7
2 changed files with 76 additions and 34 deletions

View File

@ -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!");
}

View File

@ -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 + "<?;");
pst.setLong(1, until);
return pst.executeUpdate();
@ -557,7 +581,8 @@ public class MySQL implements DataSource {
ResultSet rs = null;
List<String> list = new ArrayList<>();
try {
con = getConnection();
if ((con = getConnection()) == null)
return list;
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnLastLogin + "<?;");
pst.setLong(1, until);
rs = pst.executeQuery();
@ -584,7 +609,8 @@ public class MySQL implements DataSource {
Connection con = null;
PreparedStatement pst = null;
try {
con = getConnection();
if ((con = getConnection()) == null)
return false;
if (Settings.getPasswordHash == HashAlgorithm.XENFORO) {
int id;
ResultSet rs;
@ -620,7 +646,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 " + lastlocX + " =?, " + lastlocY + "=?, " + lastlocZ + "=?, " + lastlocWorld + "=? WHERE LOWER(" + columnName + ")=?;");
pst.setDouble(1, auth.getQuitLocX());
pst.setDouble(2, auth.getQuitLocY());
@ -645,7 +672,8 @@ public class MySQL implements DataSource {
ResultSet rs = null;
int countIp = 0;
try {
con = getConnection();
if ((con = getConnection()) == null)
return 0;
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;");
pst.setString(1, ip);
rs = pst.executeQuery();
@ -668,7 +696,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 " + columnEmail + " =? WHERE LOWER(" + columnName + ")=?;");
pst.setString(1, auth.getEmail());
pst.setString(2, auth.getNickname());
@ -691,7 +720,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 " + columnSalt + " =? WHERE LOWER(" + columnName + ")=?;");
pst.setString(1, auth.getSalt());
pst.setString(2, auth.getNickname());
@ -723,7 +753,7 @@ public class MySQL implements DataSource {
@Override
public synchronized void close() {
if (ds != null)
if (ds != null && !ds.isClosed())
ds.close();
}
@ -744,7 +774,8 @@ public class MySQL implements DataSource {
ResultSet rs = null;
List<String> 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<String> 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<String> 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()) {