Skip to content

Commit c09c73b

Browse files
gregturnmp911de
authored andcommitted
Close StoredProcedureQuery if it implements AutoCloseable.
If the JPA provider's implementation of StoredProcedureQuery implements AutoCloseable, invoke the close method when completed. Resolves #2915. Original pull request: #2938
1 parent 2bf7498 commit c09c73b

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryExecution.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -302,24 +302,36 @@ protected Object doExecute(AbstractJpaQuery jpaQuery, JpaParametersParameterAcce
302302
Assert.isInstanceOf(StoredProcedureJpaQuery.class, jpaQuery);
303303

304304
StoredProcedureJpaQuery storedProcedureJpaQuery = (StoredProcedureJpaQuery) jpaQuery;
305+
305306
StoredProcedureQuery storedProcedure = storedProcedureJpaQuery.createQuery(accessor);
306307

307-
boolean returnsResultSet = storedProcedure.execute();
308+
try {
309+
310+
boolean returnsResultSet = storedProcedure.execute();
311+
312+
if (returnsResultSet) {
308313

309-
if (returnsResultSet) {
314+
if (!SurroundingTransactionDetectorMethodInterceptor.INSTANCE.isSurroundingTransactionActive()) {
315+
throw new InvalidDataAccessApiUsageException(NO_SURROUNDING_TRANSACTION);
316+
}
310317

311-
if (!SurroundingTransactionDetectorMethodInterceptor.INSTANCE.isSurroundingTransactionActive()) {
312-
throw new InvalidDataAccessApiUsageException(NO_SURROUNDING_TRANSACTION);
318+
if (storedProcedureJpaQuery.getQueryMethod().isCollectionQuery()) {
319+
return storedProcedure.getResultList();
320+
} else {
321+
return storedProcedure.getSingleResult();
322+
}
313323
}
314324

315-
if (storedProcedureJpaQuery.getQueryMethod().isCollectionQuery()) {
316-
return storedProcedure.getResultList();
317-
} else {
318-
return storedProcedure.getSingleResult();
325+
return storedProcedureJpaQuery.extractOutputValue(storedProcedure);
326+
327+
} finally {
328+
329+
if (storedProcedure instanceof AutoCloseable autoCloseable) {
330+
try {
331+
autoCloseable.close();
332+
} catch (Exception ignored) {}
319333
}
320334
}
321-
322-
return storedProcedureJpaQuery.extractOutputValue(storedProcedure);
323335
}
324336
}
325337

0 commit comments

Comments
 (0)