Skip to content

Commit 44465bf

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 4605cd2 commit 44465bf

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

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

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

317317
StoredProcedureJpaQuery storedProcedureJpaQuery = (StoredProcedureJpaQuery) jpaQuery;
318+
318319
StoredProcedureQuery storedProcedure = storedProcedureJpaQuery.createQuery(accessor);
319320

320-
boolean returnsResultSet = storedProcedure.execute();
321+
try {
322+
323+
boolean returnsResultSet = storedProcedure.execute();
324+
325+
if (returnsResultSet) {
321326

322-
if (returnsResultSet) {
327+
if (!SurroundingTransactionDetectorMethodInterceptor.INSTANCE.isSurroundingTransactionActive()) {
328+
throw new InvalidDataAccessApiUsageException(NO_SURROUNDING_TRANSACTION);
329+
}
323330

324-
if (!SurroundingTransactionDetectorMethodInterceptor.INSTANCE.isSurroundingTransactionActive()) {
325-
throw new InvalidDataAccessApiUsageException(NO_SURROUNDING_TRANSACTION);
331+
if (storedProcedureJpaQuery.getQueryMethod().isCollectionQuery()) {
332+
return storedProcedure.getResultList();
333+
} else {
334+
return storedProcedure.getSingleResult();
335+
}
326336
}
327337

328-
if (storedProcedureJpaQuery.getQueryMethod().isCollectionQuery()) {
329-
return storedProcedure.getResultList();
330-
} else {
331-
return storedProcedure.getSingleResult();
338+
return storedProcedureJpaQuery.extractOutputValue(storedProcedure);
339+
340+
} finally {
341+
342+
if (storedProcedure instanceof AutoCloseable autoCloseable) {
343+
try {
344+
autoCloseable.close();
345+
} catch (Exception ignored) {}
332346
}
333347
}
334-
335-
return storedProcedureJpaQuery.extractOutputValue(storedProcedure);
336348
}
337349
}
338350

0 commit comments

Comments
 (0)