geforkt von SteamWar/BungeeCore
Ursprung
49f7dc9911
Commit
f8d6b73c0f
@ -20,6 +20,7 @@
|
|||||||
package de.steamwar.bungeecore;
|
package de.steamwar.bungeecore;
|
||||||
|
|
||||||
import de.steamwar.bungeecore.sql.SWException;
|
import de.steamwar.bungeecore.sql.SWException;
|
||||||
|
import de.steamwar.bungeecore.sql.Statement;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
@ -58,7 +59,7 @@ public class ErrorLogger extends Handler {
|
|||||||
SWException.log("Bungee", "DDOS", ddosRate + "");
|
SWException.log("Bungee", "DDOS", ddosRate + "");
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
} else if (stacktrace.contains("ErrorLogger")) {
|
} else if (!Statement.connectionStable()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,39 +40,48 @@ public class Statement {
|
|||||||
Statement.user = user;
|
Statement.user = user;
|
||||||
Statement.password = password;
|
Statement.password = password;
|
||||||
try {
|
try {
|
||||||
con = DriverManager.getConnection(url + "?autoreconnect=true", user, password);
|
con = DriverManager.getConnection(url + "?autoreconnect=true&useServerPrepStmts=true", user, password);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
ProxyServer.getInstance().stop();
|
ProxyServer.getInstance().stop();
|
||||||
throw new SecurityException("Could not start SQL-Connection", e);
|
throw new SecurityException("Could not start SQL-Connection", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void reset(SQLException e) {
|
private static void reset() {
|
||||||
BungeeCore.get().getLogger().log(Level.WARNING, "SQL Exception thrown", e);
|
|
||||||
close();
|
close();
|
||||||
connect(url, user, password);
|
connect(url, user, password);
|
||||||
try {
|
try {
|
||||||
for (Statement statement : statements) {
|
for (Statement statement : statements) {
|
||||||
statement.init();
|
statement.init();
|
||||||
}
|
}
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException e) {
|
||||||
throw new SecurityException("Could not reprepare SQL Statements", ex);
|
throw new SecurityException("Could not reprepare SQL statements", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void close() {
|
public static void close() {
|
||||||
for (Statement statement : statements) {
|
synchronized (statements) {
|
||||||
|
for (Statement statement : statements) {
|
||||||
|
try {
|
||||||
|
statement.st.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
BungeeCore.get().getLogger().log(Level.INFO, "Could not close statement", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
statement.st.close();
|
con.close();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
BungeeCore.get().getLogger().log(Level.INFO, "Could not close statement", e);
|
BungeeCore.get().getLogger().log(Level.INFO, "Could not close SQL connection", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean connectionStable() {
|
||||||
try {
|
try {
|
||||||
con.close();
|
return !con.isClosed();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
BungeeCore.log("Could not close SQL-Connection", e);
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,39 +94,38 @@ public class Statement {
|
|||||||
try {
|
try {
|
||||||
init();
|
init();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
reset(e);
|
throw new SecurityException("Could not init SQL statement", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void init() throws SQLException {
|
private void init() throws SQLException {
|
||||||
st = con.prepareStatement(sql);
|
st = con.prepareStatement(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
<T> T select(ResultSetUser<T> user, Object... objects) {
|
<T> T select(ResultSetUser<T> user, Object... objects) {
|
||||||
return prepare(() -> {
|
synchronized (statements) {
|
||||||
ResultSet rs = st.executeQuery();
|
return prepare(() -> {
|
||||||
T result = user.use(rs);
|
ResultSet rs = st.executeQuery();
|
||||||
rs.close();
|
T result = user.use(rs);
|
||||||
return result;
|
rs.close();
|
||||||
}, objects);
|
return result;
|
||||||
|
}, objects);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void update(Object... objects) {
|
void update(Object... objects) {
|
||||||
prepare(st::executeUpdate, objects);
|
synchronized (statements) {
|
||||||
|
prepare(st::executeUpdate, objects);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized <T> T prepare(SQLRunnable<T> runnable, Object... objects) {
|
private <T> T prepare(SQLRunnable<T> runnable, Object... objects) {
|
||||||
try {
|
try {
|
||||||
setObjects(objects);
|
setObjects(objects);
|
||||||
return runnable.run();
|
return runnable.run();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
reset(e);
|
reset();
|
||||||
try {
|
throw new SecurityException("Could not execute SQL statement", e);
|
||||||
setObjects(objects);
|
|
||||||
return runnable.run();
|
|
||||||
} catch (SQLException ex) {
|
|
||||||
throw new SecurityException("Could not execute SQL statement", ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren