5 Replies Latest reply on Dec 13, 2011 8:21 AM by Kavan Desai

    Jboss AS 7: Infinispam as Hibernate 2nd level cache

    Diego Silveira Newbie

      Hello,

       

      It's my first question here.

      I've been trying to configure my application to use Infinispam (provided by JBoss AS 7) as Hibernate (3.5.6-Final) 2nd level cache.

       

      My application is Spring 3 based, so here is the configuration of Hibernate Session Factory:

       

       

      @Configuration
      public class SessionFactoryContext {
      
          @Resource
          private DataSource dataSource;
      
      
          @Bean( name = "sessionFactory" )
          public AnnotationSessionFactoryBean sessionFactoryBean() {
      
              Properties hibernateProperties = new Properties();
              hibernateProperties.setProperty( "hibernate.dialect", "org.hibernate.dialect.MySQLDialect" );
              hibernateProperties.setProperty( "hibernate.show_sql", "true" );
              hibernateProperties.setProperty( "hibernate.format_sql", "true" );
              hibernateProperties.setProperty( "hibernate.use_sql_comments", "true" );
              hibernateProperties.setProperty( "hibernate.connection.characterEncoding", "utf8" );
      
              hibernateProperties.setProperty( "hibernate.cache.use_second_level_cache", "true" );
              hibernateProperties.setProperty( "hibernate.cache.use_query_cache", "true" );
              hibernateProperties.setProperty( "hibernate.cache.region.factory_class", "org.hibernate.cache.infinispan.JndiInfinispanRegionFactory" );
              hibernateProperties.setProperty( "hibernate.cache.infinispan.cachemanager", "java:jboss/infinispan/hibernate-cache-container" );
      
              AnnotationSessionFactoryBean sf = new AnnotationSessionFactoryBean();
              sf.setDataSource( dataSource );
              sf.setHibernateProperties( hibernateProperties );
              sf.setPackagesToScan( ... );
      
              return sf;
      
          }
      }
      

       

      JBoss standalone.xml

       

      <subsystem xmlns="urn:jboss:domain:infinispan:1.0" default-cache-container="hibernate-cache-container">
          <cache-container name="hibernate-cache-container" default-cache="hibernate-cache">
              <local-cache name="hibernate-cache"/>
          </cache-container>
      </subsystem>
      

       

      MANIFEST.MF

       

      Dependencies: org.infinispan export
      

       

      The application works fine if I remove the hibernate properties "hibernate.cache.region.factory_class" and "hibernate.cache.infinispan.cachemanager", but with these properties I get the following exception while deploying the application at JBoss AS 7 (7.0.2.Final):

       

      ...
      2011-12-12::11:56:56,904 INFO  [org.jboss.as.clustering.infinispan.subsystem] (Controller Boot Thread) Activating Infinispan subsystem.
      ...
      
      2011-12-12::11:57:16,058 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/api-checkout]] (MSC service thread 1-2) Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'campaignInputTypeDAO' defined in "/content/api-checkout.war/WEB-INF/lib/pd-core-gateway-0.0.1-SNAPSHOT.jar/br/com/buscape/pd/gateway/model/impl/CampaignInputTypeDAOImpl.class": Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: : Error creating bean with name 'sessionFactory' defined in class path resource [br/com/buscape/pd/dbaccess/config/SessionFactoryContext.class]: Invocation of init method failed; nested exception is org.hibernate.cache.CacheException: Unable to start region factory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [br/com/buscape/pd/dbaccess/config/SessionFactoryContext.class]: Invocation of init method failed; nested exception is org.hibernate.cache.CacheException: Unable to start region factory
                at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:730) [spring-beans-3.0.6.RELEASE.jar:]
                at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:196) [spring-beans-3.0.6.RELEASE.jar:]
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1003) [spring-beans-3.0.6.RELEASE.jar:]
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:907) [spring-beans-3.0.6.RELEASE.jar:]
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485) [spring-beans-3.0.6.RELEASE.jar:]
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) [spring-beans-3.0.6.RELEASE.jar:]
                at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293) [spring-beans-3.0.6.RELEASE.jar:]
                at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) [spring-beans-3.0.6.RELEASE.jar:]
                at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290) [spring-beans-3.0.6.RELEASE.jar:]
                at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192) [spring-beans-3.0.6.RELEASE.jar:]
                at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585) [spring-beans-3.0.6.RELEASE.jar:]
                at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) [spring-context-3.0.6.RELEASE.jar:]
                at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) [spring-context-3.0.6.RELEASE.jar:]
                at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:282) [spring-web-3.0.6.RELEASE.jar:]
                at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:204) [spring-web-3.0.6.RELEASE.jar:]
                at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47) [spring-web-3.0.6.RELEASE.jar:]
                at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3368) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                at org.apache.catalina.core.StandardContext.start(StandardContext.java:3821) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
                at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:70) [jboss-as-web-7.0.2.Final.jar:7.0.2.Final]
                at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1824)
                at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1759)
                at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [:1.6.0_26]
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [:1.6.0_26]
                at java.lang.Thread.run(Thread.java:662) [:1.6.0_26]
      Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [br/com/buscape/pd/dbaccess/config/SessionFactoryContext.class]: Invocation of init method failed; nested exception is org.hibernate.cache.CacheException: Unable to start region factory
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420) [spring-beans-3.0.6.RELEASE.jar:]
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) [spring-beans-3.0.6.RELEASE.jar:]
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) [spring-beans-3.0.6.RELEASE.jar:]
                at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293) [spring-beans-3.0.6.RELEASE.jar:]
                at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) [spring-beans-3.0.6.RELEASE.jar:]
                at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290) [spring-beans-3.0.6.RELEASE.jar:]
                at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192) [spring-beans-3.0.6.RELEASE.jar:]
                at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:848) [spring-beans-3.0.6.RELEASE.jar:]
                at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:790) [spring-beans-3.0.6.RELEASE.jar:]
                at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707) [spring-beans-3.0.6.RELEASE.jar:]
                at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:795) [spring-beans-3.0.6.RELEASE.jar:]
                at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:723) [spring-beans-3.0.6.RELEASE.jar:]
                ... 23 more
      Caused by: org.hibernate.cache.CacheException: Unable to start region factory
                at org.hibernate.cache.infinispan.InfinispanRegionFactory.start(InfinispanRegionFactory.java:260) [hibernate-infinispan-3.5.6-Final.jar:]
                at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:250) [hibernate-core-3.5.6-Final.jar:]
                at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385) [hibernate-core-3.5.6-Final.jar:]
                at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954) [hibernate-annotations-3.5.6-Final.jar:]
                at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:860) [spring-orm-3.0.6.RELEASE.jar:]
                at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:779) [spring-orm-3.0.6.RELEASE.jar:]
                at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211) [spring-orm-3.0.6.RELEASE.jar:]
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477) [spring-beans-3.0.6.RELEASE.jar:]
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417) [spring-beans-3.0.6.RELEASE.jar:]
                ... 34 more
      Caused by: java.lang.IllegalStateException
                at org.jboss.msc.value.InjectedValue.getValue(InjectedValue.java:47)
                at org.jboss.as.naming.service.BinderService.getValue(BinderService.java:116)
                at org.jboss.as.naming.service.BinderService.getValue(BinderService.java:43)
                at org.jboss.msc.service.ServiceControllerImpl.getValue(ServiceControllerImpl.java:1216)
                at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:111)
                at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:73)
                at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:173)
                at org.jboss.as.naming.InitialContext.lookup(InitialContext.java:57)
                at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:209)
                at javax.naming.InitialContext.lookup(InitialContext.java:392) [:1.6.0_26]
                at org.hibernate.cache.infinispan.JndiInfinispanRegionFactory.locateCacheManager(JndiInfinispanRegionFactory.java:75) [hibernate-infinispan-3.5.6-Final.jar:]
                at org.hibernate.cache.infinispan.JndiInfinispanRegionFactory.createCacheManager(JndiInfinispanRegionFactory.java:68) [hibernate-infinispan-3.5.6-Final.jar:]
                at org.hibernate.cache.infinispan.InfinispanRegionFactory.start(InfinispanRegionFactory.java:246) [hibernate-infinispan-3.5.6-Final.jar:]
                ... 42 more
      

       

      It looks like JBoss is not prepared to support the lookup of "java:jboss/infinispan/hibernate-cache-container" at the moment the session factory is instantiated by Spring, but the datasource ("java:jboss/datasources/dbpd03DS") is already bound and is succesfuly injected.

       

      Can you help me please?

       

      Thank you in advance.

        • 1. Re: Jboss AS 7: Infinispam as Hibernate 2nd level cache
          Kavan Desai Newbie

          Hi Diego,

           

          I was able to configure Infinispan in my applciation in JBOSS AS 7.1.0 Beta

           

          Following is the property element of my persistence.xml

           

          <properties>

                      <!-- According to your database type, you can set dialect properly -->           

                      <!--<property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect"/>-->

                      <!--<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>-->

                      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>

                      <property name="hibernate.cache.use_second_level_cache" value="true" />

                      <property name="hibernate.show_sql" value="true" />

                      <property name="hibernate.cache.use_query_cache" value="true" />

                      <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.infinispan.JndiInfinispanRegionFactory" />

                      <!-- For jboss as 6.1.0 Final -->

                      <!-- <property name="hibernate.cache.infinispan.cachemanager" value="java:CacheManager/entity" /> -->

                      <!-- For jboss as 7.1.0 Beta -->

                      <property name="hibernate.cache.infinispan.cachemanager" value="java:jboss/infinispan/hibernate" />

                      <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>

                      <property name="hibernate.cache.infinispan.use_synchronization"  value="false"/>

                      <property name="hibernate.cache.infinispan.statistics" value="true"/>

                      <property name="hibernate.cache.infinispan.cfg" value="/home/infinispan/cacheprovider-configs.xml"/>

                      <!-- comment the line below at least for JBOSS AS 7.1.0 Beta -->

                      <!-- <property name="hibernate.session_factory_name" value="daytraderAppSF"/> -->

                      <property name="jboss.entity.manager.factory.jndi.name" value="java:/myappnameEntityManagerFactory"/>

                      <property name="hibernate.generate_statistics" value="true"/>

                      <!-- This Hibernate property will update database schema according to EJB3 annotation definitions

                      Possible value: validate, update, create, create-drop -->

                      <!--<property name="hibernate.hbm2ddl.auto" value="update"/>-->

                  </properties>

           

          jboss-standalone-full.xml -

           

          <subsystem xmlns="urn:jboss:domain:infinispan:1.1" default-cache-container="hibernate">

                      <cache-container name="hibernate" default-cache="local-query">

                          <local-cache name="entity">

                              <eviction strategy="LRU" max-entries="10000"/>

                              <expiration max-idle="100000"/>

                          </local-cache>

                          <local-cache name="local-query">

                              <eviction strategy="LRU" max-entries="10000"/>

                              <expiration max-idle="100000"/>

                          </local-cache>

                          <local-cache name="timestamps">

                              <eviction strategy="NONE"/>

                          </local-cache>

                      </cache-container>

                  </subsystem>

          • 2. Re: Jboss AS 7: Infinispam as Hibernate 2nd level cache
            Diego Silveira Newbie

            Hello Kavan,

             

            Thank you for the reply. Looks like my configuration files are similar to yours. I managed to start my application, although I didn't love the solution:

             

            Just put an empty beans.xml in WEB-INF dir and create a dummy EJB to preload the cache-container and make JBoss bind the JNDI. This way I got rid of IllegalStateException, and accordingly to the logs, Hibernate is now using Infinispan.

             

            @Singleton

            public class CacheResource {

             

             

                @SuppressWarnings( "unused" )

                @Resource(lookup="java:jboss/infinispan/hibernate-cache-container")

                private static Object cacheContainer;

               

            }

             

            Question: Said that my app does not use EJBs, is there a better solution, forcing JBoss to start the cache-container even when there's no EJB to inject the CacheManager into?

             

            Thanks!

            • 3. Re: Jboss AS 7: Infinispam as Hibernate 2nd level cache
              Kavan Desai Newbie

              Sounds interesting!

              I have one spring based applciation which does not use EJB I will try to configure Infinispan as 2nd level cache for hibernate in it to see what happens.

              • 4. Re: Jboss AS 7: Infinispam as Hibernate 2nd level cache
                Diego Silveira Newbie

                Awesome!

                 

                Please let me know what happened after your tests.

                • 5. Re: Jboss AS 7: Infinispam as Hibernate 2nd level cache
                  Kavan Desai Newbie

                  Hi Diego,

                   

                  I aslo got the same error

                   

                  Caused by: org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.infinispan.JndiInfinispanRegionFactory]

                   

                  However perticular bug is fixed in Hibernate 3.5.0 Final.

                  https://hibernate.onjira.com/browse/HHH-5332

                   

                  I checked the application and it is using hibernate 3.4.

                  That may be the problem.