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