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
Coach
Employee
Jörg_Hoh
Employee

Likes

1,089 likes

Total Posts

3,123 posts

Correct Reply

1,063 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,089 likes

Total Posts

3,123 posts

Correct Reply

1,063 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile
Jörg_Hoh
Employee

20-02-2021

There are a few things to note here:

* An OSGI component is a singleton, thus your Resource Resolver is used on every invocation. And as Resource Resolvers are not thread-safe, you are likely to run into problems. To fix that you can either require that on every invocation of your service's method a ResourceResolver is provided, which is then used. The other way to solve is to create a dedicated ResourceResolver for every of these invocations. The same applies for JCR Sessions.

* Why do you use the JCR API? In 99% of all cases you can achieve the same with the Sling Resource API.

 

To answer your question: if you use resourceResolver.adaptTo(Session.class), you don't open the session. I have answered that question more deeply here: https://cqdump.wordpress.com/2018/11/12/resourceresolvers-and-sessions-you-open-it-you-close-it/

 

HTH,

Jörg