From a153ca1fe16e9a69e955edc9d6cd63ffe89efaea Mon Sep 17 00:00:00 2001 From: James Seibel Date: Fri, 12 Jul 2024 20:21:57 -0500 Subject: [PATCH] Fix repo connections not getting closed --- .../core/api/internal/SharedApi.java | 3 ++ .../core/level/AbstractDhLevel.java | 14 ++++++++- .../core/sql/repo/AbstractDhRepo.java | 30 +++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/seibel/distanthorizons/core/api/internal/SharedApi.java b/core/src/main/java/com/seibel/distanthorizons/core/api/internal/SharedApi.java index f66c37501..11fda5913 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/api/internal/SharedApi.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/api/internal/SharedApi.java @@ -30,6 +30,7 @@ import com.seibel.distanthorizons.core.pos.DhBlockPos2D; import com.seibel.distanthorizons.core.pos.DhChunkPos; import com.seibel.distanthorizons.core.render.renderer.DebugRenderer; import com.seibel.distanthorizons.core.sql.dto.BeaconBeamDTO; +import com.seibel.distanthorizons.core.sql.repo.AbstractDhRepo; import com.seibel.distanthorizons.core.util.TimerUtil; import com.seibel.distanthorizons.core.util.objects.Pair; import com.seibel.distanthorizons.core.util.threading.ThreadPoolUtil; @@ -97,6 +98,8 @@ public class SharedApi ThreadPoolUtil.shutdownThreadPools(); DebugRenderer.clearRenderables(); MC_RENDER.clearTargetFrameBuffer(); + // shouldn't be necessary, but if we missed closing one of the connections this should make sure they're all closed + AbstractDhRepo.closeAllConnections(); // needs to be closed on world shutdown to clear out un-processed chunks UPDATING_CHUNK_POS_SET.clear(); diff --git a/core/src/main/java/com/seibel/distanthorizons/core/level/AbstractDhLevel.java b/core/src/main/java/com/seibel/distanthorizons/core/level/AbstractDhLevel.java index a26200efa..16f7b6694 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/level/AbstractDhLevel.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/level/AbstractDhLevel.java @@ -260,6 +260,18 @@ public abstract class AbstractDhLevel implements IDhLevel //================// @Override - public void close() { this.chunkToLodBuilder.close(); } + public void close() + { + this.chunkToLodBuilder.close(); + + if (this.chunkHashRepo != null) + { + this.chunkHashRepo.close(); + } + if (this.beaconBeamRepo != null) + { + this.beaconBeamRepo.close(); + } + } } diff --git a/core/src/main/java/com/seibel/distanthorizons/core/sql/repo/AbstractDhRepo.java b/core/src/main/java/com/seibel/distanthorizons/core/sql/repo/AbstractDhRepo.java index b4377a85d..63ab8953d 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/sql/repo/AbstractDhRepo.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/sql/repo/AbstractDhRepo.java @@ -426,6 +426,36 @@ public abstract class AbstractDhRepo> implemen } } + /** can be used to make sure everything is closed when the world closes */ + public static void closeAllConnections() + { + LOGGER.info("Closing all ["+ACTIVE_CONNECTION_STRINGS_BY_REPO.size()+"] database connections..."); + for (String connectionString : ACTIVE_CONNECTION_STRINGS_BY_REPO.values()) + { + try + { + Connection connection = CONNECTIONS_BY_CONNECTION_STRING.remove(connectionString); + if (connection != null) + { + if (!connection.isClosed()) + { + LOGGER.info("Closing database connection: [" + connectionString + "]"); + connection.close(); + } + else + { + LOGGER.warn("Attempting to close already closed database connection: [" + connectionString + "]"); + } + } + } + catch(SQLException e) + { + // connection close failed. + LOGGER.error("Unable to close the connection ["+connectionString+"], error: ["+e.getMessage()+"]"); + } + } + } + @Override public void close() {