spring-boothibernateinfinispan

How do I use Infinispan Spring Boot Starter together with Infinispan as a 2nd level cache provider for Hibernate?


I am using the Infinispan Spring Boot Starter (infinispan-spring-boot3-starter-embedded) together with Spring Session distribute the session. This works perfectly.

My application also uses Hibernate (not JPA, but instead using hibernate's SessionFactory). To get a distributed second level cache, I have set the option:

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

This sets the region factory to an implementation of org.infinispan.hibernate.cache.v60.InfinispanRegionFactory. This creates a new CacheManager - not reusing the one I already have. The issue with this seems to be that their default transports collide, as my logs fill up with messages like:

2023-10-30 13:58:09.311 ERROR 21424 [4,Rainbow-12965] o.j.l.Log4J2LogImpl : JGRP000191: failed receiving packet from /172.26.127.126:51131: java.io.EOFException

2023-10-30 13:58:09.467 ERROR 21424 [4,Rainbow-12965] o.j.l.Log4J2LogImpl : JGRP000191: failed receiving packet from /172.26.127.126:51131: java.lang.IllegalArgumentException: invalid magic number 258; needs to be in range [0..100]

Besides, from what I understand Infinispan recommends using only one CacheManager. So what is the method to avoid this, and if possible, reuse the existing CacheManager?


Solution

  • I ended up using a custom region factory:

    @Component("cacheRegionFactory")
    public class CacheManagerReusingInfinispanCacheRegionFactory extends InfinispanRegionFactory {
    
        @Resource
        private EmbeddedCacheManager cacheManager;
    
        @Override
        protected EmbeddedCacheManager createCacheManager(Properties properties, ServiceRegistry serviceRegistry) {
            return cacheManager;
        }
    }
    

    and use set the property cacheRegionFactory in org.springframework.orm.hibernate5.LocalSessionFactoryBean.

    One caveat to this approach is that if you use spring-boot-actuator, you may have to exclude CacheMetricsAutoConfiguration.class since it appears to pick up some internal Hibernate caches that are not generating statistics, causing a NullPointerException.