Expand my Community achievements bar.

Nomination window for the Adobe Community Advisor Program, Class of 2025, is now open!

Are there any drawbacks getting Session from ResourceResolver

Avatar

Level 2

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 :

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

 

 

 

 

 

8 Replies

Avatar

Community Advisor

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

Avatar

Level 2

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

 

Avatar

Employee Advisor

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

Avatar

Community Advisor

@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-session...

 

Avatar

Level 5

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.

Avatar

Level 4

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.

Avatar

Level 5

Avatar

Administrator

@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