SteamWar/SpigotCore
Archiviert
13
0

Merge pull request 'Reimplement auto reconnect' (#126) from fixAutoReconnect into master
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Reviewed-on: #126
Reviewed-by: YoyoNow <jwsteam@nidido.de>
Dieser Commit ist enthalten in:
Lixfel 2021-11-09 10:53:10 +01:00
Commit 785aca4a01

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.sql;
import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException;
import de.steamwar.core.Core;
import org.bukkit.configuration.file.YamlConfiguration;
@ -33,27 +34,27 @@ public class SQL {
private SQL(){}
private static Connection con;
private static String url;
private static String user;
private static String password;
private static final String URL;
private static final String USER;
private static final String PASSWORD;
static{
static {
File file = new File(Core.getInstance().getDataFolder(), "MySQL.yml");
YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
if(!file.exists())
throw new SecurityException("SQL-ConfigFile not found!");
url = "jdbc:mysql://" + config.getString("HOST") + ":" + config.getString("PORT") + "/" + config.getString("DATABASE");
user = config.getString("USER");
password = config.getString("PASSWORD");
URL = "jdbc:mysql://" + config.getString("HOST") + ":" + config.getString("PORT") + "/" + config.getString("DATABASE");
USER = config.getString("USER");
PASSWORD = config.getString("PASSWORD");
connect();
}
private static void connect() {
try {
con = DriverManager.getConnection(url + "?autoReconnect=true&useServerPrepStmts=true", user, password);
con = DriverManager.getConnection(URL + "?useServerPrepStmts=true", USER, PASSWORD);
} catch (SQLException e) {
throw new SecurityException("Could not start SQL connection", e);
}
@ -96,6 +97,7 @@ public class SQL {
}
}
@Deprecated
static void update(String qry, Object... objects) {
try {
prepare(qry, objects).executeUpdate();
@ -105,7 +107,8 @@ public class SQL {
}
}
static ResultSet select(String qry, Object... objects){
@Deprecated
static ResultSet select(String qry, Object... objects) {
try {
return prepare(qry, objects).executeQuery();
} catch (SQLException e) {
@ -114,7 +117,8 @@ public class SQL {
}
}
static Blob blob(){
@Deprecated
static Blob blob() {
try {
return con.createBlob();
} catch (SQLException e) {
@ -123,7 +127,8 @@ public class SQL {
}
}
private static PreparedStatement prepare(String qry, Object... objects) throws SQLException{
@Deprecated
private static PreparedStatement prepare(String qry, Object... objects) throws SQLException {
PreparedStatement st = con.prepareStatement(qry);
for(int i = 0; i < objects.length; i++){
st.setObject(i+1, objects[i]);
@ -166,10 +171,15 @@ public class SQL {
private synchronized <T> T prepare(SQLRunnable<T> runnable, Object... objects) {
try {
setObjects(objects);
return runnable.run();
try {
setObjects(objects);
return runnable.run();
} catch (CommunicationsException e) {
reset();
setObjects(objects);
return runnable.run();
}
} catch (SQLException e) {
reset();
throw new SecurityException("Could not execute SQL statement", e);
}
}