# Hibernate Query Cache invalidation after persisting entity

**URL:** <https://discourse.hibernate.org/t/hibernate-query-cache-invalidation-after-persisting-entity/1334>\
**Category:** Hibernate ORM\
**Created:** [September 3, 2018, 2:29pm UTC](https://discourse.hibernate.org/t/hibernate-query-cache-invalidation-after-persisting-entity/1334 "2018-09-03T14:29:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pushkar\_Deole](https://avatars.discourse-cdn.com/v4/letter/p/bb73d2/32.png) [@Pushkar\_Deole](https://discourse.hibernate.org/u/Pushkar_Deole)\
**Post date:** [September 3, 2018, 2:29pm UTC](https://discourse.hibernate.org/t/hibernate-query-cache-invalidation-after-persisting-entity/1334/1 "2018-09-03T14:29:18Z")

</div>

I am trying to use Hibernate query cache in my application.  
Hibernate version: 4.3.11  
EHCache version: 2.10.5

I am able to get the query cache working and it is caching the results so the subsequent executions of the same read query (with same parameters) is fetching data from the cache.  
Now I try to insert a record, using hibernate API, that matches with the query predicates.  
Once the record is inserted, I execute the read query again and it goes to fetch data from database and then subsequent executions again use cache.

From this what I understand is: when the record (that matches the query predicates and parameters) is inserted into the database, the query cache with old data is invalidated so the next query execution goes to database. The question I have here is: when the insert of record that matches the query predicates and parameters is done, can the hibernate also update the query cache with the new results so the query cache now has latest results and the next read query execution doesn’t need to go back to database ?

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [September 3, 2018, 2:43pm UTC](https://discourse.hibernate.org/t/hibernate-query-cache-invalidation-after-persisting-entity/1334/2 "2018-09-03T14:43:27Z")

</div>

> [@Pushkar\_Deole](#):
>
> The question I have here is: when the insert of record that matches the query predicates and parameters is done, can the hibernate also update the query cache with the new results so the query cache now has latest results and the next read query execution doesn’t need to go back to database ?

No, it can not. If you want that, you need to use the database-specific query caching features. In fact, if you properly tune your database server, it might be that you don’t even need the Hibernate query cache.

---

<div class="post-metadata">

**Author:** ![Pushkar\_Deole](https://avatars.discourse-cdn.com/v4/letter/p/bb73d2/32.png) [@Pushkar\_Deole](https://discourse.hibernate.org/u/Pushkar_Deole)\
**Post date:** [September 3, 2018, 3:07pm UTC](https://discourse.hibernate.org/t/hibernate-query-cache-invalidation-after-persisting-entity/1334/3 "2018-09-03T15:07:28Z")

</div>

@vlad

_If you want that, you need to use the database-specific query caching features_

do you mean the query caching capabilities provided by database?

_if you properly tune your database server, it might be that you don’t even need the Hibernate query cache_

Even if this is done, the database server will still be hit to fetch the data right? I want the database not to be hit once I cache the query in hibernate query cache and rest all will be handled automatically i.e. the cache will be updated by hibernate when a new record (that would affect the results of cached query) is inserted into the database.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [September 3, 2018, 3:11pm UTC](https://discourse.hibernate.org/t/hibernate-query-cache-invalidation-after-persisting-entity/1334/4 "2018-09-03T15:11:01Z")

</div>

> do you mean the query caching capabilities provided by database?

If you’re using a database like Oracle, then yes.

> Even if this is done, the database server will still be hit to fetch the data right? I want the database not to be hit once I cache the query in hibernate query cache and rest all will be handled automatically i.e. the cache will be updated by hibernate when a new record (that would affect the results of cached query) is inserted into the database.

If you tune the DB to store the working set in memory (e.g. buffer pool), the query can be served so fast that it makes no sense to use a query cache anyway. Assuming the query always takes less than 1 millisecond, why would you cache it?

---

<div class="post-metadata">

**Author:** ![Pushkar\_Deole](https://avatars.discourse-cdn.com/v4/letter/p/bb73d2/32.png) [@Pushkar\_Deole](https://discourse.hibernate.org/u/Pushkar_Deole)\
**Post date:** [September 3, 2018, 4:13pm UTC](https://discourse.hibernate.org/t/hibernate-query-cache-invalidation-after-persisting-entity/1334/5 "2018-09-03T16:13:48Z")

</div>

_If you tune the DB to store the working set in memory (e.g. buffer pool), the query can be served so fast that it makes no sense to use a query cache anyway. Assuming the query always takes less than 1 millisecond, why would you cache it?_

Well… the product supports multiple databases viz. postgresql, oracle, sql server, mysql etc. so need to explore if each of the database supports that and how… which is going to be a challenge since we need to ask the customer to configure the database to use those caching capabilities.  
Secondly, the application has multiple java processes which could be deployed on multiple nodes, however a single database instance is supported. Since multiple processes converge into a single db, the database could become a bottleneck even if it serves the results pretty quick.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [September 3, 2018, 5:36pm UTC](https://discourse.hibernate.org/t/hibernate-query-cache-invalidation-after-persisting-entity/1334/6 "2018-09-03T17:36:31Z")

</div>

If you use multiple databases, it’s mandatory to know how to tune them. And that applies to your customers too. Things get really bad without DB knowledge.

The DB does not have to become a bottleneck if you use database replication. Just spawn more nodes if you need more capacity for queries.

---

<div class="post-metadata">

**Author:** ![Pushkar\_Deole](https://avatars.discourse-cdn.com/v4/letter/p/bb73d2/32.png) [@Pushkar\_Deole](https://discourse.hibernate.org/u/Pushkar_Deole)\
**Post date:** [September 4, 2018, 5:17am UTC](https://discourse.hibernate.org/t/hibernate-query-cache-invalidation-after-persisting-entity/1334/7 "2018-09-04T05:17:54Z")

</div>

_The DB does not have to become a bottleneck if you use database replication. Just spawn more nodes if you need more capacity for queries_

How can I do a load balancing between multiple db nodes? Does hibernate have a support for that?

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [September 4, 2018, 5:54am UTC](https://discourse.hibernate.org/t/hibernate-query-cache-invalidation-after-persisting-entity/1334/8 "2018-09-04T05:54:43Z")

</div>

Hibernate does not need to support that. This can be done in multiple ways:

- at the JDBC Driver level
- at the DataSource level
- using Spring `AbstractRoutingDataSource`

---

<div class="post-metadata">

**Author:** ![Pushkar\_Deole](https://avatars.discourse-cdn.com/v4/letter/p/bb73d2/32.png) [@Pushkar\_Deole](https://discourse.hibernate.org/u/Pushkar_Deole)\
**Post date:** [September 4, 2018, 7:09am UTC](https://discourse.hibernate.org/t/hibernate-query-cache-invalidation-after-persisting-entity/1334/9 "2018-09-04T07:09:51Z")

</div>

Thanks @vlad. Appreciate it. Will look at the Spring framework documentation for AbstractRoutingDataSource.

However, i would also like to look at the other 2 options: would you know any link to documents etc. that would provide details on these two approaches for balance requests between db nodes?

_\* at the JDBC Driver level_  
_\* at the DataSource level_

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [September 4, 2018, 7:34am UTC](https://discourse.hibernate.org/t/hibernate-query-cache-invalidation-after-persisting-entity/1334/10 "2018-09-04T07:34:52Z")

</div>

You are welcome. You can find those links via Google.
