From 1913e09303262d5383ef6258247c6258c0adfb81 Mon Sep 17 00:00:00 2001 From: James Seibel Date: Tue, 14 Jan 2025 07:03:12 -0600 Subject: [PATCH] Put several queries in try-finally blocks this prevents leaks --- .../core/sql/repo/AbstractDhRepo.java | 57 ++++----- .../core/sql/repo/FullDataSourceV2Repo.java | 115 ++++++++++-------- .../testItems/sql/TestCompoundKeyRepo.java | 5 +- .../testItems/sql/TestPrimaryKeyRepo.java | 5 +- 4 files changed, 98 insertions(+), 84 deletions(-) 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 28361b8ff..49aacfaf0 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 @@ -223,9 +223,9 @@ public abstract class AbstractDhRepo> implemen } private void insert(TDTO dto) { - try(PreparedStatement statement = this.createInsertStatement(dto)) + try(PreparedStatement statement = this.createInsertStatement(dto); + ResultSet result = this.query(statement)) { - this.query(statement); } catch (DbConnectionClosedException ignored) { @@ -240,9 +240,10 @@ public abstract class AbstractDhRepo> implemen } private void update(TDTO dto) { - try(PreparedStatement statement = this.createUpdateStatement(dto)) + try(PreparedStatement statement = this.createUpdateStatement(dto); + ResultSet result = this.query(statement)) { - this.query(statement); + } catch (DbConnectionClosedException e) { @@ -260,9 +261,10 @@ public abstract class AbstractDhRepo> implemen public void delete(TDTO dto) { this.deleteWithKey(dto.getKey()); } public void deleteWithKey(TKey key) { - try (PreparedStatement statement = this.createDeleteStatementByKey(key)) + try (PreparedStatement statement = this.createDeleteStatementByKey(key); + ResultSet result = this.query(statement)) { - this.query(statement); + } catch (SQLException e) { @@ -278,9 +280,10 @@ public abstract class AbstractDhRepo> implemen public void deleteAll() { String sql = "DELETE FROM " + this.getTableName(); - try (PreparedStatement statement = this.createPreparedStatement(sql)) + try (PreparedStatement statement = this.createPreparedStatement(sql); + ResultSet result = this.query(statement)) { - this.query(statement); + } catch (SQLException e) { @@ -328,28 +331,26 @@ public abstract class AbstractDhRepo> implemen } protected void triggerWalFlush() { - try (PreparedStatement statement = this.createPreparedStatement("PRAGMA wal_checkpoint(PASSIVE)")) + try (PreparedStatement statement = this.createPreparedStatement("PRAGMA wal_checkpoint(PASSIVE)"); + ResultSet result = this.query(statement)) { - try (ResultSet result = this.query(statement)) + if (result == null) { - if (result == null) - { - return; - } - - int busyInt = result.getInt("busy"); // usually 0 but will be 1 if a RESTART or FULL or TRUNCATE checkpoint was blocked from completing - boolean checkpointWasBlocked = (busyInt == 1); - int modifiedPageCount = result.getInt("log"); // number of modified pages that have been written to the write-ahead log file - int numberOfPagesWrittenToDb = result.getInt("checkpointed"); // number of pages in the write-ahead log file that have been successfully moved back into the database file at the conclusion of the checkpoint - - if (!checkpointWasBlocked) - { - LOGGER.info("WAL flushed, modified pages: ["+modifiedPageCount+"], written pages: ["+numberOfPagesWrittenToDb+"]."); - } - else - { - LOGGER.warn("WAL flush blocked, modified pages: ["+modifiedPageCount+"], written pages: ["+numberOfPagesWrittenToDb+"]."); - } + return; + } + + int busyInt = result.getInt("busy"); // usually 0 but will be 1 if a RESTART or FULL or TRUNCATE checkpoint was blocked from completing + boolean checkpointWasBlocked = (busyInt == 1); + int modifiedPageCount = result.getInt("log"); // number of modified pages that have been written to the write-ahead log file + int numberOfPagesWrittenToDb = result.getInt("checkpointed"); // number of pages in the write-ahead log file that have been successfully moved back into the database file at the conclusion of the checkpoint + + if (!checkpointWasBlocked) + { + LOGGER.info("WAL flushed, modified pages: ["+modifiedPageCount+"], written pages: ["+numberOfPagesWrittenToDb+"]."); + } + else + { + LOGGER.warn("WAL flush blocked, modified pages: ["+modifiedPageCount+"], written pages: ["+numberOfPagesWrittenToDb+"]."); } } catch (Exception e) diff --git a/core/src/main/java/com/seibel/distanthorizons/core/sql/repo/FullDataSourceV2Repo.java b/core/src/main/java/com/seibel/distanthorizons/core/sql/repo/FullDataSourceV2Repo.java index ea836bcc9..698966970 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/sql/repo/FullDataSourceV2Repo.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/sql/repo/FullDataSourceV2Repo.java @@ -262,7 +262,10 @@ public class FullDataSourceV2Repo extends AbstractDhRepo returnMap = new HashMap<>(); - while (result != null && result.next()) + try (ResultSet result = this.query(preparedStatement)) { - long key = DhSectionPos.encode(detailLevel, result.getInt("PosX"), result.getInt("PosZ")); - long value = result.getLong("LastModifiedUnixDateTime"); + HashMap returnMap = new HashMap<>(); + while (result != null && result.next()) + { + long key = DhSectionPos.encode(detailLevel, result.getInt("PosX"), result.getInt("PosZ")); + long value = result.getLong("LastModifiedUnixDateTime"); + + returnMap.put(key, value); + } - returnMap.put(key, value); + return returnMap; } - - return returnMap; } catch (SQLException e) { @@ -484,9 +495,8 @@ public class FullDataSourceV2Repo extends AbstractDhRepo