Skip to content

Commit 192ba02

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 6c315a3 commit 192ba02

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

+22-10
Original file line numberDiff line numberDiff line change
@@ -332,24 +332,36 @@ protected Object doExecute(AbstractJpaQuery jpaQuery, JpaParametersParameterAcce
332332
Assert.isInstanceOf(StoredProcedureJpaQuery.class, jpaQuery);
333333

334334
StoredProcedureJpaQuery storedProcedureJpaQuery = (StoredProcedureJpaQuery) jpaQuery;
335+
335336
StoredProcedureQuery storedProcedure = storedProcedureJpaQuery.createQuery(accessor);
336337

337-
boolean returnsResultSet = storedProcedure.execute();
338+
try {
339+
340+
boolean returnsResultSet = storedProcedure.execute();
341+
342+
if (returnsResultSet) {
338343

339-
if (returnsResultSet) {
344+
if (!SurroundingTransactionDetectorMethodInterceptor.INSTANCE.isSurroundingTransactionActive()) {
345+
throw new InvalidDataAccessApiUsageException(NO_SURROUNDING_TRANSACTION);
346+
}
340347

341-
if (!SurroundingTransactionDetectorMethodInterceptor.INSTANCE.isSurroundingTransactionActive()) {
342-
throw new InvalidDataAccessApiUsageException(NO_SURROUNDING_TRANSACTION);
348+
if (storedProcedureJpaQuery.getQueryMethod().isCollectionQuery()) {
349+
return storedProcedure.getResultList();
350+
} else {
351+
return storedProcedure.getSingleResult();
352+
}
343353
}
344354

345-
if (storedProcedureJpaQuery.getQueryMethod().isCollectionQuery()) {
346-
return storedProcedure.getResultList();
347-
} else {
348-
return storedProcedure.getSingleResult();
355+
return storedProcedureJpaQuery.extractOutputValue(storedProcedure);
356+
357+
} finally {
358+
359+
if (storedProcedure instanceof AutoCloseable autoCloseable) {
360+
try {
361+
autoCloseable.close();
362+
} catch (Exception ignored) {}
349363
}
350364
}
351-
352-
return storedProcedureJpaQuery.extractOutputValue(storedProcedure);
353365
}
354366
}
355367

0 commit comments

Comments
 (0)