Fix some rare cases

This commit is contained in:
kaenganxt
2014-11-22 23:26:57 +01:00
parent eca5ef967f
commit c1da960bdd
2 changed files with 25 additions and 24 deletions

View File

@@ -17,7 +17,7 @@ public class Errors {
String message = exc.getLocalizedMessage(); String message = exc.getLocalizedMessage();
String name = exc.getClass().toString(); String name = exc.getClass().toString();
String server = Core.getMainClass() == null ? "~not available~" : Core.getMainClass().getConfig().getString("server-name"); String server = Core.getMainClass() == null ? "~not available~" : Core.getMainClass().getConfig().getString("server-name");
String errorSql = "INSERT INTO coreErrors (server, exception, msg, info, timestamp) VALUES('" + server + "', '" + name + "', '" + message + "', '" + info + "', '" + System.currentTimeMillis() / 1000 + "')"; String errorSql = "INSERT INTO coreErrors (server, exception, msg, info, timestamp) VALUES('" + server + "', '" + name + "', '" + Core.getMySql().escapeString(message) + "', '" + info + "', '" + System.currentTimeMillis() / 1000 + "')";
Core.getMySql().queryUpdate(errorSql); Core.getMySql().queryUpdate(errorSql);
ResultSet rs = Core.getMySql().querySelect("SELECT id FROM coreErrors ORDER BY id DESC LIMIT 1"); ResultSet rs = Core.getMySql().querySelect("SELECT id FROM coreErrors ORDER BY id DESC LIMIT 1");
rs.first(); rs.first();

View File

@@ -40,8 +40,7 @@ public class MySQL {
cfg.options().copyDefaults(true); cfg.options().copyDefaults(true);
try { try {
cfg.save(file); cfg.save(file);
} catch(Throwable e) { } catch (IOException e) {
Errors.reportException(e);
} }
this.host = cfg.getString(dbLoc + "host"); this.host = cfg.getString(dbLoc + "host");
this.port = cfg.getInt(dbLoc + "port"); this.port = cfg.getInt(dbLoc + "port");
@@ -57,7 +56,7 @@ public class MySQL {
} }
public String escapeString(String text) { public String escapeString(String text) {
return text; return text.replace("'", "\\'");
} }
private Boolean openConnection() { private Boolean openConnection() {
@@ -66,10 +65,9 @@ public class MySQL {
Connection connLoc = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.db, this.username, this.pw); Connection connLoc = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.db, this.username, this.pw);
this.conn = connLoc; this.conn = connLoc;
return true; return true;
} catch(Throwable e) { } catch (ClassNotFoundException | SQLException e) {
Errors.reportException(e); return false;
} }
return false;
} }
private boolean hasConnection() { private boolean hasConnection() {
@@ -81,10 +79,9 @@ public class MySQL {
validConn = false; validConn = false;
} }
return validConn; return validConn;
} catch(Throwable e) { } catch (SQLException e) {
Errors.reportException(e); return false;
} }
return false;
} }
private Boolean reconnect() { private Boolean reconnect() {
@@ -131,8 +128,8 @@ public class MySQL {
try { try {
st = connLoc.prepareStatement(query); st = connLoc.prepareStatement(query);
st.executeUpdate(); st.executeUpdate();
} catch(Throwable e) { } catch (SQLException e) {
Errors.reportException(e); System.err.println("Failed to send Update '" + query + "'! (" + e.getLocalizedMessage() + ")");
} }
this.closeRessources(null, st); this.closeRessources(null, st);
} }
@@ -150,8 +147,8 @@ public class MySQL {
} else { } else {
return returns; return returns;
} }
} catch(Throwable e) { } catch (SQLException e) {
Errors.reportException(e); System.err.println("Unknown error whilst trying to build Prepared Statement!");
queryRedo(query, "select"); queryRedo(query, "select");
} }
return null; return null;
@@ -164,23 +161,28 @@ public class MySQL {
ResultSet rs; ResultSet rs;
try { try {
rs = st.executeQuery(); rs = st.executeQuery();
} catch(Throwable e) { } catch (SQLException e) {
Errors.reportException(e); System.err.println("Failed to send 'SELECT'-Query!(" + st.toString() + ") Will try to reconnect to database just in case... you know...");
System.err.println("Caused by: " + e.getMessage());
return null; return null;
} }
return rs; return rs;
} }
private void closeRessources(ResultSet rs, PreparedStatement st) { private void closeRessources(ResultSet rs, PreparedStatement st) {
try { if (rs != null) {
if (rs != null) { try {
rs.close(); rs.close();
} catch (SQLException e) {
} }
if (st != null) { }
if (st != null) {
try {
st.close(); st.close();
} } catch (SQLException e) {
} catch(Throwable e) {
Errors.reportException(e); }
} }
} }
@@ -189,8 +191,7 @@ public class MySQL {
if (this.conn != null) { if (this.conn != null) {
this.conn.close(); this.conn.close();
} }
} catch (Throwable e) { } catch (SQLException e) {
Errors.reportException(e);
} finally { } finally {
this.conn = null; this.conn = null;
} }