Are there any drawbacks getting Session from ResourceResolver | Community
Skip to main content
February 4, 2025
Solved

Are there any drawbacks getting Session from ResourceResolver

  • February 4, 2025
  • 6 replies
  • 1003 views

Are there any gotchas getting session from ResourceResolver

 

Util code :

public static ResourceResolver getRR(org.apache.sling.api.resource.ResourceResolverFactory factory) { ResourceResolver resolver = null; Map<String, Object> param = new HashMap<>(); param.put(ResourceResolverFactory.SUBSERVICE, "subservicename"); try { resolver = factory.getServiceResourceResolver(param); } catch (LoginException e) { log.error("Login Exception :{}", e.getMessage()); } return resolver; }

 

 

 

service code :

@3214626 private org.apache.sling.api.resource.ResourceResolverFactory rrFactory; public void someMethod(){ try (ResourceResolver resolver = MyUtil.getRR(rrFactory)){ Session session = resolver.adaptTo(Session.class); etc... session.save(); }

 

is this session thread safe, do I have to watch for concurrency, etc... ?

 

 

 

 

 

Best answer by Harwinder-singh

@nbg19a Thread safety is primarily the main concern with getting the session by adapting the resolver.

 

A very good read on a very related topic by @joerghoh can be found here - https://cqdump.joerghoh.de/2013/07/23/cq-development-patterns-sling-resourceresolver-and-jcr-sessions/

 

6 replies

Shiv_Prakash_Patel
Community Advisor
Community Advisor
February 4, 2025

Hi @nbg19a ,

Below are the responses to your queries:

  • The session obtained from resolver.adaptTo(Session.class) is not thread-safe.
  • You do not need explicit concurrency control since a new session is created per call, but you should be aware of JCR node conflicts when calling session.save().
  • Your use of try-with-resources is correct, ensuring the Session is properly closed.
  • Performance concerns may arise if someMethod() is called too frequently, creating too many sessions.

Regards,

Shiv Prakash
nbg19aAuthor
February 6, 2025

Another question came to my mind.

 

Why I would use session.save vs resolver.commit

Obviously it depends on what api I use jcr vs sling but is there under the hood differences when saving changes ? Is there any doc. explaining the process during saving changes to JCR ?

 

I found this 

https://developer.adobe.com/experience-manager/reference-materials/spec/jcr/2.0/10_Writing.html

 

joerghoh
Adobe Employee
Adobe Employee
February 7, 2025

There is not much difference between those, it's just that on each level of abstraction you have a way to save stuff (and you don't need to switch the abstraction layer to do it).

Harwinder-singh
Community Advisor
Harwinder-singhCommunity AdvisorAccepted solution
Community Advisor
February 4, 2025

@nbg19a Thread safety is primarily the main concern with getting the session by adapting the resolver.

 

A very good read on a very related topic by @joerghoh can be found here - https://cqdump.joerghoh.de/2013/07/23/cq-development-patterns-sling-resourceresolver-and-jcr-sessions/

 

SreenivasBr
February 5, 2025

Use sling APIs as much as possible and use JCR APIs if absolutely necessary.

Close the resolver especially when using service resolver.

Conflicts can occur when more than 1 process is changing the same node property at the same time. This needs to be checked.

chaudharynick
February 5, 2025

Hi @nbg19a 

you need to use try-with-resources statement to get the resourceresolver so that it closes the session properly when it closes the resourceresolver.

SreenivasBr
February 6, 2025
kautuk_sahni
Community Manager
Community Manager
February 10, 2025

@nbg19a Did you find the suggestions helpful? Please let us know if you need more information. If a response worked, kindly mark it as correct for posterity; alternatively, if you found a solution yourself, we’d appreciate it if you could share it with the community. Thank you!

Kautuk Sahni