Search code examples
javajava-8xquerymarklogicmarklogic-10

In MarkLogic should you close EvaluResultIterator if it is unused or contains the empty sequence?


I am using the java-api from MarkLogic to evaluate XQueries. Sometimes these XQueries return nothing(the empty sequence) or the result is not used.

According to the documentation(https://docs.marklogic.com/javadoc/client/com/marklogic/client/eval/ServerEvaluationCall.html):

NOTE: EvalResultIterator MUST BE CLOSED. If you call eval() don't forget to call close() on the returned EvalResultIterator to free up the underlying resources.

Of course this does not look very nice when you do this with an empty try with resources

try (EvalResultIterator eval = invoker.eval()) {
    // No result is returned    
}

Is it really necessary to close the EvalResultIterator in this case and what is the best way to do so?


Solution

  • Just call invoker.eval().close(). If invoker.eval() throws an exception, then no EvalResultIterator will be returned to close, so no need to worry about a finally block.