Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

Session Handling

Avatar

Avatar
Validate 25
Level 3
Jai1122
Level 3

Likes

21 likes

Total Posts

127 posts

Correct Reply

2 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Boost 5
Boost 3
View profile

Avatar
Validate 25
Level 3
Jai1122
Level 3

Likes

21 likes

Total Posts

127 posts

Correct Reply

2 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Boost 5
Boost 3
View profile
Jai1122
Level 3

19-02-2021

Hi Experts,

  I am writing a Sling Component which a Servlet can use to read and write properties from and to the repository.

Since there are many custom properties and each property updation will be mapped to a different method, is it advisable to initiate the resource resolver in the activate method of the component and then use it to adapt the session object in each method. Below code is a sample.

 

@component
public class RepoUtil{

private ResourceResolver resourceResolver;
private javax.jcr.Session session;

@Activate
@Modified
public void activate(final ComponentContext componentContext) {
    //initialize the configuration props
    resourceResolver = getResourceResolver();
}
public String getPropertyAFromNodeA(){
   session = resourceResolver.adaptTo(Session.class);
   //use session to get the node needed.
   // use node api to get the intended property
   return propertyvalue;
}
public String getPropertyBFromNodeA(){
   session = resourceResolver.adaptTo(Session.class);
   //use session to get the node needed.
   // use node api to get the intended property
   return propertyvalue;
}
public String updatePropertyAFromNodeA(){
   session = resourceResolver.adaptTo(Session.class);
   //use session to get the node needed.
   // use node api to update the intended property
  return propertyvalue;
}
}

 

Note that session is not closed at the end of each method. Since I am not creating the session object instead merely adapting it from resource resolver, I am not sure if the session object has to be closed.  Is there a pattern we can use in scenarios like this where multiple reads/writes are expected?

 

Regards,

Pal

View Entire Topic

Avatar

Avatar
Ignite 1
Level 4
davidjgonzalezzzz
Level 4

Likes

54 likes

Total Posts

60 posts

Correct Reply

20 solutions
Top badges earned
Ignite 1
Give Back 5
Give Back 3
Give Back 10
Give Back
View profile

Avatar
Ignite 1
Level 4
davidjgonzalezzzz
Level 4

Likes

54 likes

Total Posts

60 posts

Correct Reply

20 solutions
Top badges earned
Ignite 1
Give Back 5
Give Back 3
Give Back 10
Give Back
View profile
davidjgonzalezzzz
Level 4

19-02-2021

100% don't initialize a resource resolver in your activate method. Keep your ResourceResolvers/JCR Sessions short-lived as you can. Also, rememeber resourceResolvers and JCR Sessions are NOT thread-safe. So if you had a servlet being hit by 2 ppl at the same time, and they both invoke the RepoUtil service using the same resourceResolver, bad things will happen (like end up w/ lost data due to unresolvable state conflicts)

 

Here's what you should do (in order of generic preference),

 

1. Pass in the slingRequest's resourceResolver to your OSGi component's methods to read/write the properties. This way you are using the actual user's security context (permissions) to write the data. This is the preferred approach.

2. If for some reason you cannot allow for the user of the sling requests's resource resolver to write the data, obtain a Service User resourceResolver with the proper permissions in your Servlet, and pass it to your methods.  

3. You could alternatively obtain the Service User resource resolver in your RepoUtil, and get a new resourceResolver in each method, but obviously thats a bit wasteful (if the servlet calls 3 methods, then you'd be obtaining the same resourceResolver 3x)

 

Also, try to avoid using JCR APIs when you can use Sling APIs, I'm specifically saying here you should use SLing Resources and ValueMaps (and ModifiableValueMaps) over JCR Nodes and Properties. If all your RepoUtil does is a "dumb" read/write of JCR properties, it seems unnucessary. If you had a domain-specific OSGi Service, such as "ContactUsService" and you pass in a bunch of data to something like contantUsService.submit(email, name, message) .. and that service puts those 3 params in the "right place" in the JCR, that makes more sense. Just having a OSGI component that generically reads/writes properties doesn't strike me as useful and redundant of Sling's APIs.

 

https://experienceleague.adobe.com/docs/experience-manager-learn/foundation/development/understand-j...