Expand my Community achievements bar.

SOLVED

When to close ResourceResolver instances?

Avatar

Former Community Member

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

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

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

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

View solution in original post

5 Replies

Avatar

Level 10

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

Avatar

Employee

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

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

Avatar

Former Community Member

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?)?

Avatar

Correct answer by
Employee Advisor

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

Avatar

Level 2

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.