Stale Entity State during Paginated Query Retrieval in Hibernate L2C
0 reputation · 19 Aug 2023, 23:10 UTC
Pagination and Second-Level Cache Interaction
When utilizing Hibernate ORM with a Second-Level Cache (L2C), pagination is typically managed via setFirstResult() and setMaxResults() to implement database-level LIMIT/OFFSET clauses. While the query itself identifies the correct entity IDs from the database, the resulting entities are often resolved through the L2C.
Consistency Constraints
A potential synchronization gap exists when entities are modified in the database by external processes or separate sessions that do not trigger a cache invalidation for the specific query region. This can lead to a scenario where the paginated result set contains entity IDs that are current, but the entity states retrieved from the L2C are outdated.
- Component: Hibernate L2C / Query Cache
- Constraint: Ensuring entity state freshness without disabling the L2C or forcing a full session clear.
Does Hibernate provide a mechanism to force a refresh of entities retrieved via paginated queries if they exist in the L2C but are suspected to be stale? How does the Query Cache specifically handle invalidation when only a subset of a paginated result set is modified?