Expand my Community achievements bar.

JCR RepositoryException - this session has been closed

Avatar

Level 2

Below is the code: 

        ResourceResolver resourceResolver;
        Session session = null;
        List<Hit> resultList = null;
        
        try {
            
            resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
            session = resourceResolver.adaptTo(Session.class);
            
            Query query = builder.loadQuery(savedPredicatePath, session);
            resultList = query.getResult().getHits();
            
        } multiple catchers...
        
        } finally {
            // null checker and if still alive, logout the session
            if (session != null && session.isLive()) {

                session.logout();
            }
        }

Comments

1. if i do not explicitly logout the session, i do not get the exception, I do not want to leave open sessions in the code.
2. I did some research, it looks this was an issue with CQ code, and I found an adobe page recommending patches, but, that was for CQ 5.3. I'm on 5.6.1, do i still need to patch?
3. URLs: 
http://helpx.adobe.com/experience-manager/kb/CQ53ConcurrentModificationLeadsToClosedJCRSession.html
http://forums.adobe.com/message/4786192

Below is the stack trace of the exception that I'm getting: 

Caused by: javax.jcr.RepositoryException: This session has been closed. See the chained exception for a trace of where the session was closed.
    at org.apache.jackrabbit.core.session.SessionState.checkAlive(SessionState.java:150)
    at org.apache.jackrabbit.core.ItemManager.sanityCheck(ItemManager.java:138)
    at org.apache.jackrabbit.core.ItemManager.getItem(ItemManager.java:326)
    at org.apache.jackrabbit.core.ItemManager.getItem(ItemManager.java:622)
    at org.apache.jackrabbit.core.query.lucene.RowIteratorImpl$RowImpl.getNode(RowIteratorImpl.java:411)
    at org.apache.jackrabbit.core.query.lucene.RowIteratorImpl$RowImpl.getValue(RowIteratorImpl.java:333)
    at com.day.cq.search.impl.result.HitImpl.getPath(HitImpl.java:120)
    at org.apache.jsp.apps.sm.components.commons.profile.layouts.custom.querybuilder_jsp._jspService(querybuilder_jsp.java:189)
    at org.apache.sling.scripting.jsp.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:502)
    ... 244 more
Caused by: java.lang.Exception: Stack trace of  where session-admin-204340 was originally closed
    at org.apache.jackrabbit.core.session.SessionState.close(SessionState.java:275)
    at org.apache.jackrabbit.core.SessionImpl.logout(SessionImpl.java:943)
    at org.apache.jackrabbit.core.XASessionImpl.logout(XASessionImpl.java:392)
    at com.day.crx.core.CRXSessionImpl.logout(CRXSessionImpl.java:127)
    at edu.stanford.med.impl.SearchServiceImpl.getSearchHits(SearchServiceImpl.java:93)
    at org.apache.jsp.apps.sm.components.commons.profile.layouts.custom.querybuilder_jsp._jspService(querybuilder_jsp.java:184)
    ... 247 more

2 Replies

Avatar

Employee Advisor

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 shoul 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 stacktrace you get.

3) You should really consider the approach you've choosen. Why do you use an admin session to run a search (which finds all occurrences irrespective of permissions), when you're in a user context (who is maybe not allowed to see all of these occurrences)?

 

Jörg

Avatar

Level 10

Its best practice to close the session when you are done with it - as shown in your code. I will confirm if you need a patch for version 5.6.