Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Unclosed session detected errors in log

Avatar

Level 5

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.

1 Accepted Solution

Avatar

Correct answer by
Employee

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

View solution in original post

11 Replies

Avatar

Level 5

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?

Avatar

Level 5

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

Avatar

Level 10

We should not be using loginAdministrative()

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

make sure you close resourceResolver in the final block

Avatar

Level 5

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

Avatar

Correct answer by
Employee

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

Avatar

Level 10

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

Avatar

Level 5

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

Avatar

Level 10

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.