2 Replies Latest reply on Jan 2, 2009 11:57 PM by jaikiran pai

    JPA entity caching with JBossCache in JBoss-5.0 GA

    jaikiran pai Master

      I'm trying to move a EJB3 JPA example which has second level cache enabled to JBossAS-5.0 GA. I am following this wiki for instructions http://www.jboss.org/community/docs/DOC-13200. Based on those instructions i have been able to get the example deploying successfully in JBoss-5 GA. I have a couple of related questions:


      1) Is there a way to access this second level cache from within the application (ex: from a EJB3 bean). I see that the java:CacheManager is registered in the JNDI. So would the following be the correct thing to do:

      private Cache getCache() throws Exception
       {
       org.jboss.cache.CacheManager cacheManager = (CacheManager) new InitialContext().lookup("java:CacheManager");
       return cacheManager.getCache("mvcc-entity", false);
       }
      


      The org.jboss.cache.CacheManager.getCache() accepts 2 arguments. I could not find the javadocs and wasn't sure what to pass. Based on some explanation from the wiki, i decided to pass mvcc-entity as the first param. What exactly are these parameters used for?

      2) Once i get hold of the cache, is there some way through which i can figure out whether a particular entity is cached?

      public boolean isCustomerInCache(Long id){
      
       Cache cache = getCache();
       // is there some way through which i can check if the entity is cached?
      
      
      }




        • 1. Re: JPA entity caching with JBossCache in JBoss-5.0 GA
          Brian Stansberry Master

           

          "jaikiran" wrote:
          I'm trying to move a EJB3 JPA example which has second level cache enabled to JBossAS-5.0 GA. I am following this wiki for instructions http://www.jboss.org/community/docs/DOC-13200. Based on those instructions i have been able to get the example deploying successfully in JBoss-5 GA. I have a couple of related questions:


          1) Is there a way to access this second level cache from within the application (ex: from a EJB3 bean). I see that the java:CacheManager is registered in the JNDI. So would the following be the correct thing to do:
          private Cache getCache() throws Exception
           {
           org.jboss.cache.CacheManager cacheManager = (CacheManager) new InitialContext().lookup("java:CacheManager");
           return cacheManager.getCache("mvcc-entity", false);
           }
          


          The org.jboss.cache.CacheManager.getCache() accepts 2 arguments. I could not find the javadocs and wasn't sure what to pass. Based on some explanation from the wiki, i decided to pass mvcc-entity as the first param. What exactly are these parameters used for?


          Javadocs are at http://www.jboss.org/file-access/default/members/jbosscache/freezone/docs/3.0.1.GA/apidocs/index.html. The second param is whether the cache manager should create the "mvcc-entity" cache if not already created. Whether you pass true or false depends on whether you want a guaranteed non-null return.

          Note that any code that calls getCache() should also call releaseCache() when it is done with the cache. The CacheManager uses reference counting to know when to stop the cache.


          2) Once i get hold of the cache, is there some way through which i can figure out whether a particular entity is cached?

          public boolean isCustomerInCache(Long id){
          
           Cache cache = getCache();
           // is there some way through which i can check if the entity is cached?
          
          
          }




          IMO doing any of this is a bad idea; the cache is an internal structure of the Hibernate SessionFactory. For sure it's a bad idea to document such a thing in any of the EJB3 examples!! :-) If you want to poke around for your own knowledge, the eviction chapter in the Hibernate/JBC guide at http://www.jboss.org/servlet/JiveServlet/download/10386-69-6042/hibernate-jbosscache-guide-3.pdf includes discussion of how things are stored.

          Hibernate provides an API (Statistics) for inspecting the Second Level Cache. See http://www.hibernate.org/hib_docs/v3/reference/en-US/html/performance-sessioncache.html. Users should restrict themselves to that API. If an existing EJB3 example pokes around in the cache internals, I encourage you to convert it to use the Hibernate Statistics API.

          • 2. Re: JPA entity caching with JBossCache in JBoss-5.0 GA
            jaikiran pai Master

             

            "bstansberry@jboss.com" wrote:

            Javadocs are at http://www.jboss.org/file-access/default/members/jbosscache/freezone/docs/3.0.1.GA/apidocs/index.html. The second param is whether the cache manager should create the "mvcc-entity" cache if not already created. Whether you pass true or false depends on whether you want a guaranteed non-null return.

            Note that any code that calls getCache() should also call releaseCache() when it is done with the cache. The CacheManager uses reference counting to know when to stop the cache.


            Thanks!

            "bstansberry@jboss.com" wrote:

            IMO doing any of this is a bad idea; the cache is an internal structure of the Hibernate SessionFactory. For sure it's a bad idea to document such a thing in any of the EJB3 examples!! :-)
            ...
            Hibernate provides an API (Statistics) for inspecting the Second Level Cache. See http://www.hibernate.org/hib_docs/v3/reference/en-US/html/performance-sessioncache.html. Users should restrict themselves to that API. If an existing EJB3 example pokes around in the cache internals, I encourage you to convert it to use the Hibernate Statistics API.


            That's exactly what i was trying to refactor in the EJB3 tutorial :-) Thanks for the pointer to Hibernate Statistics API. I'll take a look.