Do I need to logout a session that's adapted from a created resolver? | Community
Skip to main content
Level 2
May 4, 2018

Do I need to logout a session that's adapted from a created resolver?

  • May 4, 2018
  • 2 replies
  • 4518 views

My resolver is created through ResourceResolverFactory:

resolver = getResolverFactory().getServiceResourceResolver(serviceParams); 

I already have a finally block in my method which closes the resolver.

My question is regarding the session I'm using:

Session session = resolver.adaptTo(Session.class); 

Do I need to logout this session or not?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

2 replies

Techaspect_Solu
Level 7
May 4, 2018

Hi,

Yes, you need to release all the resources associated with the Session.

When you open a JCR session, there is a reference to the JCR repository object. Every session will consume some memory unless the logout() method is called explicitly. If you do not call this call and create lots of sessions, you risk an out-of-memory exception by your JVM, which terminates the AEM instance. A single leaked session isn’t a problem, but if you have hundreds or thousands of leaked sessions, it might turn into a problem.

NOTE: session.logout() releases all the resources associated with the Session. This method should be called when a Session is no longer needed.

Session (Content Repository for Java Technology API Version 2.0)

Please refer the below link for an example:

Adobe Experience Manager Help | Querying Adobe Experience Manager 6 data using the Sling getServiceResourceResolver meth…

We hope this information helps!

Regards,

TechAspect Solutions

Feike_Visser1
Adobe Employee
Adobe Employee
May 4, 2018

Use AutoCloseable, then you don't have to worry about this.

htl-examples/AutoCloseableService.java at master · heervisscher/htl-examples · GitHub

joerghoh
Adobe Employee
Adobe Employee
May 4, 2018

Hi,

You have to close a session or a resourceresolver only if you opened the session or resourceResolver. In the typical pattern you use as well you login to a resource resolver and then adaptTo() a session.

adaptTo(Session.class) does not open a new session, but just gives you the JCR session hidden inside the resourceresolver. Therefor you don't need to close it. Just don't forget to close the resourceResolver.

Jörg