0

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.

Alex
  • 139
  • 2
  • 8

1 Answers1

3

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.

  • 1
    by the way where I can find some inforamtion about "AEM is collecting unclosed sessions after some time."? In this [article](http://tech.ethomasjoseph.com/2015/09/sling-who-is-closing-my-jcr-session.html) it talks that it closes an underlying JCR Session of the ResourceResolver when the ResourceResolver object is Garbage Collected. Do you mean that AEM has real job which with some period closees unclosed sessions or mentioned in the article behavior? – Alex Feb 08 '17 at 10:33
  • 1
    Do you have any references for "AEM is collecting unclosed sessions after some time." ? I am not aware of any such functionality in the product. – Robert Munteanu Feb 22 '17 at 10:07