From b9dd21609d6e980d6d910cd68c2176c3ebc2d52c Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Mon, 14 Jan 2019 18:19:35 +0200 Subject: [PATCH] Fixed Task related Exceptions on ShutdownHook --- .../plan/system/database/databases/sql/H2DB.java | 12 ++++++++---- .../system/database/databases/sql/SQLiteDB.java | 12 ++++++++---- .../database/databases/sql/SpongeMySQLDB.java | 14 +++++++++----- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/H2DB.java b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/H2DB.java index e200848bb..b16f9258f 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/H2DB.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/H2DB.java @@ -104,10 +104,14 @@ public class H2DB extends SQLDB { private void startConnectionPingTask() { stopConnectionPingTask(); - // Maintains Connection. - connectionPingTask = runnableFactory.create("DBConnectionPingTask " + getType().getName(), - new KeepAliveTask(connection, () -> getNewConnection(databaseFile), logger, errorHandler) - ).runTaskTimerAsynchronously(60L * 20L, 60L * 20L); + try { + // Maintains Connection. + connectionPingTask = runnableFactory.create("DBConnectionPingTask " + getType().getName(), + new KeepAliveTask(connection, () -> getNewConnection(databaseFile), logger, errorHandler) + ).runTaskTimerAsynchronously(60L * 20L, 60L * 20L); + } catch (Exception ignored) { + // Task failed to register because plugin is being disabled + } } private void stopConnectionPingTask() { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLiteDB.java b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLiteDB.java index 10869df52..3c16a88fa 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLiteDB.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLiteDB.java @@ -104,10 +104,14 @@ public class SQLiteDB extends SQLDB { private void startConnectionPingTask() { stopConnectionPingTask(); - // Maintains Connection. - connectionPingTask = runnableFactory.create("DBConnectionPingTask " + getType().getName(), - new KeepAliveTask(connection, () -> getNewConnection(databaseFile), logger, errorHandler) - ).runTaskTimerAsynchronously(60L * 20L, 60L * 20L); + try { + // Maintains Connection. + connectionPingTask = runnableFactory.create("DBConnectionPingTask " + getType().getName(), + new KeepAliveTask(connection, () -> getNewConnection(databaseFile), logger, errorHandler) + ).runTaskTimerAsynchronously(60L * 20L, 60L * 20L); + } catch (Exception ignored) { + // Task failed to register because plugin is being disabled + } } private void stopConnectionPingTask() { diff --git a/Plan/sponge/src/main/java/com/djrapitops/plan/system/database/databases/sql/SpongeMySQLDB.java b/Plan/sponge/src/main/java/com/djrapitops/plan/system/database/databases/sql/SpongeMySQLDB.java index 9fb45a226..4d6aeccc2 100644 --- a/Plan/sponge/src/main/java/com/djrapitops/plan/system/database/databases/sql/SpongeMySQLDB.java +++ b/Plan/sponge/src/main/java/com/djrapitops/plan/system/database/databases/sql/SpongeMySQLDB.java @@ -90,15 +90,19 @@ public class SpongeMySQLDB extends MySQLDB { return super.getConnection(); } catch (SQLException e) { if (e.getMessage().contains("has been closed")) { - try { - setupDataSource(); - } catch (DBInitException setupException) { - throw new IllegalStateException("Failed to set up a new datasource after connection failure.", setupException); - } + restartDataSource(); return super.getConnection(); } else { throw e; } } } + + private void restartDataSource() { + try { + setupDataSource(); + } catch (DBInitException setupException) { + throw new IllegalStateException("Failed to set up a new datasource after connection failure.", setupException); + } + } } \ No newline at end of file