Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Jcr Session is Closed

Avatar

Level 6

Hi All,

I am facing issue in session.logout. My code calls one method frequently, first time it runs successfully, next time it says session is closed. below is the finally clause for my method. Shall i not logout the session? Actually i don't want to leave the session in open state, if i have to keep it open, i would have end up leaving lot of open session objects.

Session session = null;

try {

session = resourceResolver.adaptTo(Session.class);

doSomething

} catch(Exception ex) {

finally {

            if(session != null) {
                session.logout();
            }
        }

Please advise how to resolve the issue?

1 Accepted Solution

Avatar

Correct answer by
Administrator

Hi 

Please have a look at this old forum post:- http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manage...

//1) Do not call session.logout, but resourceResolver.close(); as the resourceResolver already has the connection to the repository (it basically wraps the session) and you request it from the RRFactory, you should close it, instead of the session you adapted from it.

2) Do you have this code in a JSP? Then the <defineObjects/> tag will provide you with a variable "resourceResolver" which contains the resourceResolver for the complete request. And if you close the session, the connection to the repository is gone, which explains the stack-trace you get.

 

Reference Articles:-

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

//The basic rule of ownership is: If you open a JCR session of a Sling ResourceResolver, you are also responsible for closing it. On the other hand: If you get passed a ResourceResolver or a Session object, do not call logout() or close() on it. 

 

Link:- http://tech.ethomasjoseph.com/2015/09/sling-who-is-closing-my-jcr-session.html

 

I hope this will help you.

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

View solution in original post

4 Replies

Avatar

Correct answer by
Administrator

Hi 

Please have a look at this old forum post:- http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manage...

//1) Do not call session.logout, but resourceResolver.close(); as the resourceResolver already has the connection to the repository (it basically wraps the session) and you request it from the RRFactory, you should close it, instead of the session you adapted from it.

2) Do you have this code in a JSP? Then the <defineObjects/> tag will provide you with a variable "resourceResolver" which contains the resourceResolver for the complete request. And if you close the session, the connection to the repository is gone, which explains the stack-trace you get.

 

Reference Articles:-

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

//The basic rule of ownership is: If you open a JCR session of a Sling ResourceResolver, you are also responsible for closing it. On the other hand: If you get passed a ResourceResolver or a Session object, do not call logout() or close() on it. 

 

Link:- http://tech.ethomasjoseph.com/2015/09/sling-who-is-closing-my-jcr-session.html

 

I hope this will help you.

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

Avatar

Level 3

Just a quick update on this DO NOT expect that resourceResolver.close(); will close your session as it will not. Close your session manually. New behaviour was implemented and resourceResolver.close(); does not close session anymore and your sever will fill up stale sessions untill its hangs or you reboot it.

So in short YOU MUST run session.logout before you run resourceResolver.close();

good luck!

Avatar

Employee Advisor

MaxBarrass​: This must be a recent change, because for me whenever I requested a resourceResolver from the factory, it was sufficient to close the resourceResolver (even when I adapted it internally to a session and worked with the JCR API). Do you have some resource pointing to this change?

Avatar

Employee Advisor

If you have a ResourceResolver already, adapting it to a Session does not open a new session; it just exposes the internal JCR session used by the ResourceResolver. Therefor you must not close this session.