Skip to main content
Level 4
Feb 9, 2016 6:24 PM UTC
Solved

Unclosed session detected errors in log

We are seeing this error in the logs. I've seen that the vendor who wrote code for us is using something like this

 ResourceResolver resourceResolver = request.getResourceResolver();
        QueryBuilder queryBuilder = resourceResolver.adaptTo(QueryBuilder.class);
        Session session = resourceResolver.adaptTo(Session.class);

 

Does this session need to be closed in finally as well os it's only when we try to get administrative session that we need to correctly close it?

Any links for best practices while writing servlets and using sessions would be appreciated.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Feike_Visser1
Feike_Visser1Accepted solution
Adobe Employee
Feb 9, 2016 8:22 PM UTC

No, rule is: You create it, you close it. Does not apply for adaptTo()

11 replies

Lokesh_Shivalingaiah
Level 10
Feb 9, 2016 7:19 PM UTC

We need to close the ResourceResolver or the Session which we initiate. Refer [1], [2] in to know more in detail

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

[2] http://www.wemblog.com/2014/08/how-to-use-sessions-and-resource.html

Translate
Level 4
Feb 9, 2016 7:59 PM UTC

Does this apply only while getting an admin session using resolverfactory or even a regular session using resourceResolver.adaptTo(Session.class) using the request object of the servlet?

Translate
Level 4
Feb 9, 2016 8:00 PM UTC

Or it makes sense to use every component like this

@Activate
  protected void activate() {
    session = null;
    try {
      session = repo.loginAdministrative();
      …
    } catch (RepositoryException e) {
      // log the exception
    }
  }

  @Deactivate
  protected void deactivate() {
    if (session != null && session.isLive()) {
      session.logout();
    }
  }

Translate
Lokesh_Shivalingaiah
Level 10
Feb 9, 2016 8:04 PM UTC

We should not be using loginAdministrative()

when you are using resourceResolver.adaptTo(Session.class)

make sure you close resourceResolver in the final block

Translate
Level 4
Feb 9, 2016 8:10 PM UTC

No I meant either ways, not that we are opening sessions using both methods. If I just use session using AdaptTo, does that also need to be closed using session.close()

Translate
Feike_Visser1
Adobe Employee
Feike_Visser1Adobe EmployeeAccepted solution
Adobe Employee
Feb 9, 2016 8:22 PM UTC

No, rule is: You create it, you close it. Does not apply for adaptTo()

Translate
Lokesh_Shivalingaiah
Level 10
Feb 9, 2016 8:25 PM UTC

Yes.. You will have to close the session by session.logout()

Translate
Level 4
Feb 9, 2016 8:35 PM UTC

hey per ~~Feike Visser  if I'm using adaptTo I don't need to close it

Translate
Lokesh_Shivalingaiah
Level 10
Feb 10, 2016 2:46 AM UTC

Either you need to do Session.logout() just to close the session and not the resource, If you are doing ResourceResolver.close() then you dont have to explicitly do session logout. 

Translate