From 90435308ccbc31278a9d4aeeac054cb7c716e175 Mon Sep 17 00:00:00 2001 From: Om Mishra <32200996+mishraomp@users.noreply.github.com> Date: Thu, 23 May 2024 10:25:46 -0700 Subject: [PATCH] fix: result set leak (#72) --- .../nrs/api/service/QueryExecutorService.java | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/main/java/ca/bc/gov/nrs/api/service/QueryExecutorService.java b/src/main/java/ca/bc/gov/nrs/api/service/QueryExecutorService.java index 0367774..99fccd4 100644 --- a/src/main/java/ca/bc/gov/nrs/api/service/QueryExecutorService.java +++ b/src/main/java/ca/bc/gov/nrs/api/service/QueryExecutorService.java @@ -36,21 +36,22 @@ public List> executeQuery(String query) throws SQLException try (var connection = dataSource.getConnection()) { connection.setReadOnly(true); try (var statement = connection.prepareStatement(query)) { - var result = statement.executeQuery(); - var metaData = result.getMetaData(); - List columnNames = new ArrayList<>(); - for (int i = 1; i <= metaData.getColumnCount(); i++) { - String columnName = metaData.getColumnName(i); - columnNames.add(columnName); - } - while (result.next()) { - Map row = new HashMap<>(); - for (int i = 0; i < metaData.getColumnCount(); i++) { - String columnName = columnNames.get(i); - Object columnValue = result.getObject(i + 1); - row.put(columnName, columnValue); + try(var result = statement.executeQuery()){ + var metaData = result.getMetaData(); + List columnNames = new ArrayList<>(); + for (int i = 1; i <= metaData.getColumnCount(); i++) { + String columnName = metaData.getColumnName(i); + columnNames.add(columnName); + } + while (result.next()) { + Map row = new HashMap<>(); + for (int i = 0; i < metaData.getColumnCount(); i++) { + String columnName = columnNames.get(i); + Object columnValue = result.getObject(i + 1); + row.put(columnName, columnValue); + } + results.add(row); } - results.add(row); } } } catch (Exception e) {