Closing Resource Resolver | Community
Skip to main content
Radha_Krishna_N
Level 3
July 19, 2018
Solved

Closing Resource Resolver

  • July 19, 2018
  • 8 replies
  • 7423 views

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?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

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

8 replies

Adobe Employee
July 19, 2018

Hello Radha,

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

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

{

resourceResolver.close();

}

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
July 19, 2018

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
Radha_Krishna_N
Level 3
July 19, 2018

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

smacdonald2008
Level 10
July 19, 2018

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

Radha_Krishna_N
Level 3
July 19, 2018

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

Ratna_Kumar
Level 10
July 19, 2018

Hi Radha,

Scott is right.

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

Thanks,

Ratna Kumar.

Radha_Krishna_N
Level 3
July 19, 2018

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.

Level 2
November 13, 2019

I dont think you need to close it. ResourceResolver is Autoclosable.

ResourceResolver ("The Adobe AEM Quickstart and Web Application.")