AEM 6.4 Correct "ResourceResolver" closing



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.

Accepted Solutions (1)

Accepted Solutions (1)




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

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.

Answers (0)