When to close ResourceResolver instances?

Avatar

Avatar

paro22

Avatar

paro22

paro22

15-10-2015

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

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

942

Correct Reply

1.0K

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

942

Correct Reply

1.0K
Jörg_Hoh
Employee

15-10-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

Answers (4)

Answers (4)

Avatar

Avatar

kgio

Avatar

kgio

kgio

15-10-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. 

Avatar

Avatar

paro22

Avatar

paro22

paro22

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

Avatar

Avatar

Feike_Visser1

Employee

Avatar

Feike_Visser1

Employee

Feike_Visser1
Employee

15-10-2015

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

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

Avatar

Avatar

bsloki

MVP

Avatar

bsloki

MVP

bsloki
MVP

15-10-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