0

I have a web application which is written in JEE. I am using hibernate as a JPA provider and I would like to use Infinispan as a second level cache. As I went through internet I noticed that only adding several lines is needed. I added in my persistence.xml these lines

<property name="hibernate.cache.provider_class" value="org.hibernate.cache.infinispan.InfinispanRegionFactory"/>
<property name="javax.persistence.sharedCache.mode" value="ENABLE_SELECTIVE"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.infinispan.InfinispanRegionFactory"/>
<property name="hibernate.cache.use_query_cache" value="true"/>

but during deploying ear and war file on JBoss I receive an error message

Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.spi.CacheImplementor]
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:244)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
        at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:242)
        at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879)
        ... 83 more
Caused by: org.hibernate.cache.CacheException: Unable to start region factory
        at org.hibernate.cache.infinispan.InfinispanRegionFactory.start(InfinispanRegionFactory.java:415)
        at org.hibernate.internal.CacheImpl.<init>(CacheImpl.java:49)
        at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:28)
        at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:20)
        at org.hibernate.service.internal.SessionFactoryServiceRegistryImpl.initiateService(SessionFactoryServiceRegistryImpl.java:46)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234)
        ... 88 more
Caused by: org.infinispan.jmx.JmxDomainConflictException: ISPN000034: There's already a JMX MBean instance type=CacheManager,name="SampleCacheManager" already registered under 'org.infinispan' JMX domain. If you want to allow multiple instances configured with same JMX domain enable 'allowDuplicateDomains' attribute in 'globalJmxStatistics' config element
        at org.infinispan.jmx.JmxUtil.buildJmxDomain(JmxUtil.java:52)
        at org.infinispan.jmx.CacheManagerJmxRegistration.updateDomain(CacheManagerJmxRegistration.java:79)
        at org.infinispan.jmx.CacheManagerJmxRegistration.buildRegistrar(CacheManagerJmxRegistration.java:73)
        at org.infinispan.jmx.AbstractJmxRegistration.registerMBeans(AbstractJmxRegistration.java:37)
        at org.infinispan.jmx.CacheManagerJmxRegistration.start(CacheManagerJmxRegistration.java:41)
        at org.infinispan.manager.DefaultCacheManager.start(DefaultCacheManager.java:639)
        at org.infinispan.manager.DefaultCacheManager.<init>(DefaultCacheManager.java:300)
        at org.hibernate.cache.infinispan.InfinispanRegionFactory.createCacheManager(InfinispanRegionFactory.java:532)
        at org.hibernate.cache.infinispan.InfinispanRegionFactory$1.doWork(InfinispanRegionFactory.java:500)
        at org.hibernate.cache.infinispan.InfinispanRegionFactory$1.doWork(InfinispanRegionFactory.java:473)
        at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.workWithClassLoader(ClassLoaderServiceImpl.java:342)
        at org.hibernate.cache.infinispan.InfinispanRegionFactory.createCacheManager(InfinispanRegionFactory.java:472)
        at org.hibernate.cache.infinispan.InfinispanRegionFactory.start(InfinispanRegionFactory.java:380)
        ... 93 more

UPDATE
When my persistence unit looks like this

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
             http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="myPersistenceUnit" transaction-type="RESOURCE_LOCAL">
    <non-jta-data-source>java:jboss/datasources/myDatasource</non-jta-data-source>
    <mapping-file>META-INF/orm_mapping.xml</mapping-file>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
         ...
        <properties>
          ...
            <property name="hibernate.cache.use_second_level_cache" value="true" />
          ...
        </properties>

    </persistence-unit>

</persistence>  

I am getting an exception

Caused by: org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given; please either disable second level cache or set correct region factory using the hibernate.cache.region.factory_class setting and make sure the second level cache provider (hibernate-infinispan, e.g.) is available on the classpath.
        at org.hibernate.cache.internal.NoCachingRegionFactory.buildEntityRegion(NoCachingRegionFactory.java:66)
        at org.hibernate.internal.SessionFactoryImpl.determineEntityRegionAccessStrategy(SessionFactoryImpl.java:619)
        at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:332)
        at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879)
        ... 83 more

After adding below line to persistence.xml

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

I am still getting exception with JMX MBean

