Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Closing Resource Resolver

Avatar

Level 4

Hi,

In AEM 6.1,  we do not have to close the resolver if we are getting resource resolver from sling request or workflow session.

But in an OSGI component if we are getting the ResourceResolver from ResourceResolverFactory as below

@Reference

ResourceResolverFactory rrf;

...

public String getData() {

     ResourceResolver rr = null;

     Map<String, Object> param = ...

     param.put(ResourceResolverFactory.SUBSERVICE, "sub-service");

     try {

          rr = rrf.getServiceResourceResolver(param);

          ...

     }

     ...

}

Do we have to close the resolver?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

if you open it, you are responsible for closing it

if you use a reference to the resourceResolver, then it is not your responsibility to close it

  • Therefore, your code should open & close in the same scope.
  • If you would obtain a resourceResolver FROM a request or workflow session, you did not open the resolver and you do not need to close it.

More info

Scrupulo

Thanks

Arun



Arun Patidar

View solution in original post

8 Replies

Avatar

Employee

Hello Radha,

Yes, it is recommended to always close it via ResourceResolver.close()

if(resourceResolver != null && resourceResolver.isLive())

{

resourceResolver.close();

}

Avatar

Correct answer by
Community Advisor

if you open it, you are responsible for closing it

if you use a reference to the resourceResolver, then it is not your responsibility to close it

  • Therefore, your code should open & close in the same scope.
  • If you would obtain a resourceResolver FROM a request or workflow session, you did not open the resolver and you do not need to close it.

More info

Scrupulo

Thanks

Arun



Arun Patidar

Avatar

Level 4

Thanks arunp99088702Vish.dhaliwal​ for your inputs.

But in most of the community articles which gets resource resolver(rr) as above, either they mention a note about closing rr or use administrative rr. I think the new articles should stop using the old approach. This would help the people who are new to AEM community.

kautuksahnismacdonald2008

Avatar

Level 10

We agree - new ones will always show this way.

Map<String, Object> param = new HashMap<String, Object>();

param.put(ResourceResolverFactory.SUBSERVICE, "datawrite");

ResourceResolver resolver = null;


try {

          

    //Invoke the adaptTo method to create a Session used to create a QueryManager

resolver = resolverFactory.getServiceResourceResolver(param);

As shown here in this recent 6.4 community article: Scott's Digital Community: Querying Adobe Experience Manager 6.4 JCR data

Avatar

Level 4

smacdonald2008 Should the resource resolver be closed or get it using try-with-resources (AEM 6.2 onwards)?

Avatar

Level 10

Hi Radha,

Scott is right.

It is recommended that to use the Sling mapping to get the resolver.

Thanks,

Ratna Kumar.

Avatar

Level 4

Ratna Kumar​ Yes Scott has shown how to get the resolver in an OSGI component. But the article is not showing how to close it.