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.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;
this.executor.execute(() -> { try {
try { this.executor.execute(() -> {
action.run(this.getConnection()); try {
} catch(SQLException e) { action.run(this.getConnection());
this.handleError(description, e); } 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")); future.completeExceptionally(new IllegalStateException("No database configured"));
return future; return future;
} }
this.executor.execute(() -> { try {
try { this.executor.execute(() -> {
future.complete(query.run(this.getConnection())); try {
} catch(SQLException e) { future.complete(query.run(this.getConnection()));
this.closeConnection(); } catch(SQLException e) {
future.completeExceptionally(e); this.closeConnection();
} catch(RuntimeException e) { future.completeExceptionally(e);
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; return future;
} }