News »Browse Articles »
Understanding caching in hibernate - part two : The query cache
+2
Understanding caching in hibernate - part two : The query cache
As we have seen in the last post the session cache can help in caching values when we have an _EntityKey_ available. If we do not have the key, we ran into the problems of having to issue multiple queries for retrieving the same object. This was the reason why the session cache worked fine for the _load_ method but not when we used _session.createQuery()_.
Now this is the point where the query cache comes into play. The query cache is responsible for caching the results of queries - or to be more precise the keys of the objects returned by queries. Let us have a look how Hibernate uses the query cache to retrieve objects. In order to make use of the query cache we have to modify the person loading example as follows.
Session session = getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Person p where p.id=1");
query.setCacheable(true);
Iterator it = query.list().iterator();
while (it.hasNext ()){
Person p = (Person) it.next();
System.out.println(p.getFirstName());
}
query = session.createQuery("from Person p where p.id=1");
query.setCacheable(true);
it = query.list().iterator();
while (it.hasNext ()){
Person p = (Person) it.next();
System.out.println(p.getFirstName());
}
tx.commit();
session.close();
As highlighted in bold face we had to add a line for defining that the query is actually cachable. If we would not do this, it won’t be cached. (Note: The while loops could be omitted here. I am using for other examples where we have multiple results. …. just for code esthetics lovers). Additionally we also have change the hibernate configuration to enable the query cache. This is done by adding the following line to the Hibernate configuration.
true
Unlike most examples I found on the web I will not immediately enable the second-level cache. As the basic working do not depend on it and I do not want to create the impression that the query cache requires the second level or vice versa. Let us now verify that everything is working correctly. As we can see below only the first _query.list()_ result in a SQL statement to be issued.
The question now is, what happens internally. Therefor we analyze what happens within the second _get_ method of the _StandardQueryCache_. As we can see in the image below Hibernate first tries to retrieve the key values from the cache (as we can see the query cache internally uses the _EhCache_). After retrieving the keys the person entity is loaded from the session cache.
Query Cache Pitfalls
The query cache can be really usefull to optimize the performance of your data access layer. However there are a number of pitfalls as well. This blog post describes a serious problem regarding memory consumption of the Hibernate query cache when using objects as parameters.
Conclusion
We have learned that the query cache helps us to cache the keys of results of Hibernate queries. These keys are then uses to retrieve data objects using the Hibernate Internal loading behavior which involves the session cache and potentially also the second-level cache.
Source:
http://blog.dynatrace.com/2009/02/16/understanding-caching-in-hibernate-part-two
Search News
News Categories
What's the News?
Post a link to something interesting from another site, or submit your own original writing for the Java community to read.
Most Popular News
-
How to stand out from other Java/JEE Professionals?
Published about 14-01-2009 | Rated +3 -
10 reasons IT certification will be important in 2009
Published about 05-01-2009 | Rated +2 -
The 9 hottest skills for `09
Published about 02-01-2009 | Rated +1 -
New Features in Servlets 3.0
Published about 05-01-2009 | Rated +4
Most Recent User Submitted News
- Passing Parameters In A Data Table Using JSF
Published about 11-07-2009 | Rated 0 - JAVA Openings In Hyderabad @ Reliance Global Services Pvt.Ltd
Submitted by Bharath | Rated 0 - Domain-driven design with Java EE 6
Published about 22-05-2009 | Rated +1 - Introduction to J2ME
Published about 21-09-2009 | Rated 0







