svenpanko
svenpanko

Reputation: 180

Hibernate + Hazelcast Query Cache not refreshing sometimes

we observe a strange problem with our Hibernate query cache when we use Hazelcast as cache provider. This is our setup:

Configuration in persistence.xml:

   <properties>
        <property name="javax.persistence.sharedCache.mode" value="DISABLE_SELECTIVE"/>

        <property name="hibernate.cache.region_prefix" value="custom-pu"/>
        <property name="hibernate.enable_lazy_load_no_trans" value="true"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
        <property name="hibernate.default_batch_fetch_size" value="50"/>
        <property name="hibernate.jdbc.batch_size" value="50"/>
        <property name="hibernate.event.merge.entity_copy_observer" value="log"/>

        <property name="hibernate.session_factory_name" value="customSessionFactory"/>

        <property name="hibernate.cache.hazelcast.configuration_file_path"
                  value="${config.dir}/custom-hazelcast.xml"/>
        <property name="hibernate.cache.hazelcast.instance_name" value="customInstance"/>
        <property name="hibernate.cache.hazelcast.use_native_client" value="false"/>
        <property name="hibernate.cache.region.factory_class"
                  value="com.hazelcast.hibernate.HazelcastLocalCacheRegionFactory"/>
        <property name="hibernate.cache.hazelcast.shutdown_on_session_factory_close" value="false"/>
        <property name="hibernate.cache.use_second_level_cache" value="true"/>
        <property name="hibernate.cache.use_query_cache" value="true"/>

        <property name="javax.persistence.validation.group.pre-persist" value="javax.validation.groups.Default"/>
        <property name="javax.persistence.validation.group.pre-update" value="javax.validation.groups.Default"/>
    </properties>

Sometimes a user updates some data and users ON THE SAME NODE or on another node don't see the updates - no SQL queries are sent against the DB (as observed in the logs). Clearing the query cache on one node leads to ALL NODES requesting fresh data from the DB.

We first observed this with a list of entities backed by a database view, but the view is correctly annotated with @Synchronized, and we saw during debugging that Hibernate correctly checks the regions for the mentioned tables. If the query cache stopped working, a cache timeout (configured in hazelcast.xml) leads to correct results being displayed eventually.

Digging in the logs we saw that once the query cache stops working the timestamp cache reports that the cached data for the regions mentioned in @Synchronized is older than the query cache result, so Hibernate does not discard the old entry and request new data.

I checked the config in persistence.xml multiple times now and saw that we did not activate this property: hibernate.cache.use_minimal_puts - would that make a difference (I could of course simply apply it, but I want to know why that would solve our problem).

We are at a loss here, because we don't know how to correctly reproduce the problem and we don't know what might cause it. Additionally, the hibernate-cache integration from Hazelcast is not very verbose, so it is hard to see what's going on...

Thanks in advance!

Upvotes: 1

Views: 912

Answers (1)

Vlad Mihalcea
Vlad Mihalcea

Reputation: 153740

Hibernate only offers a 2nd-level cache SPI. The Ehcache module is maintained by Ehcache team, the same with Infinispan and Hazelcast, so someone from Hazelcast team might know what is causing this problem.

Since you asked about hibernate.cache.use_minimal_puts, I'll answer this part of your question. This configuration property optimizes second-level cache operation to minimize write, while you pay the price for more frequent reads.

This setting is useful for clustered caches, so Hazelcast might benefit from it. Usually, the 2nd-level caching provider enables it automatically for clustering environments.

Upvotes: 0

Related Questions