2022-07-20 13:18:57 +02:00
|
|
|
package fr.pandacube.lib.db;
|
2016-07-08 11:33:22 +02:00
|
|
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.DriverManager;
|
2019-03-15 19:09:11 +01:00
|
|
|
import java.sql.ResultSet;
|
2016-07-08 11:33:22 +02:00
|
|
|
import java.sql.SQLException;
|
|
|
|
|
2022-07-20 13:18:57 +02:00
|
|
|
import fr.pandacube.lib.util.Log;
|
2020-02-02 16:36:43 +01:00
|
|
|
|
2016-07-08 11:33:22 +02:00
|
|
|
public class DBConnection {
|
2020-02-02 16:36:43 +01:00
|
|
|
private static final long CONNECTION_CHECK_TIMEOUT = 30000; // in ms
|
|
|
|
|
2017-11-04 19:20:53 +01:00
|
|
|
private Connection conn;
|
2022-07-10 00:55:56 +02:00
|
|
|
private final String url;
|
|
|
|
private final String login;
|
|
|
|
private final String pass;
|
2017-11-04 19:20:53 +01:00
|
|
|
|
|
|
|
private long timeOfLastCheck = 0;
|
2016-07-14 14:22:23 +02:00
|
|
|
|
|
|
|
public DBConnection(String host, int port, String dbname, String l, String p)
|
2022-07-10 00:55:56 +02:00
|
|
|
throws SQLException {
|
2016-07-26 02:06:21 +02:00
|
|
|
url = "jdbc:mysql://" + host + ":" + port + "/" + dbname
|
|
|
|
+ "?autoReconnect=true"
|
|
|
|
+ "&useUnicode=true"
|
2020-12-27 01:35:49 +01:00
|
|
|
+ "&useSSL=false"
|
2016-07-26 02:06:21 +02:00
|
|
|
+ "&characterEncoding=utf8"
|
|
|
|
+ "&characterSetResults=utf8"
|
|
|
|
+ "&character_set_server=utf8mb4"
|
|
|
|
+ "&character_set_connection=utf8mb4";
|
2016-07-08 11:33:22 +02:00
|
|
|
login = l;
|
|
|
|
pass = p;
|
2016-07-12 19:26:49 +02:00
|
|
|
connect();
|
2016-07-08 11:33:22 +02:00
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2019-03-15 19:09:11 +01:00
|
|
|
private void checkConnection() throws SQLException {
|
|
|
|
if (!isConnected()) {
|
2020-02-02 16:36:43 +01:00
|
|
|
Log.info("Connection to the database lost. Trying to reconnect...");
|
2017-11-04 19:20:53 +01:00
|
|
|
close();
|
2016-07-12 19:26:49 +02:00
|
|
|
connect();
|
2016-07-08 11:33:22 +02:00
|
|
|
}
|
|
|
|
}
|
2019-03-15 19:09:11 +01:00
|
|
|
|
2022-07-20 13:18:57 +02:00
|
|
|
private boolean isConnected()
|
2019-03-15 19:09:11 +01:00
|
|
|
{
|
2020-02-02 16:36:43 +01:00
|
|
|
try {
|
|
|
|
if (conn.isClosed())
|
|
|
|
return false;
|
2019-03-15 19:09:11 +01:00
|
|
|
|
2020-02-02 16:36:43 +01:00
|
|
|
// avoid checking the connection everytime we want to do a db request
|
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
if (timeOfLastCheck + CONNECTION_CHECK_TIMEOUT > now)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
timeOfLastCheck = now;
|
|
|
|
|
|
|
|
if (conn.isValid(1))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
try (ResultSet rs = conn.createStatement().executeQuery("SELECT 1;")) {
|
2022-07-10 00:55:56 +02:00
|
|
|
return rs != null && rs.next();
|
2020-02-02 16:36:43 +01:00
|
|
|
}
|
2019-03-15 19:09:11 +01:00
|
|
|
} catch (Exception e) {
|
2020-02-02 16:36:43 +01:00
|
|
|
return false;
|
2019-03-15 19:09:11 +01:00
|
|
|
}
|
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
|
|
|
public Connection getNativeConnection() throws SQLException {
|
2020-02-02 16:36:43 +01:00
|
|
|
checkConnection();
|
2016-07-08 11:33:22 +02:00
|
|
|
return conn;
|
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2016-07-12 19:26:49 +02:00
|
|
|
private void connect() throws SQLException {
|
|
|
|
conn = DriverManager.getConnection(url, login, pass);
|
2017-11-04 19:20:53 +01:00
|
|
|
timeOfLastCheck = System.currentTimeMillis();
|
2016-07-12 19:26:49 +02:00
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2016-07-08 11:33:22 +02:00
|
|
|
public void close() {
|
|
|
|
try {
|
|
|
|
conn.close();
|
2022-07-10 00:55:56 +02:00
|
|
|
} catch (Exception ignored) {}
|
2016-07-08 11:33:22 +02:00
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2016-07-08 11:33:22 +02:00
|
|
|
}
|