Hibernate second-level cache and external backup utility restoration boundary
0 reputation · 04 Dec 2023, 13:03 UTC
0 reputation · 04 Dec 2023, 13:03 UTC
Determine whether Hibernate's second-level cache must be evicted or invalidated after an external backup utility restores entity data, before new sessions read those entities.
Hibernate caches persistent objects across sessions. External backup tools operate outside the session factory and may not understand Hibernate's internal serialization format. This boundary between SessionFactory cache and restore behavior differs across Hibernate 5.x and 6.x, particularly for eviction and serialization.
The unresolved design question is whether unrestored cache entries can return stale objects across session boundaries, and whether L2 cache snapshots should be serialized alongside entity data given potential major-release mismatch.
Is cache invalidation required after every external restore? Can deterministic pagination queries confirm row-count consistency after restore? Should L2 snapshots be excluded from backup serialization to prevent cross-release incompatibility?
35275 reputation · 04 Dec 2023, 20:04 UTC
Yes. If the restored data overlaps entities that were cached in Hibernate's second-level cache, you must explicitly evict or invalidate those cache regions before new sessions read them. The L2 cache does not detect external database changes, so pre-restore entries can be served as stale data indefinitely.
Confirmed: A restore performed by an external utility bypasses Hibernate's transaction and event system. The SessionFactory cache has no built-in hook to observe those changes, and no Hibernate 5.x or 6.x release adds automatic invalidation for out-of-band writes.
Setup-dependent: Whether stale reads actually occur depends on whether the affected entity regions were populated in L2 before the restore and whether your cache provider persists entries across application restarts. If the restore also overwrites cache region files, provider-specific behavior applies.
SessionFactory cache API.Deterministic pagination with setFirstResult and maxResults returns correct row counts from the database after the restore. It does not purge or refresh L2 entries. A paginated query can show the right number of rows while individual entity lookups still return stale cached objects.
Exclude L2 cache snapshots from external backup serialization. Hibernate 5.x and 6.x do not guarantee binary compatibility of internal cache formats across major releases. Restoring serialized cache data from a different major version can cause deserialization failures or silent corruption.
After the restore, start a new SessionFactory, query an entity that was previously cached, and compare the returned state against the restored database row. If the object is stale without explicit eviction, the cache must be cleared before production use.
// Scoped eviction after external restore
sessionFactory.getCache().evict(com.example.model.Entity.class);
Diagnostic question: Does the external backup utility include serialized Hibernate L2 cache files, or only database dump data? If it includes cache files, excluding them from the backup set is part of the fix; if it does not, region eviction alone is sufficient.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.