Hibernate second-level cache concurrency strategy after moving from 5.x to 6.x: which setting preserves correctness?
0 reputation · 24 Sept 2023, 10:09 UTC
0 reputation · 24 Sept 2023, 10:09 UTC
I am planning a migration of an application from Hibernate 5.x to 6.x and need to settle one unresolved decision before touching configuration: the second-level cache concurrency strategy for entities that are read frequently but updated by multiple threads.
The L2 cache is configured per SessionFactory with a pluggable provider (EhCache or Infinispan in our case), and each entity declares a strategy such as READ_ONLY, NONSTRICT_READ_WRITE, READ_WRITE, or TRANSACTIONAL. I understand 6.x changed eviction semantics around cacheable entities, and I am unsure whether the strategy choices that were safe under 5.x still guarantee the same freshness guarantees under 6.x, particularly for NONSTRICT_READ_WRITE where stale reads across threads were already a documented risk.
A second constraint: our cache region names are shared across several entities today, and I have seen claims that duplicate region names within one SessionFactory can cause silent cache misses, which would make any correctness testing misleading.
When migrating from Hibernate 5.x to 6.x, the READ_WRITE concurrency strategy is the only one that guarantees the same level of data freshness for entities updated by multiple threads. NONSTRICT_READ_WRITE remains a stale‑read risk, and TRANSACTIONAL only works with a JTA‑aware provider such as Infinispan in transactional mode.
TRANSACTIONAL strategy is meaningful only when the cache provider implements JTA transactions (e.g., Infinispan configured for transactional mode).READ_WRITE behavior, depending on the region factory implementation.TRANSACTIONAL for correctness.hibernate.generate_statistics=true.READ_WRITE.NONSTRICT_READ_WRITE and observe a possible stale read.stats.getCacheHits(), getCacheMisses(), getCachePuts().For correctness in a concurrent environment, switch all frequently updated entities to READ_WRITE and remove any NONSTRICT_READ_WRITE declarations unless you accept brief staleness. If you need true transactional guarantees, migrate to a JTA‑aware provider and keep TRANSACTIONAL. Finally, audit region names to avoid accidental sharing.
To fine‑tune the recommendation for your specific environment, could you confirm which cache provider and version (EhCache 3.x, Infinispan 13.x, etc.) you plan to use in the 6.x deployment? This will help determine whether TRANSACTIONAL can be safely enabled.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.