Hibernate / EclipseLink Interview questions
How does EclipseLink cache query results, and what invalidation strategies are available?
Beyond caching individual entities by primary key, EclipseLink can also cache the results of specific named or parameterized queries, avoiding re-execution of the same query entirely when the result set is likely still valid, controlled through the eclipselink.cache-usage query hint or @QueryHints on a named query.
@NamedQuery( name = "Department.findActive", query = "SELECT d FROM Department d WHERE d.active = true", hints = { @QueryHint(name = "eclipselink.query-results-cache", value = "true"), @QueryHint(name = "eclipselink.query-results-cache.expiry", value = "60000") } )
Individual entity cache entries can be invalidated a few different ways: automatically, when a managed transaction updates that entity, EclipseLink invalidates or refreshes its own cached copy; via a configured time-to-live expiry set on @Cache; or explicitly, by calling em.getEntityManagerFactory().getCache().evict(...) to force-remove specific entries.
Query-results caching is separate from entity caching and comes with a sharper edge: since it caches an entire result list keyed to the query and its parameters, it's more prone to staleness if the underlying data changes outside of EclipseLink's own tracked transactions (for example, another application writing to the same database), which is why it's typically applied selectively to queries against genuinely slow-changing data.
More Related questions...