Expand my Community achievements bar.

SOLVED

Query related to jcrSession

Avatar

Level 4

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?

1 Accepted Solution

Avatar

Correct answer by
Level 10

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();

View solution in original post

5 Replies

Avatar

Employee

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.

Avatar

Level 4

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

Avatar

Employee

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. 

Avatar

Correct answer by
Level 10

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();

Avatar

Former Community Member

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