AEM 6.4 Correct "ResourceResolver" closing | Community
Skip to main content
Oleh_Shulha
March 20, 2019
Solved

AEM 6.4 Correct "ResourceResolver" closing

  • March 20, 2019
  • 1 reply
  • 1321 views

Hello all,

We encountered a minor problem with the Sling ResourceResolver. There is a small util method that should close the resolver, taking it as an argument. Inside the method, we check the resolver for null and resolver.isLive () and if everything is ok we call resolver.close().

And the problem is that if the resolver was adapted to the session and the session was closed before the resolver was closed, resolver.isLive() will return false, although the resolver itself is not closed in fact.

Actually the question is what practice of closing the resolver is the most correct. Maybe you should remove the check for isLive () from the utility method, or it may be possible to make a check whether the resolver is closed, something like "isClosed()" if it is available (I did not find it in the standard functionality).

The documentation says that we can call close() on a resolver many times, however in earlier versions this action threw an exception. I would like to be sure that it is safe and will not cause any errors. It would be ideal to check if the resolver is closed, and if not, call the close() method.

Thanks in advance.

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 Gaurav-Behl

Check this -- CQ development patterns – Sling ResourceResolver and JCR sessions | Things on a content management system 

Update (2018-10-26): 5th rule: Adapting a ResourceResolver to a session object does not create a new session!

When you have an already open ResourceResolver, adapting it to a Session.class will just expose the internally used JCR session; it will not create a new Session for it. Therefor you must not close this session.

1 reply

Gaurav-Behl
Gaurav-BehlAccepted solution
Level 10
March 20, 2019

Check this -- CQ development patterns – Sling ResourceResolver and JCR sessions | Things on a content management system 

Update (2018-10-26): 5th rule: Adapting a ResourceResolver to a session object does not create a new session!

When you have an already open ResourceResolver, adapting it to a Session.class will just expose the internally used JCR session; it will not create a new Session for it. Therefor you must not close this session.