Put several queries in try-finally blocks
this prevents leaks
This commit is contained in:
@@ -223,9 +223,9 @@ public abstract class AbstractDhRepo<TKey, TDTO extends IBaseDTO<TKey>> 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<TKey, TDTO extends IBaseDTO<TKey>> 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<TKey, TDTO extends IBaseDTO<TKey>> 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<TKey, TDTO extends IBaseDTO<TKey>> 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<TKey, TDTO extends IBaseDTO<TKey>> 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)
|
||||
|
||||
+63
-52
@@ -262,7 +262,10 @@ public class FullDataSourceV2Repo extends AbstractDhRepo<Long, FullDataSourceV2D
|
||||
statement.setInt(i++, DhSectionPos.getX(pos));
|
||||
statement.setInt(i++, DhSectionPos.getZ(pos));
|
||||
|
||||
this.query(statement);
|
||||
try (ResultSet result = this.query(statement))
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
@@ -296,16 +299,18 @@ public class FullDataSourceV2Repo extends AbstractDhRepo<Long, FullDataSourceV2D
|
||||
|
||||
statement.setInt(i++, returnCount);
|
||||
|
||||
ResultSet result = this.query(statement);
|
||||
while (result != null && result.next())
|
||||
try (ResultSet result = this.query(statement))
|
||||
{
|
||||
byte detailLevel = result.getByte("DetailLevel");
|
||||
byte sectionDetailLevel = (byte) (detailLevel + DhSectionPos.SECTION_MINIMUM_DETAIL_LEVEL);
|
||||
int posX = result.getInt("PosX");
|
||||
int posZ = result.getInt("PosZ");
|
||||
|
||||
long pos = DhSectionPos.encode(sectionDetailLevel, posX, posZ);
|
||||
list.add(pos);
|
||||
while (result != null && result.next())
|
||||
{
|
||||
byte detailLevel = result.getByte("DetailLevel");
|
||||
byte sectionDetailLevel = (byte) (detailLevel + DhSectionPos.SECTION_MINIMUM_DETAIL_LEVEL);
|
||||
int posX = result.getInt("PosX");
|
||||
int posZ = result.getInt("PosZ");
|
||||
|
||||
long pos = DhSectionPos.encode(sectionDetailLevel, posX, posZ);
|
||||
list.add(pos);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
@@ -343,28 +348,30 @@ public class FullDataSourceV2Repo extends AbstractDhRepo<Long, FullDataSourceV2D
|
||||
statement.setInt(i++, DhSectionPos.getZ(pos));
|
||||
|
||||
|
||||
ResultSet result = this.query(statement);
|
||||
if (result == null || !result.next())
|
||||
try (ResultSet result = this.query(statement))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
byte compressionModeEnumValue = result.getByte("CompressionMode");
|
||||
EDhApiDataCompressionMode compressionModeEnum = EDhApiDataCompressionMode.getFromValue(compressionModeEnumValue);
|
||||
|
||||
try
|
||||
{
|
||||
// decompress the data
|
||||
DhDataInputStream compressedIn = new DhDataInputStream(result.getBinaryStream("ColumnGenerationStep"), compressionModeEnum);
|
||||
putAllBytes(compressedIn, outputByteArray);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
LOGGER.warn("Decompression issue when getting column gen steps for pos: [" + DhSectionPos.toString(pos) + "], deleting corrupted data.", e);
|
||||
if (result == null || !result.next())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.deleteWithKey(pos);
|
||||
ListUtil.clearAndSetSize(outputByteArray, FullDataSourceV2.WIDTH * FullDataSourceV2.WIDTH);
|
||||
|
||||
byte compressionModeEnumValue = result.getByte("CompressionMode");
|
||||
EDhApiDataCompressionMode compressionModeEnum = EDhApiDataCompressionMode.getFromValue(compressionModeEnumValue);
|
||||
|
||||
try
|
||||
{
|
||||
// decompress the data
|
||||
DhDataInputStream compressedIn = new DhDataInputStream(result.getBinaryStream("ColumnGenerationStep"), compressionModeEnum);
|
||||
putAllBytes(compressedIn, outputByteArray);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
LOGGER.warn("Decompression issue when getting column gen steps for pos: [" + DhSectionPos.toString(pos) + "], deleting corrupted data.", e);
|
||||
|
||||
this.deleteWithKey(pos);
|
||||
ListUtil.clearAndSetSize(outputByteArray, FullDataSourceV2.WIDTH * FullDataSourceV2.WIDTH);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (SQLException e)
|
||||
@@ -402,13 +409,15 @@ public class FullDataSourceV2Repo extends AbstractDhRepo<Long, FullDataSourceV2D
|
||||
preparedStatement.setInt(i++, DhSectionPos.getX(pos));
|
||||
preparedStatement.setInt(i++, DhSectionPos.getZ(pos));
|
||||
|
||||
ResultSet result = this.query(preparedStatement);
|
||||
if (result == null || !result.next())
|
||||
try (ResultSet result = this.query(preparedStatement))
|
||||
{
|
||||
return null;
|
||||
if (result == null || !result.next())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return result.getLong("LastModifiedUnixDateTime");
|
||||
}
|
||||
|
||||
return result.getLong("LastModifiedUnixDateTime");
|
||||
}
|
||||
catch (DbConnectionClosedException e)
|
||||
{
|
||||
@@ -445,17 +454,19 @@ public class FullDataSourceV2Repo extends AbstractDhRepo<Long, FullDataSourceV2D
|
||||
preparedStatement.setInt(i++, endPosZ - 1);
|
||||
|
||||
|
||||
ResultSet result = this.query(preparedStatement);
|
||||
HashMap<Long, Long> 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<Long, Long> 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<Long, FullDataSourceV2D
|
||||
}
|
||||
|
||||
|
||||
try
|
||||
try(ResultSet result = this.query(statement))
|
||||
{
|
||||
ResultSet result = this.query(statement);
|
||||
while (result != null && result.next())
|
||||
{
|
||||
byte detailLevel = result.getByte("DetailLevel");
|
||||
@@ -533,13 +543,15 @@ public class FullDataSourceV2Repo extends AbstractDhRepo<Long, FullDataSourceV2D
|
||||
statement.setInt(i++, DhSectionPos.getZ(pos));
|
||||
|
||||
|
||||
ResultSet result = this.query(statement);
|
||||
if (result == null || !result.next())
|
||||
try (ResultSet result = this.query(statement)) // TODO check other query's
|
||||
{
|
||||
return 0L;
|
||||
if (result == null || !result.next())
|
||||
{
|
||||
return 0L;
|
||||
}
|
||||
|
||||
return result.getLong("dataSize");
|
||||
}
|
||||
|
||||
return result.getLong("dataSize");
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
@@ -555,9 +567,8 @@ public class FullDataSourceV2Repo extends AbstractDhRepo<Long, FullDataSourceV2D
|
||||
{
|
||||
PreparedStatement statement = this.createPreparedStatement(this.getTotalDataSizeInBytesSql);
|
||||
|
||||
try
|
||||
try(ResultSet result = this.query(statement))
|
||||
{
|
||||
ResultSet result = this.query(statement);
|
||||
if (result == null || !result.next())
|
||||
{
|
||||
return 0;
|
||||
|
||||
@@ -48,9 +48,10 @@ public class TestCompoundKeyRepo extends AbstractDhRepo<DhChunkPos, TestCompound
|
||||
"\n" +
|
||||
",PRIMARY KEY (XPos, ZPos)" +
|
||||
");";
|
||||
try (PreparedStatement createTableStatement = this.createPreparedStatement(createTableSql))
|
||||
try (PreparedStatement createTableStatement = this.createPreparedStatement(createTableSql);
|
||||
ResultSet result = this.query(createTableStatement))
|
||||
{
|
||||
this.query(createTableStatement);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -46,9 +46,10 @@ public class TestPrimaryKeyRepo extends AbstractDhRepo<Integer, TestSingleKeyDto
|
||||
",LongValue BIGINT NULL\n" +
|
||||
",ByteValue TINYINT NULL\n" +
|
||||
");";
|
||||
try (PreparedStatement createTableStatement = this.createPreparedStatement(createTableSql))
|
||||
try (PreparedStatement createTableStatement = this.createPreparedStatement(createTableSql);
|
||||
ResultSet result = this.query(createTableStatement))
|
||||
{
|
||||
this.query(createTableStatement);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user