Skip to main content
Level 2
Feb 4, 2025 4:00 PM UTC
Solved

Are there any drawbacks getting Session from ResourceResolver

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... ?

 

 

 

 

 

Harwinder-singh
Harwinder-singhAccepted solution
Community Advisor
Feb 4, 2025 7:40 PM UTC

@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/

 

8 replies

Shiv_Prakash_Patel
Community Advisor
Community Advisor
Feb 4, 2025 4:09 PM UTC

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,

Translate
Shiv Prakash
nbg19aAuthor
Level 2
Feb 6, 2025 10:42 AM UTC

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

 

Translate
joerghoh
Adobe Employee
Adobe Employee
Feb 7, 2025 7:30 PM UTC

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

Translate
Harwinder-singh
Community Advisor
Harwinder-singhCommunity AdvisorAccepted solution
Community Advisor
Feb 4, 2025 7:40 PM UTC

@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/

 

Translate
SreenivasBr
Level 4
Feb 5, 2025 9:19 AM UTC

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.

Translate
chaudharynick
Adobe Champion
Adobe Champion
Feb 5, 2025 8:15 PM UTC

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.

Translate
kautuk_sahni
Level 9
Feb 10, 2025 10:34 AM UTC

@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!

Translate
Kautuk Sahni