Caused by: org.infinispan.jmx.JmxDomainConflictException: ISPN000034: There's already a JMX MBean instance type=CacheManager,name="SampleCacheManager" already registered under 'org.infinispan' JMX domain. If you want to allow multiple instances configured with same JMX domain enable 'allowDuplicateDomains' attribute in 'globalJmxStatistics' config element
        at org.infinispan.jmx.JmxUtil.buildJmxDomain(JmxUtil.java:52)
        at org.infinispan.jmx.CacheManagerJmxRegistration.updateDomain(CacheManagerJmxRegistration.java:79)
        at org.infinispan.jmx.CacheManagerJmxRegistration.buildRegistrar(CacheManagerJmxRegistration.java:73)
        at org.infinispan.jmx.AbstractJmxRegistration.registerMBeans(AbstractJmxRegistration.java:37)
        at org.infinispan.jmx.CacheManagerJmxRegistration.start(CacheManagerJmxRegistration.java:41)
        at org.infinispan.manager.DefaultCacheManager.start(DefaultCacheManager.java:639)
        at org.infinispan.manager.DefaultCacheManager.<init>(DefaultCacheManager.java:300)
        at org.hibernate.cache.infinispan.InfinispanRegionFactory.createCacheManager(InfinispanRegionFactory.java:532)
        at org.hibernate.cache.infinispan.InfinispanRegionFactory$1.doWork(InfinispanRegionFactory.java:500)
        at org.hibernate.cache.infinispan.InfinispanRegionFactory$1.doWork(InfinispanRegionFactory.java:473)
        at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.workWithClassLoader(ClassLoaderServiceImpl.java:342)
        at org.hibernate.cache.infinispan.InfinispanRegionFactory.createCacheManager(InfinispanRegionFactory.java:472)
        at org.hibernate.cache.infinispan.InfinispanRegionFactory.start(InfinispanRegionFactory.java:380)
        ... 93 more

Maybe I need some kind of infinispan config file where I will be able to place

<jmx duplicate-domains="true" />

like it is mentioned here Infinispan as second level cache hibernate

ketrab321
  • 541
  • 2
  • 12
  • 22

2 Answers2

1

You should check the EAP 7 documentation since all the configuration required to get second level cache (powered by Infinispan behind the scenes) is this:

<persistence-unit name="...">
  (...) <!-- other configuration -->
  <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
  <properties>
    <property name="hibernate.cache.use_second_level_cache" value="true" />
    <property name="hibernate.cache.use_query_cache" value="true" />
  </properties>
</persistence-unit>

https://access.redhat.com/documentation/en-us/red_hat_jboss_enterprise_application_platform/7.1/html/development_guide/java_persistence_api_jpa#second_level_caches

Galder Zamarreño
  • 5,027
  • 2
  • 26
  • 34
  • Thanks for the answer. But when I try to add `` to my persistence.xml it says that Element shared cache mode is not allowed here. Can it be something wrong with my hibernate version? Or missing library? – ketrab321 Dec 15 '17 at 08:12
  • Ups! As indicated in the docu, there are several possible values for it. Normally this is `ENABLE_SELECTIVE` #readthedocs ;) – Galder Zamarreño Jan 05 '18 at 11:26
0

The hibernate version should be 5.0.x.

persistence.xml :

<persistence ...>
        <persistence-unit ...>
                ...
                <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>  <!-- ADD THIS -->
                <properties>
                        ...
                        <property name="hibernate.cache.use_second_level_cache" value="true" />  <!-- KEEP THIS -->
                </properties>
        </persistence-unit>
</persistence>

Remove all other hibernate.cache.* properties----leaving only the above

Note that using the above setting you must explicitly mark entities as @Cacheable. It can be used the ALL setting for shared-cache-mode if not wish to explicitly mark each entity as @cacheable---- and then all entities would be cached.

Additional documentation on configuration and implementation may be found in EAP 7: Hibernate 5 User Guide(http://docs.jboss.org/hibernate/orm/5.0/userguide/html_single/Hibernate_User_Guide.html#caching) and Hibernate 5 Developer Guide(http://docs.jboss.org/hibernate/orm/5.0/devguide/en-US/html_single/#d5e1433) and Infinispan 8 User Guide(http://infinispan.org/docs/8.1.x/user_guide/user_guide.html)

Anup Dey
  • 876
  • 5
  • 6
  • Thanks for the answer. If you can see my update I still get exceptions connected to JMX MBean – ketrab321 Dec 28 '17 at 10:31
  • @bartekms274: When using library mode and the global configuration builder to create a new cache manager, then enable allowDuplicateDomains attribute in globalJmxStatistics config to resolve this exception... Make sure that the CachManager is stopped , during the un-deployment stage – Anup Dey Dec 29 '17 at 08:43
  • Thanks for your help. I finally managed to run my app with infinispan with use of library mode. W would like to ask one more thing. I would like to use it with Hibernate so I nedd to annotate every entity with '@Cache'. Do I need to specify in infinispan.xml config file local cache for every entity like it is shown in infinispan docs? [Infinispan docs](http://infinispan.org/docs/stable/user_guide/user_guide.html#jpa_cache_store) – ketrab321 Jan 03 '18 at 19:11
  • JPA cache store has nothing to do with Hibernate use of Infinispan as second level cache. – Galder Zamarreño Jan 05 '18 at 11:26
  • Btw, the JMX domain duplicate exceptions are gone in latest Infinispan 9.2.x pre-release. – Galder Zamarreño Jan 05 '18 at 11:28