Skip to content

Commit

Permalink
fix: result set leak (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
mishraomp authored May 23, 2024
1 parent 28ab006 commit 9043530
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/main/java/ca/bc/gov/nrs/api/service/QueryExecutorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,22 @@ public List<Map<String, Object>> 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<String> columnNames = new ArrayList<>();
for (int i = 1; i <= metaData.getColumnCount(); i++) {
String columnName = metaData.getColumnName(i);
columnNames.add(columnName);
}
while (result.next()) {
Map<String, Object> 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<String> columnNames = new ArrayList<>();
for (int i = 1; i <= metaData.getColumnCount(); i++) {
String columnName = metaData.getColumnName(i);
columnNames.add(columnName);
}
while (result.next()) {
Map<String, Object> 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) {
Expand Down

0 comments on commit 9043530

Please sign in to comment.