Database open failure now disables the plugin

Affects issues:
- Fixed #1394
This commit is contained in:
Risto Lahtela 2020-08-12 13:27:42 +03:00
parent 57d9a2762c
commit fef717cd33
6 changed files with 29 additions and 13 deletions

View File

@ -21,7 +21,7 @@ package com.djrapitops.plan.exceptions;
*
* @author Rsl1122
*/
public class EnableException extends Exception {
public class EnableException extends IllegalStateException {
public EnableException(String message, Throwable cause) {
super(message, cause);

View File

@ -16,9 +16,15 @@
*/
package com.djrapitops.plan.exceptions.database;
import com.djrapitops.plan.utilities.logging.ErrorContext;
public class FatalDBException extends DBOpException {
public FatalDBException(String message) {
super(message);
}
public FatalDBException(String message, DBOpException cause) {
super(message + cause.getMessage(), cause.getCause(), cause.getContext().orElse(ErrorContext.builder().build()));
}
}

View File

@ -16,7 +16,7 @@
*/
package com.djrapitops.plan.storage.database;
import com.djrapitops.plan.exceptions.database.DBOpException;
import com.djrapitops.plan.exceptions.EnableException;
import com.djrapitops.plan.storage.database.transactions.Transaction;
import com.djrapitops.plan.storage.database.transactions.init.OperationCriticalTransaction;
@ -58,7 +58,7 @@ public class DBAccessLock {
synchronized (lockObject) {
lockObject.wait();
if (database.getState() == Database.State.CLOSED) {
throw new DBOpException("Database failed to open, Query has failed. (This exception is necessary to not keep query threads waiting)");
throw new EnableException("Database failed to open, Query has failed. (This exception is necessary to not keep query threads waiting)");
}
}
}

View File

@ -42,10 +42,7 @@ import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.BiFunction;
import java.util.function.Supplier;
@ -259,13 +256,20 @@ public abstract class SQLDB extends AbstractDatabase {
if (throwable == null) {
return CompletableFuture.completedFuture(null);
}
if (throwable instanceof FatalDBException) {
if (throwable.getCause() instanceof FatalDBException) {
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);
whatToDo.ifPresent(message -> logger.error("What to do: " + message));
if (!whatToDo.isPresent()) logger.error("Error msg: " + actual.getMessage());
setState(State.CLOSED);
}
ThrowableUtils.appendEntryPointToCause(throwable, origin);
errorLogger.log(L.ERROR, throwable, ErrorContext.builder()
.related("Transaction: " + transaction.getClass()).build());
errorLogger.log(getState() == State.CLOSED ? L.CRITICAL : L.ERROR, throwable, ErrorContext.builder()
.related("Transaction: " + transaction.getClass())
.related("DB State: " + getState())
.build());
return CompletableFuture.completedFuture(null);
};
}

View File

@ -16,6 +16,7 @@
*/
package com.djrapitops.plan.storage.database.transactions.init;
import com.djrapitops.plan.exceptions.database.DBOpException;
import com.djrapitops.plan.exceptions.database.FatalDBException;
import com.djrapitops.plan.storage.database.SQLDB;
import com.djrapitops.plan.storage.database.transactions.Transaction;
@ -31,9 +32,13 @@ public abstract class OperationCriticalTransaction extends Transaction {
@Override
public void executeTransaction(SQLDB db) {
super.executeTransaction(db);
if (!success) {
throw new FatalDBException(getClass().getSimpleName() + " failed to execute and database can not be opened.");
try {
super.executeTransaction(db);
if (!success) {
throw new FatalDBException(getClass().getName() + " failed to execute and database could not be opened.");
}
} catch (DBOpException e) {
throw new FatalDBException(getClass().getName() + " failed to execute and database could not be opened: ", e);
}
}
}

View File

@ -51,6 +51,7 @@ public class ErrorContext {
public void merge(ErrorContext context) {
this.related.addAll(context.related);
if (this.whatToDo == null && context.whatToDo != null) this.whatToDo = context.whatToDo;
}
public static class Builder {