3 Replies Latest reply on Jul 26, 2010 4:17 PM by Randall Hauch

    JCR-SQL2 query doesn't find node created in another application

    Greg Larson Newbie

      I'm running two web apps which access the same ModeShape (version 1.1.0.Final) repository via JpaConnector.  The applications use the same library for basic DAO functions -- one app is an 'admin' tool in which content changes are made; the other is a 'reader' app which reads and displays content.


      Since both apps use a container which references content from several JCR nodes, I'm using a JCR-SQL2 query to find the container's content.  However, the query from the reader app only finds nodes that existed at the time it was started -- if I run both apps simultaneously and create a new node in the admin tool, the query from the reader app will not find the new node until after it has been restarted.  The admin tool does not have this problem.


      Oddly, if I replace the JCR-SQL2 query with a simple loop using session.getNodeByUUID(uuid) to find each referenced node, the reader app has no problem finding newly-created nodes.


      Here is an example of the JCR-SQL2 query:


      SELECT * FROM [mix:referenceable] AS contentItem WHERE contentItem.[jcr:uuid]='x' OR contentItem.[jcr:uuid]='y' OR contentItem.[jcr:uuid]='z'


      I've also tried using path instead of UUID (with the same results):


      SELECT * FROM [mix:referenceable] AS contentItem WHERE PATH(contentItem)='/path/to/x' OR PATH(contentItem)='/path/to/y' OR PATH(contentItem)='/path/to/z'


      The following information may also be useful:


      • Debug hibernate SQL and cache logging for calls to the JCR-SQL2 query is identical in both apps when the problem occurs.
      • The problem is not affected by switching ModeShape databases -- I've tested using both MySQL and HSQLDB.
      • Setting JpaSource.cacheTimeToLiveInMilliseconds=0 or 1 and JpaSource.connectionIdleTimeInSeconds=0 or 1 does not affect the problem.
      • Calling session.refresh(true) after saves/updates or just prior to calling the JCR-SQL2 query does not resolve the problem.


      This is a critical issue for my development team -- many thanks for any assistance!

        • 1. Re: JCR-SQL2 query doesn't find node created in another application
          Randall Hauch Master

          Do your web applications each instantiate a ModeShape engine, or do they both look up and use the same JcrEngine instance? Based upon the behavior you are seeing, I suspect they are using different engines.


          When two (or more) engines are used, and a change is made to the persisted stored via one engine, the other engine does not receive a notification of that change, and it does not update its local indexes. (The content is persisted in JPA in your case, so that's why navigating via the JCR API does see the updated content.) A workaround is to periodically rebuild the indexes in the admin web application. (There is a "

          reindexContent()" method on JcrSession.)


          That's obviously a non-ideal solution. The real solution with ModeShape 1.x is to use the same engine instance. This likely means creating the JcrEngine and placing it in JNDI, where both web applications can access and use it. Because only one engine will be used, it will remain consistent with all changes, and your queries will work.


          If you're using ModeShape 2.0 and JCR 2.0, you could use the RepositoryFactory approach to look up the desired repository, and our RepositoryFactory implementation manages the JcrEngine instances automatically. For details, see http://docs.jboss.org/modeshape/latest/manuals/reference/html_single/reference-guide-en.html#obtainining_jcr_repository


          Our next release will introduce support for clustering multiple engines so that they all are aware of changes made in other engines in the cluster. We've already implemented this feature in the trunk of our SVN, and we plan to issue a ModeShape 2.1 candidate release within the next two weeks. Feel free to download the latest from SVN and build it yourself. (The Reference Guide and Getting Started documents have already been updated in trunk, too, and describe how to set up clustering. So doing "mvn clean install -Pintegration -DskipTests=true" will build everything, including the local docs. Note that if any of the clustering tests fail, you may be using IPv6 locally, so try "mvn clean install -Pintegration,preferIpv6 -DskipTests=true".)

          • 2. Re: JCR-SQL2 query doesn't find node created in another application
            Greg Larson Newbie

            Thanks for the quick response!  Each application uses its own ModeShape engine, so your analysis matches the problem exactly.  We will be eagerly awaiting ModeShape 2.1 since we definitely need the clustering support you mentioned.

            • 3. Re: JCR-SQL2 query doesn't find node created in another application
              Randall Hauch Master

              Good to hear. Stay tuned to our forum, watch us on Twitter, or subscribe to the mailing lists -- we send out news on all of these fronts.


              And if you are feeling adventurous, we'd love feedback on the feature before we release! I can walk you through building it locally if you need help. If you don't have time or desire, that's fine, too.