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) {