1 Reply Latest reply on Sep 12, 2016 5:43 PM by ryanrupp

    Hibernate second level cache using Infinispan in cluster defaulting to local cache instead of invalidation

    ryanrupp

      On Wildfly 10.0.0 I'm running into an issue where I'm expecting Wildfly to use an Infinispan invalidation cache but it ends up using a local cache so invalidation doesn't happen correctly of the second level cache (consistency issues between nodes). I verified this was a local cache by looking at the JMX MBeans (where the entity caches have local-cache in the configuration details). This is what my Wildfly XML looks like for the Hibernate cache manager:

       

      <cache-container name="hibernate" default-cache="local-query" module="org.hibernate.infinispan">
                      <transport lock-timeout="60000"/>
                      <local-cache name="local-query">
                          <eviction strategy="LRU" max-entries="10000"/>
                          <expiration max-idle="100000"/>
                      </local-cache>
                      <invalidation-cache name="entity" statistics-enabled="true" mode="SYNC">
                          <transaction mode="NON_XA"/>
                          <eviction strategy="LRU" max-entries="10000"/>
                          <!-- MGP - increase cache expiration to 15 minutes as the default of 100 seconds is too low -->
                          <expiration max-idle="900000"/>
                      </invalidation-cache>
                      <replicated-cache name="timestamps" mode="ASYNC"/>
                  </cache-container>
      

      The only change I made from the standard configuration was to increase the max-idle expiration and tried to enable statistics on the entity cache. This may be because I'm confusing how this actually works, I thought the defined "entity" cache was basically a template for any Hibernate second level entity cache and it would use this and create a cache per entity following the invalidation configuration. When I debugged the Wildfly/Hibernate/Infinispan execution though it looks like the EmbeddedCacheManager.getCache is getting called with a name per entity e.g. com.mycompany.entityName - the configuration for this then just seems to default to local cache. Do I have to configure an invalidation cache explicitly per entity or is there someway to create a "template" for the default entity cache?

       

      Other relevant information that may be affecting this is Wildfly is not creating the persistent unit, I'm essentially using the configuration detailed here - https://docs.jboss.org/author/display/WFLY8/Spring+applications+development+and+migration+guide - specifically the "Using Spring-managed persistence units" section - I'm not sure if this is may be affecting things since Spring is creating the EntityManagerFactory rather than Wildfly. I've tried explicitly configuring the region factory in my persistence.xml to try using the SharedInfinispanRegionFactory (provided by Wildfly) and the JndiInfinispanRegionFactory (provided by Hibernate) but I got the same results. Also for the "timestamps" cache I don't actually see this cache via JMX but instead see a cache with the name "org.hibernate.cache.spi.UpdateTimestampsCache" and it is local instead of the expected replicated/async (again could be a misunderstanding of how this is supposed to work).

       

      Session clustering does work correctly, I see the replicated "web" cache and this works as expected just entity caching for Hibernate isn't defaulting to the expected caches (or I'm missing the configuration here!). Thanks!

       

      Guides I've looked at for reference:

       

      Wildfly JPA guide - https://docs.jboss.org/author/display/WFLY10/JPA+Reference+Guide#JPAReferenceGuide-UsingtheInfinispansecondlevelcache

      Older guide about Infinispan as the second level cache provider for JPA - https://docs.jboss.org/author/display/ISPN/Using+Infinispan+as+JPA-Hibernate+Second+Level+Cache+Provider - talks about how you can configure each individual cache differently but I'm not sure what part Wildfly plays here in managing the configuration.

        • 1. Re: Hibernate second level cache using Infinispan in cluster defaulting to local cache instead of invalidation
          ryanrupp

          Update - Out of curiosity I tried upgrading Wildfly from 10.0.0 to 10.1.0 and this fixed the issue! Entity caches now show up as invalidation, <entity>-pending-puts caches are local, and the timestamps cache "org.hibernate.cache.spi.UpdateTimestampsCache" is now repl_async. So this works as I would expect it to now! I didn't dig into the commit logs to see whether this was fixed by Wildfly, Hibernate, or Infinsipan. Side note but I think because I'm using Spring to manage the persistence unit I do have to specify the region factory so I have in persistence.xml:

           

           

          <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.jboss.as.jpa.hibernate5.infinispan.SharedInfinispanRegionFactory"/>