From 62f06b49014498fe5af8840134747709c28a5bae Mon Sep 17 00:00:00 2001 From: Aurora Lahtela <24460436+AuroraLS3@users.noreply.github.com> Date: Sat, 21 May 2022 09:58:07 +0300 Subject: [PATCH] Prevent further transactions from executing if FatalDBException occurs Affects issues: - Fixed #2365 --- .../djrapitops/plan/storage/database/MySQLDB.java | 2 +- .../com/djrapitops/plan/storage/database/SQLDB.java | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/MySQLDB.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/MySQLDB.java index 84b1ad5a14..c58cf84dbd 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/MySQLDB.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/MySQLDB.java @@ -194,7 +194,7 @@ public void returnToPool(Connection connection) { connection.close(); } } catch (SQLException e) { - errorLogger.critical(e, ErrorContext.builder().related("Closing connection").build()); + errorLogger.error(e, ErrorContext.builder().related("Closing connection").build()); } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/SQLDB.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/SQLDB.java index 6bd3abaab2..c9f8237563 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/SQLDB.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/SQLDB.java @@ -53,6 +53,7 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Function; import java.util.function.Supplier; @@ -84,6 +85,8 @@ public abstract class SQLDB extends AbstractDatabase { private Supplier transactionExecutorServiceProvider; private ExecutorService transactionExecutor; + private final AtomicBoolean ranIntoFatalError = new AtomicBoolean(false); + protected SQLDB( Supplier serverUUIDSupplier, Locale locale, @@ -295,7 +298,7 @@ private void unloadDriverClassloader() { // if (driverClassLoader instanceof IsolatedClassLoader) { // ((IsolatedClassLoader) driverClassLoader).close(); // } - driverClassLoader = null; + driverClassLoader = null; // } catch (IOException e) { // errorLogger.error(e, ErrorContext.builder().build()); // } @@ -321,7 +324,9 @@ public CompletableFuture executeTransaction(Transaction transaction) { return CompletableFuture.supplyAsync(() -> { accessLock.checkAccess(transaction); - transaction.executeTransaction(this); + if (!ranIntoFatalError.get()) { + transaction.executeTransaction(this); + } return CompletableFuture.completedFuture(null); }, getTransactionExecutor()).exceptionally(errorHandler(transaction, origin)); } @@ -332,6 +337,7 @@ private Function> errorHandler(Transaction return CompletableFuture.completedFuture(null); } if (throwable.getCause() instanceof FatalDBException) { + ranIntoFatalError.set(true); logger.error("Database failed to open, " + transaction.getClass().getName() + " failed to be executed."); FatalDBException actual = (FatalDBException) throwable.getCause(); Optional whatToDo = actual.getContext().flatMap(ErrorContext::getWhatToDo); @@ -343,7 +349,7 @@ private Function> errorHandler(Transaction ErrorContext errorContext = ErrorContext.builder() .related("Transaction: " + transaction.getClass()) - .related("DB State: " + getState()) + .related("DB State: " + getState() + " - fatal: " + ranIntoFatalError.get()) .build(); if (getState() == State.CLOSED) { errorLogger.critical(throwable, errorContext);