views:

64

answers:

3

Hi,

During performing some performance tuning in my application I have noticed, that hibernate query cache is never used.

I am quite sure I have it properly set up:

  • hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
  • hibernate.cache.use_query_cache=true
  • I use setCacheable(true) on queries I want to cache

I have set up a simple stress test, when I perform the same set of operations over and over by several threads. When I check hibernate statistics it turns out that query cache hitCount is zero!

What am I missing?

EDIT:
For all of you asking: I have set hibernate.generate_statistics = true

A: 

Hibernate does not gather statistics by default. Have you enabled statistics?

  • hibernate.generate_statistics = true
Thierry-Dimitri Roy
Yes, I have this enabled.
Ula Krukar
+1  A: 

I have set up a simple stress test, when I perform the same set of operations over and over by several threads. When I check hibernate statistics it turns out that query cache hitCount is zero!

Well, while it is possible to get statistics about cache hits from statistics (if you enable statistics by setting the property hibernate.generate_statistics=true in your configuration), this is IMO not the best way to diagnose caching "issues".

My suggestion would be to activate the logging of 1. the DML statements that are executed and of 2. the cache activity. The relevant logging categories are:

  • org.hibernate.SQL Log all SQL DML statements as they are executed
  • org.hibernate.cache Log all second-level cache activity

This is IMO more useful than statistics to fine tune the behavior during development.

References

Pascal Thivent
Thanks Pascal. I analyzed the logs, turned out much of the queries I presumed to be cached, were not. Also a caught a couple of simple mistakes like using new Date() as parameter value. It works way better now.
Ula Krukar
@Ula You're welcome. Glad it helped to solve the problem.
Pascal Thivent
A: 

Though you do not mention it, you have also set hibernate.cache.use_second_level_cache=true|false?

extraneon
I this necessary? I though hibernate.cache.provider_class set is enough.
Ula Krukar