Search code examples
osgijcrslingjackrabbitjackrabbit-oak

Sling / Jackrabbit - resolver / session lifetime and its concurrent consistency


I have a osgi component which works with JCR (for example, CRUD).

@Component
@Service
public class SomeServiceImpl implements SomeService {

    @Reference
    private ResourceResolverFactory resourceResolverFactory;

    private ResourceResolver resourceResolver;

    @Activate
    private void init() {
       resourceResolver = resourceResolverFactory.getServiceResourceResolver(
              Collections.singletonMap(ResourceResolverFactory.SUBSERVICE, "myService"));
    }

    @Override
    public void serve() {
        //does something with resourceResolver
    }

    @Deactivate
    private void dispose() {
        resourceResolver.close();
    }
}

It creates new instance of resourceResolver and keeps it as long as this service is alive. From time to time this service is invoked outside.

My questions are:

  1. Is it correct approach where I once create resourceResolver and reuse it? Is it constantly?
  2. Do I have guarantees that underlying session will not be expired?
  3. By the way How long resourceResolver and their session lives and where can I see it?
  4. What about concurrency? Imagine this service is invoked from serveral places parallely, Does Jackrabbit guarantee me consistency?

@Component
@Service
public class SomeServiceImpl implements SomeService {

    @Reference
    private SlingRepository slingRepository;

    private Session session;

    @Activate
    private void init() {
       session = slingRepository.login();
    }

    @Override
    public void serve() {
        //does something with session
    }

    @Deactivate
    private void dispose() {
        session.logout();
    }
}

The same questions for another service (with session implementation).

It will be nice to see some proofs if it's possible. Maybe docs...

Thanks.


Solution

  • Is it correct approach where I once create resourceResolver and reuse it? Is it constantly?

    No, it is not. It is perfect example of bad practice. Creation of resourceResolver is lightweight you can create as many as you need. Note: you have to always close resourceResolver after usage but be careful and don't close it to early.

    Do I have guarantees that underlying session will not be expired?

    No you don't. AEM is collecting unclosed sessions after some time.

    By the way How long resourceResolver and their session lives and where can I see it?

    This session will became invalid after first concurrent write to the same resource. IRL big amount of changes even without conflict can fail on save.

    What about concurrency? Imagine this service is invoked from serveral places parallely, Does Jackrabbit guarantee me consistency?

    JCR session is supporting concurrency in scope of one session. Main assumption that will always create new session per update request.

    The same questions for another service (with session implementation).

    ResourceResolver is working over the Session it is just higher level of API.