Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does HQL update clear 2nd level cache?

Tags:

java

hibernate

Native queries are clearing the 2nd level cache entries. An answer from the hibernate forum that is 7 years old says that HQL update queries also clear the 2nd level cache. But is this still true?

Since the HQL query has the exact fields to be updated, and in which entity, I think it shouldn't be that hard to behave as if a regular session.save(..) is invoked.

like image 697
Bozho Avatar asked May 23 '11 09:05

Bozho


People also ask

How does second level cache works in Hibernate?

This cache only works at a session level, meaning each session object caches data independently, so there is no sharing of cached data across sessions, and the cached data is deleted when the session closes. This makes the cache only useful for repeated queries in the same session.

Which 2nd level cache is better in Hibernate?

Hibernate second level cache uses a common cache for all the session object of a session factory. It is useful if you have multiple session objects from a session factory. SessionFactory holds the second level cache data. It is global for all the session objects and not enabled by default.

Is second level caching mandatory in Hibernate?

You can extend this caching with an optional second-level cache. The first-level keeps being mandatory and is consulted first always. The second-level cache is used to cache object across sessions.

What is secondary level cache?

A second-level cache is a local store of entity data managed by the persistence provider to improve application performance. A second-level cache helps improve performance by avoiding expensive database calls, keeping the entity data local to the application.


2 Answers

Native queries are clearing the 2nd level cache entries.

  1. Native queries (actually this is only for native inserts/deletes/updates, not queries) are invalidating the 2nd level cache entries for all caches (WHOLE CACHE). I have verified this with the current stable version of Hibernate: 4.1.9. This is sensible because Hiberante can't possibly know what was changed in the DB, so the only option is to invalidate whole 2nd level cache. This can be a serious issue in some systems which relies heavily on the 2nd level cache.

There is though a possibility to specify what from 2nd level cache should be invalidated (or even specify that nothing is evicted from cache). Look at great blog post http://www.link-intersystems.com/bin/view/Blog/Hibernate%27s+second+level+cache+and+native+queries where this is explained thoroughly.

To prevent Hibernate from invalidating anything from cache:

SQLQuery sqlQuery = session.createSQLQuery("ALTER SESSION SET NLS_COMP = 'BINARY'");
sqlQuery.addSynchronizedQuerySpace("");  
int updatedEntities = sqlQuery.executeUpdate();

To instruct Hibernate to invalidate only the Person entity cache:

SQLQuery sqlQuery = session.createSQLQuery("UPDATE PERSON SET ... WHERE ...");
sqlQuery.addSynchronizedEntityClass(Person.class);
int updatedEntities = sqlQuery.executeUpdate();

An answer from the hibernate forum that is 7 years old says that HQL update queries also clear the 2nd level cache. But is this still true?

  1. HQL does invalidate only a the 2nd level cache region which is related to the entity you are doing some inserts/updates/deletes on. That makes sense because Hibernate knows which entities are affected by the HQL, it can clear only the 2nd level cache region for this entity.

Example:

entityManager.createQuery("delete from Person p where p.id = 1").executeUpdate();

This will invalidate "only" the Person entity cache.

I am not aware of any possibility to prevent Hibernate from invalidating 2nd level cache for specific entity when it comes to HQL.

like image 174
Marek Dominiak Avatar answered Oct 25 '22 06:10

Marek Dominiak


We did see HQL update clearing the 2nd level cache when running with Hibernate 3.2.x

As a simple way to validate for your individual setup, implement something like:

http://narcanti.keyboardsamurais.de/hibernate-statistics-jsp-reloaded.html

Note details on that page before and after running the HQL update transaction ...

Or gather stats directly in your code and/or using JMX

http://docs.jboss.org/hibernate/core/3.5/reference/en/html/performance.html#performance-monitoring

As for improved behaviour, the Hibernate project might be open to implement a patch :)

like image 24
gliptak Avatar answered Oct 25 '22 06:10

gliptak