Skip to content

Commit 4e20ef0

Browse files
authored
Close result set using try with resources (#4929)
1 parent b626efa commit 4e20ef0

File tree

1 file changed

+78
-47
lines changed

1 file changed

+78
-47
lines changed

app/src/main/java/io/apicurio/registry/storage/impl/sql/jdb/MappedQueryImpl.java

+78-47
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public class MappedQueryImpl<T> implements MappedQuery<T>, Closeable {
3636

3737
final PreparedStatement statement;
3838
final RowMapper<T> mapper;
39-
final ResultSet resultSet;
4039

4140
/**
4241
* Constructor.
@@ -47,7 +46,6 @@ public class MappedQueryImpl<T> implements MappedQuery<T>, Closeable {
4746
public MappedQueryImpl(PreparedStatement statement, RowMapper<T> mapper) throws SQLException {
4847
this.statement = statement;
4948
this.mapper = mapper;
50-
this.resultSet = statement.executeQuery();
5149
}
5250

5351
/**
@@ -56,18 +54,21 @@ public MappedQueryImpl(PreparedStatement statement, RowMapper<T> mapper) throws
5654
@Override
5755
public T one() {
5856
T rval = null;
59-
try {
60-
if (this.resultSet.next()) {
57+
try (ResultSet resultSet = statement.executeQuery()) {
58+
if (resultSet.next()) {
6159
rval = this.mapper.map(resultSet);
62-
if (this.resultSet.next()) {
60+
if (resultSet.next()) {
6361
throw new RuntimeSqlException("SQL error: Expected only one result but got multiple.");
6462
}
65-
} else {
63+
}
64+
else {
6665
throw new RuntimeSqlException("SQL error: Expected only one result row but got none.");
6766
}
68-
} catch (SQLException e) {
67+
}
68+
catch (SQLException e) {
6969
throw new RuntimeSqlException(e);
70-
} finally {
70+
}
71+
finally {
7172
close();
7273
}
7374
return rval;
@@ -79,15 +80,18 @@ public T one() {
7980
@Override
8081
public T first() {
8182
T rval = null;
82-
try {
83-
if (this.resultSet.next()) {
83+
try (ResultSet resultSet = statement.executeQuery()) {
84+
if (resultSet.next()) {
8485
rval = this.mapper.map(resultSet);
85-
} else {
86+
}
87+
else {
8688
throw new RuntimeSqlException("SQL error: Expected AT LEAST one result row but got none.");
8789
}
88-
} catch (SQLException e) {
90+
}
91+
catch (SQLException e) {
8992
throw new RuntimeSqlException(e);
90-
} finally {
93+
}
94+
finally {
9195
close();
9296
}
9397
return rval;
@@ -99,18 +103,21 @@ public T first() {
99103
@Override
100104
public Optional<T> findOne() {
101105
Optional<T> rval;
102-
try {
103-
if (this.resultSet.next()) {
106+
try (ResultSet resultSet = statement.executeQuery()) {
107+
if (resultSet.next()) {
104108
rval = Optional.of(this.mapper.map(resultSet));
105-
if (this.resultSet.next()) {
109+
if (resultSet.next()) {
106110
throw new RuntimeSqlException("SQL error: Expected only one result but got multiple.");
107111
}
108-
} else {
112+
}
113+
else {
109114
rval = Optional.empty();
110115
}
111-
} catch (SQLException e) {
116+
}
117+
catch (SQLException e) {
112118
throw new RuntimeSqlException(e);
113-
} finally {
119+
}
120+
finally {
114121
close();
115122
}
116123
return rval;
@@ -122,15 +129,18 @@ public Optional<T> findOne() {
122129
@Override
123130
public Optional<T> findFirst() {
124131
Optional<T> rval = null;
125-
try {
126-
if (this.resultSet.next()) {
132+
try (ResultSet resultSet = statement.executeQuery()) {
133+
if (resultSet.next()) {
127134
rval = Optional.of(this.mapper.map(resultSet));
128-
} else {
135+
}
136+
else {
129137
rval = Optional.empty();
130138
}
131-
} catch (SQLException e) {
139+
}
140+
catch (SQLException e) {
132141
throw new RuntimeSqlException(e);
133-
} finally {
142+
}
143+
finally {
134144
close();
135145
}
136146
return rval;
@@ -142,16 +152,18 @@ public Optional<T> findFirst() {
142152
@Override
143153
public Optional<T> findLast() {
144154
Optional<T> rval = null;
145-
try {
146-
while (this.resultSet.next()) {
155+
try (ResultSet resultSet = statement.executeQuery()) {
156+
while (resultSet.next()) {
147157
rval = Optional.of(this.mapper.map(resultSet));
148158
}
149159
if (rval == null) {
150160
rval = Optional.empty();
151161
}
152-
} catch (SQLException e) {
162+
}
163+
catch (SQLException e) {
153164
throw new RuntimeSqlException(e);
154-
} finally {
165+
}
166+
finally {
155167
close();
156168
}
157169
return rval;
@@ -163,14 +175,16 @@ public Optional<T> findLast() {
163175
@Override
164176
public List<T> list() {
165177
List<T> rval = new LinkedList<>();
166-
try {
167-
while (this.resultSet.next()) {
178+
try (ResultSet resultSet = statement.executeQuery()) {
179+
while (resultSet.next()) {
168180
T t = this.mapper.map(resultSet);
169181
rval.add(t);
170182
}
171-
} catch (SQLException e) {
183+
}
184+
catch (SQLException e) {
172185
throw new RuntimeSqlException(e);
173-
} finally {
186+
}
187+
finally {
174188
close();
175189
}
176190
return rval;
@@ -181,22 +195,38 @@ public List<T> list() {
181195
*/
182196
@Override
183197
public Stream<T> stream() {
184-
return StreamSupport.stream(new Spliterators.AbstractSpliterator<T>(Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED | Spliterator.DISTINCT | Spliterator.NONNULL) {
185-
@Override
186-
public boolean tryAdvance(Consumer<? super T> action) {
198+
try {
199+
ResultSet resultSet = statement.executeQuery();
200+
return StreamSupport.stream(
201+
new Spliterators.AbstractSpliterator<T>(Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED | Spliterator.DISTINCT | Spliterator.NONNULL) {
202+
@Override
203+
public boolean tryAdvance(Consumer<? super T> action) {
204+
try {
205+
if (!resultSet.next()) {
206+
return false;
207+
}
208+
T t = mapper.map(resultSet);
209+
action.accept(t);
210+
return true;
211+
}
212+
catch (SQLException e) {
213+
throw new RuntimeSqlException(e);
214+
}
215+
}
216+
217+
}, false).onClose(() -> {
187218
try {
188-
if (!resultSet.next()) {
189-
return false;
190-
}
191-
T t = mapper.map(resultSet);
192-
action.accept(t);
193-
return true;
194-
} catch (SQLException e) {
195-
throw new RuntimeSqlException(e);
219+
resultSet.close();
220+
close();
196221
}
197-
}
198-
199-
}, false).onClose(this::close);
222+
catch (SQLException e) {
223+
throw new RuntimeException(e);
224+
}
225+
});
226+
}
227+
catch (SQLException e) {
228+
throw new RuntimeException(e);
229+
}
200230
}
201231

202232
/**
@@ -206,7 +236,8 @@ public boolean tryAdvance(Consumer<? super T> action) {
206236
public void close() {
207237
try {
208238
this.statement.close();
209-
} catch (SQLException e) {
239+
}
240+
catch (SQLException e) {
210241
throw new RuntimeSqlException(e);
211242
}
212243
}

0 commit comments

Comments
 (0)