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

Avatar

Avatar

carlos71224838

Avatar

carlos71224838

carlos71224838

04-05-2018

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?

Replies

Avatar

Avatar

Techaspect_Solu

Avatar

Techaspect_Solu

Techaspect_Solu

04-05-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 getServiceR...

We hope this information helps!

Regards,

TechAspect Solutions

Avatar

Avatar

Feike_Visser1

Employee

Avatar

Feike_Visser1

Employee

Feike_Visser1
Employee

04-05-2018

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

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

Avatar

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

991

Correct Reply

1.0K

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

991

Correct Reply

1.0K
Jörg_Hoh
Employee

04-05-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