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 //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,7 @@
package com.wasteinformationserver.db; package com.wasteinformationserver.db;
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 +10,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 +24,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 +68,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 +83,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,8 +91,10 @@ public class JDBC {
username, username,
password); password);
loggedin = true; loggedin = true;
Log.Log.message("Connected to database");
} catch (SQLException e) { } catch (SQLException e) {
//logintodb(usernamec, passwordc, dbnamec, ipc, portc); // reconnect every 10 sec
Log.Log.warning("Database-Connection not possible");
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -103,17 +103,20 @@ public class JDBC {
} catch (InterruptedException interruptedException) { } catch (InterruptedException interruptedException) {
interruptedException.printStackTrace(); interruptedException.printStackTrace();
} }
try { Log.Log.debug("Reading config");
logintodb(usernamec, passwordc, dbnamec, ipc, portc); Storage st = Storage.Companion.getInstance();
} catch (IOException ioException) { st.init();
ioException.printStackTrace(); 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(); }).start();
throw new IOException("No connection to database");
// todo reconnect every 5mins or something
} }
return loggedin;
} }
public void disconnect(){ public void disconnect(){