I agree with Paul, I would say that the rule of thumb is to stay open for just 
the length of time needed to access the resource via the resource resolver. 

A couple of notes, based on your sample

1. The use of getAdministrativeResourceResolver is considered harmful. Ideally 
you should have an account that has been configured with just the permissions 
for the service to do what it needs to do. In the latest versions of Sling, 
there are service accounts that can be configured just for that purpose.

2. Review how the service is being accessed. I see a common use case where 
services are being accessed as part of a HTTP request, when an http request is 
made there is already a resource resolver created and associated with that 
request that you can then pass in to the service, like Paul mentioned.

3. When you create a resource resolver you are creating an associated session. 
That session maintains a persistent view of the underlying JCR. Think MVCC in 
DB terms. If you must have a resource resolver tied to an account that is open 
for the entire time period. Be sure to use the .refresh() method on the 
resolver before using. Otherwise the result you are returning could be 
incorrect.

-Jason

-----Original Message-----
From: Paul McMahon [mailto:oro...@yahoo.com.INVALID] 
Sent: Tuesday, June 14, 2016 11:31 AM
To: users@sling.apache.org
Subject: Re: Using the resource resolver

I generally follow the practice of having the resource resolver passed in if 
possible. If the whatever is calling the service is triggered by some sort of 
HTTP request it's always best to just have the resource resolver passed in - in 
which case the resource resolver is only around for the life the request and 
gets disposed of automatically by the system. If I do have to leverage a 
resource resolver that you get from the factory I tend to try and dispose of 
them in the same method I created it.
Paul McMahon
      From: Roy Teeuwen <r...@teeuwen.be>
 To: users@sling.apache.org
 Sent: Tuesday, June 14, 2016 11:19 AM
 Subject: Using the resource resolver
   
Hello all,

I am wondering on the usage of the resource resolver and on how long it should 
stay open. Lets say I have the following implementation:

@Component
@Service(SomeService.class)
public class SomeServiceImpl implements SomeService {

@Reference
 private ResourceResolverFactory resourceResolverFactory;
    
private ResourceResolver resourceResolver;

    @Activate
    protected synchronized void activate(Map<String, Object> properties) throws 
Exception {
      try {
            resourceResolver = 
resourceResolverFactory.getAdministrativeResourceResolver(null);
        } catch (LoginException e) {
            LOG.error("Failed to get admin resourceresolver", e);
            throw e;
        }
    }

 @Deactivate
    protected synchronized void deactivate(Map<String, Object> properties) 
throws Exception {
        if (resourceResolver != null && resourceResolver.isLive()) {
            resourceResolver.close();
        }
    }

  public void doSomething(String path) {
    Resource resource = resourceResolver.getResource(path);
      //do something
}
}


Is there anything wrong on using this? Knowing that this means the 
resourceresolver will stay open for possible months/years… Or should one try 
and make the resourceresolver opening as short as possible? Knowing that the 
method will be called around 1000-2000 times an hour. 

Another solution is of course passing the resourceResolver to do the 
doSomething, but then the question still remains, how long should you keep a 
resourceresolver open 

Greetings,
Roy

  

Reply via email to