Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Querying Adobe Experience Manager Data using the JCR API [Error]

Avatar

Level 2

hi, im doing this tutorial about querys to JCR:

http://helpx.adobe.com/adobe-cq/using/querying-experience-manager-data-using.html

everything went smoothly, and i have everything up and running, i can add data (customers) and thats working, but when i try to see the list, im getting an error. Here is the stacktrace:

org.apache.jackrabbit.core.SessionImpl Unclosed session detected. The session was opened here:  java.lang.Exception: Stack Trace at org.apache.jackrabbit.core.SessionImpl.<init>(SessionImpl.java:222) at org.apache.jackrabbit.core.XASessionImpl.<init>(XASessionImpl.java:117) at com.day.crx.core.CRXSessionImpl.<init>(CRXSessionImpl.java:69) at com.day.crx.core.CRXRepositoryImpl.createSessionInstance(CRXRepositoryImpl.java:935) at org.apache.jackrabbit.core.RepositoryImpl.createSession(RepositoryImpl.java:959) at org.apache.jackrabbit.core.SessionFactory.createAdminSession(SessionFactory.java:42) at com.day.crx.sling.server.impl.SlingRepositoryWrapper.loginAdministrative(SlingRepositoryWrapper.java:76) at com.adobe.cq.CustomerServiceImp.getCustomerData(CustomerServiceImp.java:159) at org.apache.jsp.apps.jcrpersist.components.page.templateQuery.query_json_jsp._jspService(query_json_jsp.java:153) at org.apache.sling.scripting.jsp.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:502) at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:449) at org.apache.sling.scripting.jsp.JspScriptEngineFactory.callJsp(JspScriptEngineFactory.java:265) at org.apache.sling.scripting.jsp.JspScriptEngineFactory.access$100(JspScriptEngineFactory.java:87) at org.apache.sling.scripting.jsp.JspScriptEngineFactory$JspScriptEngine.eval(JspScriptEngineFactory.java:465) at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:361) at org.apache.sling.scripting.core.impl.DefaultSlingScript.eval(DefaultSlingScript.java:171) at org.apache.sling.scripting.core.impl.DefaultSlingScript.service(DefaultSlingScript.java:463) at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:508) at org.apache.sling.engine.impl.filter.SlingComponentFilterChain.render(SlingComponentFilterChain.java:45) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:64) at com.day.cq.wcm.core.impl.WCMDebugFilter.doFilterWithErrorHandling(WCMDebugFilter.java:182) at com.day.cq.wcm.core.impl.WCMDebugFilter.doFilter(WCMDebugFilter.java:149) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60) at com.day.cq.wcm.core.impl.WCMComponentFilter.filterRootInclude(WCMComponentFilter.java:356) at com.day.cq.wcm.core.impl.WCMComponentFilter.doFilter(WCMComponentFilter.java:168) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60) at com.day.cq.personalization.impl.TargetComponentFilter.doFilter(TargetComponentFilter.java:96) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60) at org.apache.sling.engine.impl.SlingRequestProcessorImpl.processComponent(SlingRequestProcessorImpl.java:254) at org.apache.sling.engine.impl.filter.RequestSlingFilterChain.render(RequestSlingFilterChain.java:49) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:64) at com.day.cq.wcm.core.impl.AuthoringUIModeServiceImpl.doFilter(AuthoringUIModeServiceImpl.java:301) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60) at com.day.cq.wcm.core.impl.warp.TimeWarpFilter.doFilter(TimeWarpFilter.java:106) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60) at com.day.cq.wcm.mobile.core.impl.redirect.RedirectFilter.doFilter(RedirectFilter.java:290) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60) at com.adobe.cq.commerce.hybris.impl.LoginRequestFilter.doFilter(LoginRequestFilter.java:56) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60) at org.apache.sling.engine.impl.debug.RequestProgressTrackerLogFilter.doFilter(RequestProgressTrackerLogFilter.java:64) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60) at com.day.cq.wcm.foundation.forms.impl.FormsHandlingServlet.doFilter(FormsHandlingServlet.java:221) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60) at com.day.cq.theme.impl.ThemeResolverFilter.doFilter(ThemeResolverFilter.java:76) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60) at com.adobe.granite.optout.impl.OptOutFilter.doFilter(OptOutFilter.java:74) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60) at com.day.cq.wcm.core.impl.WCMRequestFilter.doFilter(WCMRequestFilter.java:90) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60) at com.adobe.cq.history.impl.HistoryRequestFilter.doFilter(HistoryRequestFilter.java:107) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60) at com.day.cq.wcm.designimporter.CanvasPageDeleteRequestFilter.doFilter(CanvasPageDeleteRequestFilter.java:88) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60) at org.apache.sling.rewriter.impl.RewriterFilter.doFilter(RewriterFilter.java:83) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60) at org.apache.sling.i18n.impl.I18NFilter.doFilter(I18NFilter.java:127) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60) at org.apache.sling.bgservlets.impl.BackgroundServletStarterFilter.doFilter(BackgroundServletStarterFilter.java:135) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60) at org.apache.sling.engine.impl.SlingRequestProcessorImpl.processRequest(SlingRequestProcessorImpl.java:151) at org.apache.sling.engine.impl.SlingMainServlet.service(SlingMainServlet.java:206) at org.apache.felix.http.base.internal.handler.ServletHandler.doHandle(ServletHandler.java:96) at org.apache.felix.http.base.internal.handler.ServletHandler.handle(ServletHandler.java:79) at org.apache.felix.http.base.internal.dispatch.ServletPipeline.handle(ServletPipeline.java:42) at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:49) at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33) at org.apache.sling.i18n.impl.I18NFilter.doFilter(I18NFilter.java:127) at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88) at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76) at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47) at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33) at org.apache.felix.http.sslfilter.internal.SslFilter.doFilter(SslFilter.java:55) at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88) at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76) at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47) at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33) at org.apache.sling.security.impl.ReferrerFilter.doFilter(ReferrerFilter.java:263) at org.apache.felix.http.base..

 

