Coding best practice : Lucene search query : resultSet.close()

Just want to share a very important coding best practice if you are using Lucence search query in Alfresco : do not forget to close the ResultSet object ( at the end of your treatments.

This is very important to avoid disk space issue (and also probably memory leak issue). Indeed, if the ResultSet object is not closed properly, then it seems that Lucene will have to “keep it on disk” as a “piece of Lucene index” (serialization here ?) and this block will not be removed by the “index cleaner process”. So space of Lucene index will continue to grow indefinitely….

There are a lots of  ” ” here, but I think you understand that this could be a major issue.

Code sample:

try {

 ResultSet resultSet = null;
 resultSet = searchService.query(
 Repository.getStoreRef(), SearchService.LANGUAGE_LUCENE, query.toString());           
 List<NodeRef> nodes = resultSet.getNodeRefs();
 for (int index=0; index<nodes.size(); index++) {
 //Do whatever you want here

catch (Throwable err) {
finally {
         if (resultSet != null)


5 Responses to Coding best practice : Lucene search query : resultSet.close()

  1. Thanks so much for taking the time to share this with the Alfresco community. Your blog is a great asset to those who are learning Alfresco.

    Nancy Garrity
    Alfresco Community Manager

    • Enguerrand SPINDLER says:

      Thanks for your support Nancy…I’m a big fan of Alfresco, and I will continue to share my comments as often as I can.

  2. […] practice : Lucene search query : resultSet.close() : part2 To continue on the same topic (see Part1 of this […]

  3. Enguerrand SPINDLER says:

    This is already documented on the wiki:

    “try .. finally pattern:

    The try..finally pattern above must be followed. If result sets are left unclosed they will hold on to the underlying files in the lucene index. Although merging can go ahead, the files will most likely not be deleted. The number of index directories will rise and no merged index segments will be deleted, so the on disk size will also rise. Restarting will clean up these directories in later versions of Alfresco but if you do not fix the code to follow the required pattern issue will recur.

    At some point the result set will get bound to the transaction and closed automatically after the transaction commits.”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: