Unclosed session detected errors in log | Community
Skip to main content
Level 4
February 9, 2016
Solved

Unclosed session detected errors in log

  • February 9, 2016
  • 11 replies
  • 2060 views

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.
Best answer by Feike_Visser1

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

11 replies

Lokesh_Shivalingaiah
Level 10
February 9, 2016

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

Level 4
February 9, 2016

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?

Level 4
February 9, 2016

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();
    }
  }

Lokesh_Shivalingaiah
Level 10
February 9, 2016

We should not be using loginAdministrative()

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

make sure you close resourceResolver in the final block

Level 4
February 9, 2016

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

Feike_Visser1
Adobe Employee
Feike_Visser1Adobe EmployeeAccepted solution
Adobe Employee
February 9, 2016

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

Lokesh_Shivalingaiah
Level 10
February 9, 2016

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

Level 4
February 9, 2016

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

Lokesh_Shivalingaiah
Level 10
February 10, 2016

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.