Closing Resource Resolver | Community
Skip to main content
Radha_Krishna_N
Level 3
Jul 19, 2018 3:55 PM UTC
Solved

Closing Resource Resolver

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.
arunpatidar
arunpatidarAccepted solution
New Participant
Jul 19, 2018 4:06 PM UTC

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

8 replies

Level 2
Nov 13, 2019 4:15 PM UTC

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

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

Translate
Radha_Krishna_N
Level 3
Jul 19, 2018 6:22 PM UTC

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.

Translate
Ratna_Kumar
Level 10
Jul 19, 2018 6:15 PM UTC

Hi Radha,

Scott is right.

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

Thanks,

Ratna Kumar.

Translate
Radha_Krishna_N
Level 3
Jul 19, 2018 6:03 PM UTC

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

Translate
smacdonald2008
Level 10
Jul 19, 2018 5:55 PM UTC

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

Translate
Radha_Krishna_N
Level 3
Jul 19, 2018 5:49 PM UTC

Thanks arunp99088702​ Vish.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.

kautuksahni​ smacdonald2008​

Translate
arunpatidar
arunpatidarAccepted solution
New Participant
Jul 19, 2018 4:06 PM UTC

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

Translate
Arun Patidar
Employee
Jul 19, 2018 4:04 PM UTC

Hello Radha,

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

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

{

resourceResolver.close();

}

Translate