Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

JCR RepositoryException - this session has been closed


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()) {



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:

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(
    at org.apache.jackrabbit.core.ItemManager.sanityCheck(
    at org.apache.jackrabbit.core.ItemManager.getItem(
    at org.apache.jackrabbit.core.ItemManager.getItem(
    at org.apache.jackrabbit.core.query.lucene.RowIteratorImpl$RowImpl.getNode(
    at org.apache.jackrabbit.core.query.lucene.RowIteratorImpl$RowImpl.getValue(
    at javax.servlet.http.HttpServlet.service(
    ... 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(
    at org.apache.jackrabbit.core.SessionImpl.logout(
    at org.apache.jackrabbit.core.XASessionImpl.logout(
    ... 247 more

0 Replies


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




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.