Expand my Community achievements bar.

SOLVED

AEM 6.5.12 - Querybuilder unclosed resourceresolver in logs

Avatar

Level 10

Hi all,

 

I have been referring to the adobe link in the link https://helpx.adobe.com/au/experience-manager/kb/Unclosed-ResourceResolver-warnng-at-com-day-cq-sear...  and the ref https://kiransg.com/2022/03/26/unclosed-resource-resolver-complete-guide-aem/
since i noticed that we have tons of unclosedresource resolvers in the code we migrated to 65. While i managed to close most, the question becomes, of the unclosed resource resolver that comes up if the hits is empty.  if the hits is not empty , this works fine. 

The service resolver is obtained within autocloseable try block via resourceresolverfactory obtained using getSlingscriptHelper 
key difference- its in wcmusepojo class

ResourceResolverFactory factory = getSlingScriptHelper().
getService(ResourceResolverFactory.class);
try(open service resolver in closeable try BLOCK){

      ResourceResolver leakingResourceResolverReference = null;
     try {
          Session session = resourceResolver.adaptTo(Session.class);
            Map<String, String> map = new HashMap<>();
           //map put goes here
            Query query = queryBuilder.createQuery(PredicateGroup.create(map), session);
             SearchResult result = query.getResult();
            for (Hit hit : result.getHits()) {
                     if(leakingResourceResolverReference == null) {
                        leakingResourceResolverReference = hit.getResource().getResourceResolver();
                     }
             //business logic
           }
     } catch(Exception e) {
         log.info("error::{}",e.getMessage());
       } finally {
            if(leakingResourceResolverReference != null){
                 leakingResourceResolverReference.close();
              }
        }
}

@Jörg_Hoh @Feike_Visser1

1 Accepted Solution

Avatar

Correct answer by
Level 10

Anyone facing the issue ,received confirmation from adobe ..  this is a bug and that has been existing with tagid and the KB still holds good since the issue exists since I guess 2017 

ignoring the info messages is the suggestion. 

 

https://helpx.adobe.com/au/experience-manager/kb/Unclosed-ResourceResolver-warnng-at-com-day-cq-sear...

View solution in original post

5 Replies

Avatar

Community Advisor

Hi @NitroHazeDev ,
According to SLING-4805[0], ResourceResolver should properly extend AutoCloseable[1] as of Sling API 2.11.0. 
So if you are using Sling API 2.11.0 you should be able to refactor all of our try-catch-finally resource resolver code to try-with-resources[2]

and my suspect is your below finally block to check 

finally {
        if(leakingResourceResolverReference != null) {
             leakingResourceResolverReference.close();
          }
    }

[0]: https://issues.apache.org/jira/browse/SLING-4805

[1]: https://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html 

[2]: https://docs.oracle.com/javase/8/docs/technotes/guides/language/try-with-resources.html

Hope that helps!

Regards,

Santosh

Avatar

Level 10

Hi @SantoshSai ,

 

Does not look like it is the cause. So there is a service resource resolver opened with a try - autoclosable block as shown. Within it, is a QB session picked from the resourceresolver obtained with autocloseable block. If the finally is not added, there are unclosed sessions.With the closing of QB leaking resolver in the finally block, the unclosed resolver disappears, provided- hits exist , else it throws the info in logs.

Unsure if this is a wcmusepojo issue where service resolvers are opened and used, i have never seen this occur on a service tbh possibly since Querybuilder is referenced using @Reference.. anyone knows? 

Update- 

I stand corrected, QB resolver must be closed as well in a servlet as i see to avoid unclosed sessions. The only worry is why is the wcmusepojo not following it

Avatar

Level 10

 after digging found that 
the below query is the culprit anyone knows why? tagid vs property on 6.5.12 ootb as well.. got to wait for the message to pop up in error log 

Throws unclosed resourceresolver in query debugger
path=/content
group.1_tagid.property=cq:tags
group.1_tagid=properties:style/color
group.2_tagid.property=cq:tags
group.2_tagid=properties:style/monochrome
orderby=path
group.p.or=false
p.offset=0
p.limit=-1

This one also throws the warning
path=/content/
tagid.property=@jcr:content/cq:tags
tagid=properties:style/color
p.offset=0
p.limit=-1



Does not throw unclosed resource resolver in Query builder
path=/content
group.1_property=jcr:content/@cq:tags
group.1_property.value=properties:style/color
group.2_property=jcr:content/@cq:tags
group.2_property.value=properties:style/monochrome
orderby=path
group.p.or=true
p.offset=0
p.limit=-1

@Jörg_Hoh @Feike_Visser1

Avatar

Correct answer by
Level 10

Anyone facing the issue ,received confirmation from adobe ..  this is a bug and that has been existing with tagid and the KB still holds good since the issue exists since I guess 2017 

ignoring the info messages is the suggestion. 

 

https://helpx.adobe.com/au/experience-manager/kb/Unclosed-ResourceResolver-warnng-at-com-day-cq-sear...