When to close ResourceResolver instances? | Community
Skip to main content
October 16, 2015
Solved

When to close ResourceResolver instances?

  • October 16, 2015
  • 5 replies
  • 5444 views

This is probably a silly question, but in what cases does a developer actually have to call close() on a ResourceResolver instance?

I keep reading that this is necessary [1] [2], but when using WCMUse's getResourceResolver() helper function in AEM6 it doesn't seem to be necessary any more. We also have some cases where we get the RR from the request and this would break the site because it prematurely closes the resource resolver:

ResourceResolver resourceResolver = getResourceResolver(); resourceResolver.close();

[1] https://cqdump.wordpress.com/2013/07/23/cq-development-patterns-sling-resourceresolver-and-jcr-sessions/

[2] https://docs.adobe.com/docs/en/aem/6-0/develop/ref/javadoc/index.html?org/apache/sling/api/resource/ResourceResolver.html

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 joerghoh

Hi,

no, request.getResourceResolver() or request.adaptTo(ResourceResolver.class) do not create a new ResourceResolver.

The most used way to create a new ResourceResolver is using the ResourceResolverFactory.

kind regards,
Jörg

5 replies

Lokesh_Shivalingaiah
New Participant
October 16, 2015

you should be responsible to close the resourceResolver session which is initiated by you. You can make sure that you close your RR session using 'finally' block

Feike_Visser1
Employee
October 16, 2015

Follow the rule "You open a resourceResovler / Session, You Close it". 

If this is not the case, leave it...

October 16, 2015

OK, thanks. So just to confirm, "getResourceResolver()" or adaptTo(ResourceResolver.class) would not actually create a new instance, right? What's an example of a manually created ResourceResolver (factory?)?

joerghoh
joerghohAccepted solution
Employee
October 16, 2015

Hi,

no, request.getResourceResolver() or request.adaptTo(ResourceResolver.class) do not create a new ResourceResolver.

The most used way to create a new ResourceResolver is using the ResourceResolverFactory.

kind regards,
Jörg

New Participant
October 16, 2015

Typically, any code in which a resource resolver is not tied to a request, you will need to create/close.

 

So, for example, if you have a service running in the background; one thats maybe on a schedule.

There's no request whose resource-resolver you can re-use, so you create one manually directly from the factory for use by this service.

You will be responsible for closing this resource resolver/session.