i guess it has something to do with this code line (line 159 of CustomerService.java):

Session session = this.repository.loginAdministrative(null);

because it does say it has to do with the session, and it mentions the line in the stacktrace.

but i dont know what should be done, can someone tell me how to proceed? to make this work?

 

thanks in advance!!

1 Accepted Solution

Avatar

Correct answer by
Level 10

I have been thinking about this somewhat -- i think i know what happened -- you took code from another article that talks about just Persisting Data into the AEM JCR. The code you had in your Java for injestCustData was from that article and when you switched to this article - you did not update the injestCustData method (take the code from this article). I have updated the code in the Persisting Data in the AEM JCR so if somebody happens to merge code like this again from different articles - they will not run into the issue. 

View solution in original post

13 Replies

Avatar

Level 10

In your code -- be sure to close the Session (as shown in the article), Very important to ensure that no leaks occur. 

Also -here is some more background:

Here is more information on this issue of using the JCR API within an OSGi bundle:  http://cqdump.wordpress.com/2013/07/23/cq-development-patterns-sling-resourceresolver-and-jcr-sessio...

 

1st rule: Session leaks = memory leaks
Whenever you open a JCR session, there is a reference to the JCR repository object; this link prevents the session object from being garbage collected even if you drop all references to that session object. Every session will consume a little bit of memory unless the logout() method is called explicitly. If you omit this call and create lots of sessions, you risk an out-of-memory exception by your JVM, which terminates the CQ instance. A single leaked session isn’t a problem, but if you have hundreds or thousands of leaked sessions, it might turn into a problem. Gladly you will find lot’s of warnings in CQ’s error.log, so it isn’t that hard to spot them.

A simple pattern to avoid such a situation is to limit to scope of a JCR session to the scope of a java method or OSGI service/component. At the beginning create the session and the end terminate it.

 

So be sure to close the Session object. However -- that does not sound like the cause of your issue.  What version of CQ are you using? 

Avatar

Level 2

thank you for your answer, i am closing the session, with this:

// Save the session changes and log out session.save(); session.logout();

as shown in the tutorial... but ill attacth my java file too if you want to take a look at it

 

my version is: 5.6.1.20130606

Avatar

Level 10

This article was just tested and this was the result:

[img]Customer.png[/img]

Can you set a breakpoint in your browser and see if the AJAX call returns data or if there is an emtpy result.

I will go through your Java and help you discover the issue. 

Avatar

Level 2

the response is empty, here is an image

[img]filteredData.jpg[/img]

 

