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... ?
Views
Replies
Total Likes
Hi @nbg19a ,
Below are the responses to your queries:
Regards,
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
Views
Replies
Total Likes
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).
@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...
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.
You can refer to this for the best practices - https://experienceleague.adobe.com/en/docs/experience-manager-learn/foundation/development/understan...
@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!
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies