fix potential db connexion problem
This commit is contained in:
parent
1c632788f1
commit
173762e481
@ -5,7 +5,11 @@ import java.sql.DriverManager;
|
|||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import fr.pandacube.util.Log;
|
||||||
|
|
||||||
public class DBConnection {
|
public class DBConnection {
|
||||||
|
private static final long CONNECTION_CHECK_TIMEOUT = 30000; // in ms
|
||||||
|
|
||||||
private Connection conn;
|
private Connection conn;
|
||||||
private String url;
|
private String url;
|
||||||
private String login;
|
private String login;
|
||||||
@ -30,6 +34,7 @@ public class DBConnection {
|
|||||||
|
|
||||||
private void checkConnection() throws SQLException {
|
private void checkConnection() throws SQLException {
|
||||||
if (!isConnected()) {
|
if (!isConnected()) {
|
||||||
|
Log.info("Connection to the database lost. Trying to reconnect...");
|
||||||
close();
|
close();
|
||||||
connect();
|
connect();
|
||||||
}
|
}
|
||||||
@ -37,29 +42,30 @@ public class DBConnection {
|
|||||||
|
|
||||||
public boolean isConnected()
|
public boolean isConnected()
|
||||||
{
|
{
|
||||||
boolean connected = false;
|
try {
|
||||||
|
if (conn.isClosed())
|
||||||
|
return false;
|
||||||
|
|
||||||
try (ResultSet rs = conn.createStatement().executeQuery("SELECT 1;"))
|
// avoid checking the connection everytime we want to do a db request
|
||||||
{
|
long now = System.currentTimeMillis();
|
||||||
if (rs == null)
|
if (timeOfLastCheck + CONNECTION_CHECK_TIMEOUT > now)
|
||||||
connected = false;
|
return true;
|
||||||
else if (rs.next())
|
|
||||||
connected = true;
|
timeOfLastCheck = now;
|
||||||
} catch (Exception e) {
|
|
||||||
connected = false;
|
if (conn.isValid(1))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
try (ResultSet rs = conn.createStatement().executeQuery("SELECT 1;")) {
|
||||||
|
return rs == null ? false : rs.next();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
return connected;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Connection getNativeConnection() throws SQLException {
|
public Connection getNativeConnection() throws SQLException {
|
||||||
if (conn.isClosed())
|
|
||||||
connect();
|
|
||||||
long now = System.currentTimeMillis();
|
|
||||||
if (timeOfLastCheck + 5000 > now) {
|
|
||||||
timeOfLastCheck = now;
|
|
||||||
if (!conn.isValid(1))
|
|
||||||
checkConnection();
|
checkConnection();
|
||||||
}
|
|
||||||
return conn;
|
return conn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user