Unclosed session detected errors in log | Community
Skip to main content
New Participant
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

smacdonald2008
New Participant
February 29, 2016

Here is a good article on this: 

http://scottsdigitalcommunity.blogspot.ca/2014/12/querying-adobe-experience-manager-6.html

Yes - close session by invoking session.logout();

Lokesh_Shivalingaiah
New Participant
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. 

New Participant
February 9, 2016

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

Lokesh_Shivalingaiah
New Participant
February 9, 2016

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

Feike_Visser1
Feike_Visser1Accepted solution
Employee
February 9, 2016

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

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

Lokesh_Shivalingaiah
New Participant
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

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

New Participant
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?