Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

Closing Resource Resolver

Radha_Krishna_N
Level 4
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
Arun_Patidar
Correct answer by
Community Advisor
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

View solution in original post

8 Replies
Vish_dhaliwal
Employee
Employee

Hello Radha,

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

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

{

resourceResolver.close();

}

Arun_Patidar
Correct answer by
Community Advisor
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

View solution in original post

Radha_Krishna_N
Level 4
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

smacdonald2008
Level 10
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

Radha_Krishna_N
Level 4
Level 4

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

Ratna_Kumar
Community Advisor
Community Advisor

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 4
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.