I have a jdbc update query which updates a table. And entity cache is maintained for the same table in hibernate. I need to tell hibernate to refresh the cache region for that entity which is updated by the jdbc query.
Asked
Active
Viewed 237 times
1
-
This should help you [How hibernate ensures second level cache is updated with latest data in database](http://stackoverflow.com/questions/30413321/how-hibernate-ensures-second-level-cache-is-updated-with-latest-data-in-database) – Tomz Jan 04 '17 at 11:47
1 Answers
2
You're looking for
sessionFactory.getCache().evictEntityRegion(MyEntity.class)
This removes the stale entries from cache. However, if you do that before JDBC update, in the window between this invalidation and the JDBC update, the cache can be populated again by concurrent reads, so if your application does not stop all DB accesses, you could end up with some stale entries.
Another region eviction after the update could sort it out, but between the JDBC query and region eviction you're prone to inconsistent data in the cache, again.

Radim Vansa
- 5,686
- 2
- 25
- 40