Skip to content

Commit

Permalink
eclipse-rdf4jGH-4968: server-spring - Close query result on pre-rende…
Browse files Browse the repository at this point in the history
…r exception

- Previously between getting the query response and rendering it
  to the desired output an exception could occur (e.g. due to unsupported
  result format) which would leave the result un-closed. This in turn
  would sometimes trigger an exception on closing the associated
  repo connection.
  • Loading branch information
vtermanis committed May 17, 2024
1 parent d878c68 commit f9ed099
Showing 1 changed file with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public ModelAndView handleQueryRequest(HttpServletRequest request, RequestMethod
HttpServletResponse response) throws HTTPException, IOException {

RepositoryConnection repositoryCon = null;
Object queryResponse = null;

try {
Repository repository = repositoryResolver.getRepository(request);
Expand All @@ -75,9 +76,6 @@ public ModelAndView handleQueryRequest(HttpServletRequest request, RequestMethod
boolean distinct = isDistinct(request);

try {

Object queryResponse;

if (headersOnly) {
queryResponse = null;
} else {
Expand Down Expand Up @@ -114,10 +112,22 @@ public ModelAndView handleQueryRequest(HttpServletRequest request, RequestMethod
}

} catch (Exception e) {
// only close the connection when an exception occurs. Otherwise, the QueryResultView will take care of
// closing it.
if (repositoryCon != null) {
repositoryCon.close();
// only close the response & connection when an exception occurs. Otherwise, the QueryResultView will take
// care of closing it.
try {
if (queryResponse instanceof AutoCloseable) {
((AutoCloseable) queryResponse).close();
}
} catch (Exception qre) {
logger.warn("Query response closing error", qre);
} finally {
try {
if (repositoryCon != null) {
repositoryCon.close();
}
} catch (Exception qre) {
logger.warn("Connection closing error", qre);
}
}
throw e;
}
Expand Down

0 comments on commit f9ed099

Please sign in to comment.