Merge branch 'master' into dbschemacheck

This commit is contained in:
Lukas-Heiligenbrunner 2020-04-17 18:32:23 +02:00 committed by GitHub
commit 51f6ca8958
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.*;
@ -10,6 +12,7 @@ import java.util.Scanner;
* 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;
@ -23,7 +26,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);
} }
@ -67,11 +70,7 @@ public class JDBC {
*/ */
public static JDBC getInstance() { public static JDBC getInstance() {
if (!loggedin) { if (!loggedin) {
try { logintodb(usernamec, passwordc, dbnamec, ipc, portc);
JDBC = new JDBC(usernamec, passwordc, dbnamec, ipc, portc);
} catch (IOException e) {
Log.Log.error("no connetion to db - retrying in 5min");
}
} }
return JDBC; return JDBC;
} }
@ -86,7 +85,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 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(
@ -95,10 +94,32 @@ public class JDBC {
password); password);
checkDBStructure(); checkDBStructure();
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() {
@ -117,13 +138,20 @@ 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 (SQLException e) { } catch (SQLNonTransientConnectionException ee){
e.printStackTrace(); if (logintodb(usernamec, passwordc, dbnamec, ipc, portc)) {
} return this.executeQuery(sql);
} else {
return null; return null;
} }
} catch (SQLException e) {
return null;
}
}
/** /**
* update db in some way * update db in some way
@ -133,9 +161,19 @@ public class JDBC {
* @throws SQLException * @throws SQLException
*/ */
public int executeUpdate(String sql) throws SQLException { public int executeUpdate(String sql) throws SQLException {
try {
conn.isValid(5);
PreparedStatement stmt = conn.prepareStatement(sql); PreparedStatement stmt = conn.prepareStatement(sql);
return stmt.executeUpdate(); 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();
}
} }
/** /**