2 Replies Latest reply on Jul 13, 2011 8:42 AM by Van Halbert

    Modeshape connexion design pattern

    sjahan Newbie



      We're running into some design problem and i wondered if by any chance, someone might have an answer.


      We're using Modeshape to manage our data behind a portal, so we have to deal with several simultaneous connexion to Modeshape and we're wonder if there is some common pattern in this case.


      At the beginning, nearly each access to Modeshape was done by creating a JCRConfiguration and starting a JCREngine from it. This was really the early stage.

      By time, number of accesses increased, so we made the JCREngine a singleton so that the start() method (that takes quite some time if the tree is large) is only called once at the start of the server. But now, we're facing some issues because of the repository login()/logout() method at the beginning/end of our access methods. Actually, if two calls are performed at the same time, two login() are fired but at the end of the first call, logout() is fired and it cut the session which is still used by the second call.

      This is quite troublesome obviously, and i guess this could be not entirely  Modeshape specific, but do you advise some pattern to apply to use properly Modeshape in that case?


      Like are we supposed to use one JCREngine by user? How are we supposed to login/logout (this could be a more general question though)?

      If you have any advice, it could be a great help to get us through that matter!


      Thank you very much by advance,



        • 1. Re: Modeshape connexion design pattern
          Brian Carothers Apprentice

          As you've correctly identified, you want no more than one JcrEngine instance per-repository-and-JVM.  You're also seeing that your sessions need to be per-request, not per-method or per-instance of your business objects.


          My first cut would be to write an OpenSessionInView (OSIV) implementation for ModeShape.  It's pretty straightforward to do so.  You just write a servlet filter that opens up the session before chaining, sticks it in thread-local storage, and then closes the session after chaining is complete.  There may already be one of these out there.  Then, your access methods would pull the existing session off of the thread-local storage instead of creating and destroying sessions with each method.  This also assumes that your methods that access content are not in a singleton, even though your JcrEngine itself may be.


          The second thing that I would consider doing is implementing a connection pool for ModeShape connections.  Apache commons has an ObjectPool class that makes this pretty easy to do.  Just don't forget to call Session.refresh(false) before returning sessions to the pool.  OTOH, Session.login and .logout are pretty inexpensive methods.  You might want to hold off on pooling sessions until there's some evidence that you need to do so.

          • 2. Re: Modeshape connexion design pattern
            Van Halbert Master

            If they are deploying to JBoss AS, modeshape already has a JcrEngine instance running.    Would it be a better option to just use the RepositoryFactory (as described here: http://docs.jboss.org/modeshape/2.5.0.Final/manuals/gettingstarted/html_single/getting-started-en.html#repository_factory) to find the repository and create a session?