Merge pull request #13 from IngProjDutzlerHeiligenbrunnerMeindl/dbReconnect

Db reconnect
This commit is contained in:
Lukas-Heiligenbrunner 2020-04-17 18:29:24 +02:00 committed by GitHub
commit f8fd7d1b5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 23 deletions

View File

@ -37,14 +37,8 @@ fun main() {
//initial connect to db //initial connect to db
Log.message("initial login to db") Log.message("initial login to db")
try { val stor = Storage.getInstance();
val stor = Storage.getInstance(); JDBC.init(stor.dbUser, stor.dbPassword, stor.dbName, stor.dbhost, stor.dbPort)
JDBC.init(stor.dbUser, stor.dbPassword, stor.dbName, stor.dbhost, stor.dbPort)
//JDBC.init("ingproject", "Kb9Dxklumt76ieq6", "ingproject", "db.power4future.at", 3306)
//JDBC.init("users", "kOpaIJUjkgb9ur6S", "wasteinformation", "192.168.65.15", 3306);
} catch (e: IOException) {
Log.error("no connection to db")
}
//startup web server //startup web server
@ -57,7 +51,6 @@ fun main() {
if (JDBC.isConnected()) { if (JDBC.isConnected()) {
val m = MqttService(Storage.getInstance().mqttServer, Storage.getInstance().mqttPort.toString()) val m = MqttService(Storage.getInstance().mqttServer, Storage.getInstance().mqttPort.toString())
// val m = MqttService("mqtt.heili.eu", "1883")
m.startupService() m.startupService()
}else{ }else{
Log.error("could't start mqtt service because of missing db connection!") Log.error("could't start mqtt service because of missing db connection!")

View File

@ -1,6 +1,8 @@
package com.wasteinformationserver.db; package com.wasteinformationserver.db;
import com.mysql.cj.exceptions.ConnectionIsClosedException;
import com.wasteinformationserver.basicutils.Log; import com.wasteinformationserver.basicutils.Log;
import com.wasteinformationserver.basicutils.Storage;
import java.io.IOException; import java.io.IOException;
import java.sql.*; import java.sql.*;
@ -9,6 +11,7 @@ import java.sql.*;
* basic connection class to a Database * basic connection class to a Database
* *
* @author Lukas Heiligenbrunner * @author Lukas Heiligenbrunner
* @author Emil Meindl
*/ */
public class JDBC { public class JDBC {
private static Connection conn; private static Connection conn;
@ -22,7 +25,7 @@ public class JDBC {
private static String ipc; private static String ipc;
private static int portc; private static int portc;
private JDBC(String username, String password, String dbname, String ip, int port) throws IOException { private JDBC(String username, String password, String dbname, String ip, int port) {
logintodb(username, password, dbname, ip, port); logintodb(username, password, dbname, ip, port);
} }
@ -66,11 +69,7 @@ public class JDBC {
*/ */
public static JDBC getInstance() { public static JDBC getInstance() {
if (!loggedin) { if (!loggedin) {
try { logintodb(usernamec, passwordc, dbnamec, ipc, portc);
logintodb(usernamec, passwordc, dbnamec, ipc, portc);
} catch (IOException e) {
Log.Log.error("no connetion to db - retrying in 5min");
}
} }
return JDBC; return JDBC;
} }
@ -85,7 +84,7 @@ public class JDBC {
* @param port Server port * @param port Server port
* @throws IOException thrown if no connection to db is possible. * @throws IOException thrown if no connection to db is possible.
*/ */
private static void logintodb(String username, String password, String dbname, String ip, int port) throws IOException { private static boolean logintodb(String username, String password, String dbname, String ip, int port) {
try { try {
DriverManager.setLoginTimeout(1); DriverManager.setLoginTimeout(1);
conn = DriverManager.getConnection( conn = DriverManager.getConnection(
@ -93,10 +92,32 @@ public class JDBC {
username, username,
password); password);
loggedin = true; loggedin = true;
Log.Log.message("Connected to database");
} catch (SQLException e) { } catch (SQLException e) {
throw new IOException("No connection to database"); // reconnect every 10 sec
// todo reconnect every 5mins or something Log.Log.warning("Database-Connection not possible");
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
Log.Log.debug("Reading config");
Storage st = Storage.Companion.getInstance();
st.init();
usernamec = st.getDbName();
passwordc = st.getDbPassword();
dbnamec = st.getDbName();
ipc = st.getDbhost();
portc = st.getDbPort();
Log.Log.info("Retry connection");
loggedin = logintodb(usernamec, passwordc, dbnamec, ipc, portc);
}
}).start();
} }
return loggedin;
} }
public void disconnect(){ public void disconnect(){
@ -115,12 +136,19 @@ public class JDBC {
*/ */
public ResultSet executeQuery(String sql) { public ResultSet executeQuery(String sql) {
try { try {
conn.isValid(5);
PreparedStatement stmt = conn.prepareStatement(sql); PreparedStatement stmt = conn.prepareStatement(sql);
return stmt.executeQuery(); return stmt.executeQuery();
} catch (SQLNonTransientConnectionException ee){
if (logintodb(usernamec, passwordc, dbnamec, ipc, portc)) {
return this.executeQuery(sql);
} else {
return null;
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); return null;
} }
return null;
} }
/** /**
@ -131,9 +159,19 @@ public class JDBC {
* @throws SQLException * @throws SQLException
*/ */
public int executeUpdate(String sql) throws SQLException { public int executeUpdate(String sql) throws SQLException {
PreparedStatement stmt = conn.prepareStatement(sql); try {
conn.isValid(5);
return stmt.executeUpdate(); PreparedStatement stmt = conn.prepareStatement(sql);
return stmt.executeUpdate();
} catch (SQLNonTransientConnectionException ee){
if (logintodb(usernamec, passwordc, dbnamec, ipc, portc)) {
return this.executeUpdate(sql);
} else {
throw new SQLException();
}
} catch (SQLException e){
throw new SQLException();
}
} }
/** /**