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
+30 -18
@@ -9,6 +9,7 @@ import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
@@ -36,7 +37,7 @@ public class Database {
1, 1, 0, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<>(1000),
runnable -> new Thread(runnable, "Spawn-Database"),
(runnable, executor) -> Main.logger().warning("Database queue full, dropping statement")
new ThreadPoolExecutor.AbortPolicy()
);
private @Nullable Connection connection;
@@ -69,13 +70,19 @@ public class Database {
*/
public void execute(String description, SqlAction action) {
if(!this.isAvailable()) return;
this.executor.execute(() -> {
try {
action.run(this.getConnection());
} catch(SQLException e) {
this.handleError(description, e);
}
});
try {
this.executor.execute(() -> {
try {
action.run(this.getConnection());
} catch(SQLException 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,16 +94,21 @@ public class Database {
future.completeExceptionally(new IllegalStateException("No database configured"));
return future;
}
this.executor.execute(() -> {
try {
future.complete(query.run(this.getConnection()));
} catch(SQLException e) {
this.closeConnection();
future.completeExceptionally(e);
} catch(RuntimeException e) {
future.completeExceptionally(e);
}
});
try {
this.executor.execute(() -> {
try {
future.complete(query.run(this.getConnection()));
} catch(SQLException e) {
this.closeConnection();
future.completeExceptionally(e);
} catch(RuntimeException e) {
future.completeExceptionally(e);
}
});
} catch(RejectedExecutionException e) {
Main.logger().warning("Database queue full, dropping statement");
future.completeExceptionally(e);
}
return future;
}