Skip to content

Commit

Permalink
Prevent further transactions from executing if FatalDBException occurs
Browse files Browse the repository at this point in the history
Affects issues:
- Fixed #2365
  • Loading branch information
AuroraLS3 committed May 21, 2022
1 parent de27669 commit 62f06b4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -84,6 +85,8 @@ public abstract class SQLDB extends AbstractDatabase {
private Supplier<ExecutorService> transactionExecutorServiceProvider;
private ExecutorService transactionExecutor;

private final AtomicBoolean ranIntoFatalError = new AtomicBoolean(false);

protected SQLDB(
Supplier<ServerUUID> serverUUIDSupplier,
Locale locale,
Expand Down Expand Up @@ -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());
// }
Expand All @@ -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));
}
Expand All @@ -332,6 +337,7 @@ private Function<Throwable, CompletableFuture<Object>> 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<String> whatToDo = actual.getContext().flatMap(ErrorContext::getWhatToDo);
Expand All @@ -343,7 +349,7 @@ private Function<Throwable, CompletableFuture<Object>> 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);
Expand Down

0 comments on commit 62f06b4

Please sign in to comment.