diff --git a/app/src/main/java/io/apicurio/registry/storage/impl/sql/jdb/MappedQueryImpl.java b/app/src/main/java/io/apicurio/registry/storage/impl/sql/jdb/MappedQueryImpl.java index 025850e408..8c220d7c3d 100644 --- a/app/src/main/java/io/apicurio/registry/storage/impl/sql/jdb/MappedQueryImpl.java +++ b/app/src/main/java/io/apicurio/registry/storage/impl/sql/jdb/MappedQueryImpl.java @@ -36,7 +36,6 @@ public class MappedQueryImpl implements MappedQuery, Closeable { final PreparedStatement statement; final RowMapper mapper; - final ResultSet resultSet; /** * Constructor. @@ -47,7 +46,6 @@ public class MappedQueryImpl implements MappedQuery, Closeable { public MappedQueryImpl(PreparedStatement statement, RowMapper mapper) throws SQLException { this.statement = statement; this.mapper = mapper; - this.resultSet = statement.executeQuery(); } /** @@ -56,18 +54,21 @@ public MappedQueryImpl(PreparedStatement statement, RowMapper mapper) throws @Override public T one() { T rval = null; - try { - if (this.resultSet.next()) { + try (ResultSet resultSet = statement.executeQuery()) { + if (resultSet.next()) { rval = this.mapper.map(resultSet); - if (this.resultSet.next()) { + if (resultSet.next()) { throw new RuntimeSqlException("SQL error: Expected only one result but got multiple."); } - } else { + } + else { throw new RuntimeSqlException("SQL error: Expected only one result row but got none."); } - } catch (SQLException e) { + } + catch (SQLException e) { throw new RuntimeSqlException(e); - } finally { + } + finally { close(); } return rval; @@ -79,15 +80,18 @@ public T one() { @Override public T first() { T rval = null; - try { - if (this.resultSet.next()) { + try (ResultSet resultSet = statement.executeQuery()) { + if (resultSet.next()) { rval = this.mapper.map(resultSet); - } else { + } + else { throw new RuntimeSqlException("SQL error: Expected AT LEAST one result row but got none."); } - } catch (SQLException e) { + } + catch (SQLException e) { throw new RuntimeSqlException(e); - } finally { + } + finally { close(); } return rval; @@ -99,18 +103,21 @@ public T first() { @Override public Optional findOne() { Optional rval; - try { - if (this.resultSet.next()) { + try (ResultSet resultSet = statement.executeQuery()) { + if (resultSet.next()) { rval = Optional.of(this.mapper.map(resultSet)); - if (this.resultSet.next()) { + if (resultSet.next()) { throw new RuntimeSqlException("SQL error: Expected only one result but got multiple."); } - } else { + } + else { rval = Optional.empty(); } - } catch (SQLException e) { + } + catch (SQLException e) { throw new RuntimeSqlException(e); - } finally { + } + finally { close(); } return rval; @@ -122,15 +129,18 @@ public Optional findOne() { @Override public Optional findFirst() { Optional rval = null; - try { - if (this.resultSet.next()) { + try (ResultSet resultSet = statement.executeQuery()) { + if (resultSet.next()) { rval = Optional.of(this.mapper.map(resultSet)); - } else { + } + else { rval = Optional.empty(); } - } catch (SQLException e) { + } + catch (SQLException e) { throw new RuntimeSqlException(e); - } finally { + } + finally { close(); } return rval; @@ -142,16 +152,18 @@ public Optional findFirst() { @Override public Optional findLast() { Optional rval = null; - try { - while (this.resultSet.next()) { + try (ResultSet resultSet = statement.executeQuery()) { + while (resultSet.next()) { rval = Optional.of(this.mapper.map(resultSet)); } if (rval == null) { rval = Optional.empty(); } - } catch (SQLException e) { + } + catch (SQLException e) { throw new RuntimeSqlException(e); - } finally { + } + finally { close(); } return rval; @@ -163,14 +175,16 @@ public Optional findLast() { @Override public List list() { List rval = new LinkedList<>(); - try { - while (this.resultSet.next()) { + try (ResultSet resultSet = statement.executeQuery()) { + while (resultSet.next()) { T t = this.mapper.map(resultSet); rval.add(t); } - } catch (SQLException e) { + } + catch (SQLException e) { throw new RuntimeSqlException(e); - } finally { + } + finally { close(); } return rval; @@ -181,22 +195,38 @@ public List list() { */ @Override public Stream stream() { - return StreamSupport.stream(new Spliterators.AbstractSpliterator(Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED | Spliterator.DISTINCT | Spliterator.NONNULL) { - @Override - public boolean tryAdvance(Consumer action) { + try { + ResultSet resultSet = statement.executeQuery(); + return StreamSupport.stream( + new Spliterators.AbstractSpliterator(Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED | Spliterator.DISTINCT | Spliterator.NONNULL) { + @Override + public boolean tryAdvance(Consumer action) { + try { + if (!resultSet.next()) { + return false; + } + T t = mapper.map(resultSet); + action.accept(t); + return true; + } + catch (SQLException e) { + throw new RuntimeSqlException(e); + } + } + + }, false).onClose(() -> { try { - if (!resultSet.next()) { - return false; - } - T t = mapper.map(resultSet); - action.accept(t); - return true; - } catch (SQLException e) { - throw new RuntimeSqlException(e); + resultSet.close(); + close(); } - } - - }, false).onClose(this::close); + catch (SQLException e) { + throw new RuntimeException(e); + } + }); + } + catch (SQLException e) { + throw new RuntimeException(e); + } } /** @@ -206,7 +236,8 @@ public boolean tryAdvance(Consumer action) { public void close() { try { this.statement.close(); - } catch (SQLException e) { + } + catch (SQLException e) { throw new RuntimeSqlException(e); } }