Query related to jcrSession | Community
Skip to main content
Level 4
October 16, 2015
Solved

Query related to jcrSession

  • October 16, 2015
  • 5 replies
  • 1286 views

Hi,

Just want to understand the diffrence between the approach to get a JCR session like below:

Approach 1

ResourceResolver lResourceResolver = pResolverFactory.getAdministrativeResourceResolver(null);
Session lSession = lResourceResolver.adaptTo(Session.class);

Approach 2

Session lSession = repository.loginAdministrative(null);

Also, do we need to call session.logout() after I complete my work with session?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by smacdonald2008

See these community articles:

https://helpx.adobe.com/experience-manager/using/programmatically-accessing-cq-content-using.html

you use:

//Create a connection to the CQ repository running on local host 
Repository repository = JcrUtils.getRepository("http://localhost:4503/crx/server");
   
 //Create a Session
 javax.jcr.Session session = repository.login( new SimpleCredentials("admin", "admin".toCharArray()));

when you are using an external Java app. For example - a Java Swing app. 

If you are using the JCR API from within an OSGi bundle - you can use:

ResourceResolver lResourceResolver = pResolverFactory.getAdministrativeResourceResolver(null);
Session lSession = lResourceResolver.adaptTo(Session.class);

 This article shows you how to do this:

http://scottsdigitalcommunity.blogspot.ca/2013/02/querying-adobe-experience-manager-data.html

But some ppl do not like using getAdministrativeResourceResolver()

Instead - they recommend that you create a dedicated user account, specify permissions using ACL, and use sling mapping.

Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "datawrite");
ResourceResolver resolver = null;

try {
           
    //Invoke the adaptTo method to create a Session used to create a QueryManager
 resolver = resolverFactory.getServiceResourceResolver(param);
    session = resolver.adaptTo(Session.class);

 This article shows you how to do this:

http://scottsdigitalcommunity.blogspot.ca/2014/12/querying-adobe-experience-manager-6.html

When you close your session and you modified the JCR - use these methods:

session.save(); 
  session.logout();

5 replies

Feike_Visser1
Adobe Employee
Adobe Employee
October 16, 2015

Always try to avoid admin-sessions..., that is the first advice I would say.

And if you find a reason still to use it, then if you *create* the session, you need the close it. Same applies for the resourceResolver.

sumitc22Author
Level 4
October 16, 2015

Thanks for your reply. Can you please elaborate ?

Always try to avoid admin-sessions - which are admin sessions ? I just need to run some SQL2 statement ? What should be the preferred way?

What do you mean by *create* a session ? Please advise about session.logout() also

Adobe Employee
October 16, 2015

Firstly..when you create a session that belongs to the admin user(repo.loginAdministrative or pResolverFactory.getAdministrativeResourceResolver) 

Then it is admin sessions. This MUST NOT be done. Please follow the approach that scott has mentioned above if you need sessions. Secondly, slign apis provide all the necessary methods to create,modify and deleite nodes(resources). We should always prefer to use sling apis to jcr apis. 

smacdonald2008
smacdonald2008Accepted solution
Level 10
October 16, 2015

See these community articles:

https://helpx.adobe.com/experience-manager/using/programmatically-accessing-cq-content-using.html

you use:

//Create a connection to the CQ repository running on local host 
Repository repository = JcrUtils.getRepository("http://localhost:4503/crx/server");
   
 //Create a Session
 javax.jcr.Session session = repository.login( new SimpleCredentials("admin", "admin".toCharArray()));

when you are using an external Java app. For example - a Java Swing app. 

If you are using the JCR API from within an OSGi bundle - you can use:

ResourceResolver lResourceResolver = pResolverFactory.getAdministrativeResourceResolver(null);
Session lSession = lResourceResolver.adaptTo(Session.class);

 This article shows you how to do this:

http://scottsdigitalcommunity.blogspot.ca/2013/02/querying-adobe-experience-manager-data.html

But some ppl do not like using getAdministrativeResourceResolver()

Instead - they recommend that you create a dedicated user account, specify permissions using ACL, and use sling mapping.

Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "datawrite");
ResourceResolver resolver = null;

try {
           
    //Invoke the adaptTo method to create a Session used to create a QueryManager
 resolver = resolverFactory.getServiceResourceResolver(param);
    session = resolver.adaptTo(Session.class);

 This article shows you how to do this:

http://scottsdigitalcommunity.blogspot.ca/2014/12/querying-adobe-experience-manager-6.html

When you close your session and you modified the JCR - use these methods:

session.save(); 
  session.logout();

October 16, 2015

Does the same work in AEM 6.1? 

I'm seeing a Login Exception, with the following message:

LoginException. org.apache.sling.api.resource.LoginException: Cannot derive user name for bundle group.tti.commons-service [395] and sub service writeService