From 173762e48100e16814cad2d8b63c99aa0bc0a7d8 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Sun, 2 Feb 2020 16:36:43 +0100 Subject: [PATCH] fix potential db connexion problem --- .../fr/pandacube/util/orm/DBConnection.java | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/main/java/fr/pandacube/util/orm/DBConnection.java b/src/main/java/fr/pandacube/util/orm/DBConnection.java index b649762..4a63107 100644 --- a/src/main/java/fr/pandacube/util/orm/DBConnection.java +++ b/src/main/java/fr/pandacube/util/orm/DBConnection.java @@ -5,7 +5,11 @@ import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; +import fr.pandacube.util.Log; + public class DBConnection { + private static final long CONNECTION_CHECK_TIMEOUT = 30000; // in ms + private Connection conn; private String url; private String login; @@ -30,6 +34,7 @@ public class DBConnection { private void checkConnection() throws SQLException { if (!isConnected()) { + Log.info("Connection to the database lost. Trying to reconnect..."); close(); connect(); } @@ -37,29 +42,30 @@ public class DBConnection { public boolean isConnected() { - boolean connected = false; + try { + if (conn.isClosed()) + return false; - try (ResultSet rs = conn.createStatement().executeQuery("SELECT 1;")) - { - if (rs == null) - connected = false; - else if (rs.next()) - connected = true; + // 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;")) { + return rs == null ? false : rs.next(); + } } catch (Exception e) { - connected = false; + return false; } - return connected; } 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; }