From 922c99872c8b8aa698eebdf096e186529b733457 Mon Sep 17 00:00:00 2001 From: Risto Lahtela <24460436+AuroraLS3@users.noreply.github.com> Date: Tue, 1 Feb 2022 20:42:45 +0200 Subject: [PATCH] Hacky fix to forge crash The server crashes due to sqlite driver loading disabling class loading caches. This re-enables them after using the method. Note that if a class is loaded in a small timeframe between sqlite driver loading and the finally-block running there is a slight chance of a crash, but it is less than 100% Affects issues: - Fix #2202 --- .../plan/storage/database/SQLiteDB.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/SQLiteDB.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/SQLiteDB.java index a535a3b0b..7db8118d3 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/SQLiteDB.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/SQLiteDB.java @@ -37,6 +37,7 @@ import java.io.File; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; +import java.net.URLConnection; import java.sql.Connection; import java.sql.SQLException; import java.util.List; @@ -82,7 +83,7 @@ public class SQLiteDB extends SQLDB { try { return files.getResourceFromJar("dependencies/sqliteDriver.txt").asLines(); } catch (IOException e) { - throw new RuntimeException("Failed to get SQLite dependency information", e); + throw new DBInitException("Failed to get SQLite dependency information: " + e.getMessage(), e); } } @@ -93,7 +94,7 @@ public class SQLiteDB extends SQLDB { connection = getNewConnection(databaseFile); } catch (SQLException e) { - throw new DBInitException(e.getMessage(), e); + throw new DBInitException(e.toString(), e); } startConnectionPingTask(); } @@ -147,6 +148,16 @@ public class SQLiteDB extends SQLDB { return tryToConnect(dbFilePath, false); } catch (InstantiationException | IllegalAccessException e) { throw new DBInitException("Failed to initialize SQLite Driver", e); + } finally { + new URLConnection(null) { + @Override + public void connect() { + // Hack for fixing a class loading crash (https://github.com/plan-player-analytics/Plan/issues/2202) + // Caused by https://github.com/xerial/sqlite-jdbc/issues/656 + // Where setDefaultUseCaches is set to false + // TODO Remove after the underlying issue has been fixed in SQLite + } + }.setDefaultUseCaches(true); } }