9.8 Closing a Query

When you are finished with the result of a query, you can close the results, allowing the release of resources used in implementing the query (e.g., database cursors or iterators). You can use the following Query methods to close query results:

void close(Object queryResult);
void closeAll(  );

The close( ) method closes the result that was returned by one call to execute( ). You use closeAll( ) to close all the results from calls to execute( ) on the Query instance. Both methods release the query result's resources. After they complete, you cannot use the query result (e.g., to iterate the returned elements). Closing a query result does not affect the state of its instances. Once you have closed a result, any Iterator that was acquired returns false to hasNext( ) and throws NoSuchElementException if next( ) is called. But the Query instance is still valid and can be used to execute more queries. Each query example in this chapter closed its query result.