fixed dropped database statements never completing their query

This commit is contained in:
MineTec committed 2026-10-14 21:07:27 +02:00
1 parent 7bd1e13660
commit ddcbdbaec5
1 file changed
+13 -1
@@ -9,6 +9,7 @@ import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.logging.Level; import java.util.logging.Level;
@@ -36,7 +37,7 @@ public class Database {
1, 1, 0, TimeUnit.MILLISECONDS, 1, 1, 0, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<>(1000), new ArrayBlockingQueue<>(1000),
runnable -> new Thread(runnable, "Spawn-Database"), runnable -> new Thread(runnable, "Spawn-Database"),
(runnable, executor) -> Main.logger().warning("Database queue full, dropping statement") new ThreadPoolExecutor.AbortPolicy()
); );
private @Nullable Connection connection; private @Nullable Connection connection;
@@ -69,13 +70,19 @@ public class Database {
*/ */
public void execute(String description, SqlAction action) { public void execute(String description, SqlAction action) {
if(!this.isAvailable()) return; if(!this.isAvailable()) return;
try {
this.executor.execute(() -> { this.executor.execute(() -> {
try { try {
action.run(this.getConnection()); action.run(this.getConnection());
} catch(SQLException e) { } catch(SQLException e) {
this.handleError(description, e); this.handleError(description, e);
} catch(RuntimeException e) {
Main.logger().log(Level.WARNING, String.format("Database: %s failed", description), e);
} }
}); });
} catch(RejectedExecutionException e) {
Main.logger().warning("Database queue full, dropping statement");
}
} }
/** /**
@@ -87,6 +94,7 @@ public class Database {
future.completeExceptionally(new IllegalStateException("No database configured")); future.completeExceptionally(new IllegalStateException("No database configured"));
return future; return future;
} }
try {
this.executor.execute(() -> { this.executor.execute(() -> {
try { try {
future.complete(query.run(this.getConnection())); future.complete(query.run(this.getConnection()));
@@ -97,6 +105,10 @@ public class Database {
future.completeExceptionally(e); future.completeExceptionally(e);
} }
}); });
} catch(RejectedExecutionException e) {
Main.logger().warning("Database queue full, dropping statement");
future.completeExceptionally(e);
}
return future; return future;
} }