finished initial db reconnect

This commit is contained in:
EmilMeindl 2020-04-17 16:52:19 +02:00
parent f0e654399a
commit 605a2ca33d
2 changed files with 21 additions and 25 deletions

View File

@ -37,14 +37,8 @@ fun main() {
//initial connect to db
Log.message("initial login to db")
try {
val stor = Storage.getInstance();
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
@ -57,7 +51,6 @@ fun main() {
if (JDBC.isConnected()) {
val m = MqttService(Storage.getInstance().mqttServer, Storage.getInstance().mqttPort.toString())
// val m = MqttService("mqtt.heili.eu", "1883")
m.startupService()
}else{
Log.error("could't start mqtt service because of missing db connection!")

View File

@ -1,6 +1,7 @@
package com.wasteinformationserver.db;
import com.wasteinformationserver.basicutils.Log;
import com.wasteinformationserver.basicutils.Storage;
import java.io.IOException;
import java.sql.*;
@ -9,6 +10,7 @@ import java.sql.*;
* basic connection class to a Database
*
* @author Lukas Heiligenbrunner
* @author Emil Meindl
*/
public class JDBC {
private static Connection conn;
@ -22,7 +24,7 @@ public class JDBC {
private static String ipc;
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);
}
@ -66,11 +68,7 @@ public class JDBC {
*/
public static JDBC getInstance() {
if (!loggedin) {
try {
logintodb(usernamec, passwordc, dbnamec, ipc, portc);
} catch (IOException e) {
Log.Log.error("no connetion to db - retrying in 5min");
}
}
return JDBC;
}
@ -85,7 +83,7 @@ public class JDBC {
* @param port Server port
* @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 {
DriverManager.setLoginTimeout(1);
conn = DriverManager.getConnection(
@ -93,8 +91,10 @@ public class JDBC {
username,
password);
loggedin = true;
Log.Log.message("Connected to database");
} catch (SQLException e) {
//logintodb(usernamec, passwordc, dbnamec, ipc, portc);
// reconnect every 10 sec
Log.Log.warning("Database-Connection not possible");
new Thread(new Runnable() {
@Override
public void run() {
@ -103,17 +103,20 @@ public class JDBC {
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
try {
logintodb(usernamec, passwordc, dbnamec, ipc, portc);
} catch (IOException ioException) {
ioException.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();
throw new IOException("No connection to database");
// todo reconnect every 5mins or something
}
return loggedin;
}
public void disconnect(){