thank you for your help!!

Avatar

Level 10

I am going to dig through this and get to the bottom -- you ahve the Customer data in the node? Can you send me a pic of the CRXDDE Lite showing the customer. Does Add Customer work?

Avatar

Level 2

thank you!, the add does work, here is the img showing the data

[img]customerdata.jpg[/img]

 

[edit]

you know what, maybe the trace i showed in the original post its for something else, because now im not getting any errors in the error.log file... but still, the call to ajax returns an empty response and i dont get any customers in the list

[/edit]

Avatar

Level 10

HI

Can you please post your client files too - lets look at them. 

Thxs

Avatar

Level 2

i found the error! :)

here it is for future cases:

the add customer function, adds a customer with a sling:OrderedFolder type, but the querys in the code looks for a nt:unestructured type, so i changed the querys to look for sling:OrderedFolder, and it works now, heres is the before and after:

 

before:

if (filter.equals("All Customers"))     sqlStatement = "SELECT * FROM [nt:unstructured] WHERE CONTAINS(desc, 'Customer')"; else if(filter.equals("Active Customer"))     sqlStatement = "SELECT * FROM [nt:unstructured] WHERE CONTAINS(desc, 'Active')"; else if(filter.equals("Past Customer"))     sqlStatement = "SELECT * FROM [nt:unstructured] WHERE CONTAINS(desc, 'Past')";

 

after:

if (filter.equals("All Customers")) sqlStatement = "SELECT * FROM [sling:OrderedFolder] WHERE CONTAINS(desc, 'Customer')"; else if(filter.equals("Active Customer"))     sqlStatement = "SELECT * FROM [sling:OrderedFolder] WHERE CONTAINS(desc, 'Active')"; else if(filter.equals("Past Customer"))     sqlStatement = "SELECT * FROM [sling:OrderedFolder] WHERE CONTAINS(desc, 'Past')";

 

also, as i did the "persist data" tutorial first, in the first tutorial you use phone as one of the properties, and in the second you use address, so i changed the nodes directly and the code that i had from the first tutorial from phone to address

 

thank you for your help! you have been really helpful!

Avatar

Level 10

WHen you say "but when i try to see the list, im getting an error" - i am unsure what you mean. 

Are you talking about populating the grid in the sample? 

Avatar

Level 10

Awesome -- I am glad that you got it working. 

Avatar

Level 10

Actually - i am a bit confused -- in your Java code:

 

//Create a node that represents the root node Node root = session.getRootNode(); //Get the content node in the JCR Node content = root.getNode("content"); //Determine if the content/customer node exists Node customerRoot = null; int custRec = doesCustExist(content); //-1 means that content/customer does not exist if (custRec == -1) { //content/customer does not exist -- create it customerRoot = content.addNode("customer"); } else { //content/customer does exist -- retrieve it customerRoot = content.getNode("customer"); } int custId = custRec+1; //assign a new id to the customer node //Store content from the client JSP in the JCR Node custNode = customerRoot.addNode("customer"+firstName+lastName+custId);

 

 

But in the article -- we made the customer nodes nt:unstructured:
 

//-1 means that content/customer does not exist
 if (custRec == -1)
     //content/customer does not exist -- create it
    customerRoot = content.addNode("customer","sling:OrderedFolder");
 else
   //content/customer does exist -- retrieve it
   customerRoot = content.getNode("customer");
                                
   int custId = custRec+1; //assign a new id to the customer node
                     
   //Store content from the client JSP in the JCR
   Node custNode = customerRoot.addNode("customer"+firstName+lastName+custId,"nt:unstructured");

 

 

Wonder why "nt:unstructured" was not part of your Java code. For future reference - when adding a node to the AEM JCR - be sure to specify a type.

Avatar

Correct answer by
Level 10

I have been thinking about this somewhat -- i think i know what happened -- you took code from another article that talks about just Persisting Data into the AEM JCR. The code you had in your Java for injestCustData was from that article and when you switched to this article - you did not update the injestCustData method (take the code from this article). I have updated the code in the Persisting Data in the AEM JCR so if somebody happens to merge code like this again from different articles - they will not run into the issue. 

Avatar

Level 2

you are right! i did the other tutorial first and didnt update that method, ill change it to have the nt:unestructured structure as intended.

 

thank you!