3 Replies Latest reply: May 26, 2011 2:46 AM by jaikiran pai RSS

    Configuring JBoss cache for EJB3 entities (JBoss-5.0.0.GA)

    piotrekde Newbie

      Hello,

       

      My applications very often queries database for rarely-changed data. So I've decided to optimize it using cached EJB entities as desribed in:

      http://docs.jboss.org/ejb3/docs/tutorial/1.0.7/html/Caching_EJB3_Entities.html

       

      I've done everything in the same way, but additionaly I had to copy following JARs to common/lib (otherwise NoClassDefFound exception occurred):

      hibernate-jbosscache2-3.3.3.GA.jar

      jgroups-2.0.0.GA.jar

      jbosscache-core-3.2.2.GA.jar

       

      Now, when I try to start JBoss I can see the following stacktrace on the console:

       

      (...)

      Caused by: org.hibernate.cache.CacheException: Unable to retreive Cache from JNDI [java:CacheManager]

                at org.hibernate.cache.jbc2.builder.JndiMultiplexingCacheInstanceManager.locateCacheFactory(JndiMultiplexingCacheInstanceManager.java:88)

                at org.hibernate.cache.jbc2.builder.JndiMultiplexingCacheInstanceManager.start(JndiMultiplexingCacheInstanceManager.java:72)

                at org.hibernate.cache.jbc2.JBossCacheRegionFactory.start(JBossCacheRegionFactory.java:106)

                at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:215)

                at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1327)

                at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)

                at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)

                ... 51 more

       

       

      I have to believe it, probably there is no CacheManager in the JNDI tree , but I still don't know what to do. There is nothing about additional JNDI configuration in the document I've pasted (and in the other articles I browsed).

       

      Thanks in advance, any help is appreciated.

      Piotr

       

      (unfortunately I cannot upgrade my JBoss version)

        • 1. Re: Configuring JBoss cache for EJB3 entities (JBoss-5.0.0.GA)
          piotrekde Newbie

          It was my fault...  I tried to use cache from JBoss server configuration without cache available (default). In order to do not run whole 'all' server configuration, I've copied following fies/directories from 'all' to 'default':

          deploy/cluster

          lib/jgroups.jar

          lib/hibernate-jbosscache2.jar

          lib/jbosscache-core.jar

          lib/jbosscache-pojo.jar

          deploy-hasinleton

           

          Now server starts without errors, however when I take a look at hibernate sql logs I can still see the same number of queries hitting database as without cache configured. Here is my persistence.xml file:

           

           

          <persistence xmlns="http://java.sun.com/xml/ns/persistence"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
                    version="1.0">
          
          
                    <persistence-unit name="mydb">
                              <jta-data-source>java:/myds</jta-data-source>
          
                                  (...) classes definitions
                              <class>com.my.class.MyEntityOne</class>
                                  (...)
          
                              <properties>
                                        <property name="hibernate.show_sql" value="true" />
                                        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
                                        <property name="hibernate.format_sql" value="false" />
                                        <property name="hibernate.hbm2ddl.auto" value="update"/>
                                        <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
                                        <property name="hibernate.transaction.factory_class" value="org.hibernate.ejb.transaction.JoinableCMTTransactionFactory"/>
          
          
                                          <!-- 2nd level cache -->                                       
                                          <property name="hibernate.cache.use_second_level_cache" value="true"/>
                                          <property name="hibernate.cache.use_query_cache" value="true"/>
                                          <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.jbc2.JndiMultiplexedJBossCacheRegionFactory"/>
                                          <property name="hibernate.cache.region.jbc2.cachefactory" value="java:CacheManager"/>
                                          <property name="hibernate.cache.region.jbc2.cfg.entity" value="mvcc-entity"/>
                                          <property name="hibernate.cache.region.jbc2.cfg.query" value="local-query"/>
                              </properties>
                    </persistence-unit>
          </persistence>
          
          

           

           

          Also, all my entities are annotated with:

          @Cache (usage=CacheConcurrencyStrategy.TRANSACTIONAL)

           

           

           

          Should I do some additional setup?

          Maybe a little hint will be that when I shut down server the following exception can be seen in logs:

           

           

          org.hibernate.cache.CacheException: java.lang.IllegalStateException: Cache not in STARTED state!

                    at org.hibernate.cache.jbc2.BasicRegionAdapter.destroy(BasicRegionAdapter.java:243)

                    at org.hibernate.impl.SessionFactoryImpl.close(SessionFactoryImpl.java:813)

                    at org.hibernate.ejb.EntityManagerFactoryImpl.close(EntityManagerFactoryImpl.java:46)

                    at org.jboss.jpa.deployment.ManagedEntityManagerFactory.destroy(ManagedEntityManagerFactory.java:93)

                    at org.jboss.jpa.deployment.PersistenceUnitDeployment.stop(PersistenceUnitDeployment.java:343)

                    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

                    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

                    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

                    at java.lang.reflect.Method.invoke(Method.java:597)

           

           

           

          Thanks,

          Piotr

          • 2. Re: Configuring JBoss cache for EJB3 entities (JBoss-5.0.0.GA)
            piotrekde Newbie

            It seems that query caching and entity caching are quite distinct.

             

            Queries has to explicitly state that they want to be cached:

             

            Query query = manager.createQuery("SELECT i FROM ..");
            query.setHint("org.hibernate.cacheable", true);
            

             

            For entities, in my case (I don't know if it's a general rule) it was not enough to annotate classes with @Cache at the top, but also include this annotation on every field that refers to other cached entity, for example:

             

             

            @Cache (usage=CacheConcurrencyStrategy.TRANSACTIONAL)
            @Entity
            public class EntityA {
              @Cache (usage=CacheConcurrencyStrategy.TRANSACTIONAL)
              private Set<EntityB> entitiesB;
            }
            
            @Cache (usage=CacheConcurrencyStrategy.TRANSACTIONAL)
            @Entity
            public class EntityB {
            }
            

             

            As I mentioned, it may be not a general rule about caching entities, but in my case, after I've added these annotations on fields-level everything started to work as expected.

            • 3. Re: Configuring JBoss cache for EJB3 entities (JBoss-5.0.0.GA)
              jaikiran pai Master

              piotrekde wrote:

               

              It seems that query caching and entity caching are quite distinct.

               

              Yes, they are different things. This is a good read http://www.javalobby.org/java/forums/t48846.html to understand what each one does.