views:

103

answers:

3

Given all the smarts around Hibernate and it's various caching strategies, how do I know if a certain operation is resulting in a physical database hit, or coming from the cache?

+4  A: 

Traditionally, you'll use Hibernate built-in cache logging and do some profiling during development to tune the whole thing until you get the expected behavior. To activate the cache logging, alter the log level, for example when using a log4j.xml:

<logger name="org.hibernate.cache">
   <level value="DEBUG" />
</logger>

Note that I do not recommend using this on the production platform (it may be very verbose and thus may slow down the execution).

For more details, refer to the Logging of the documentation.

Pascal Thivent
Also: when the log says that it missed the cache and went to the database.
Travis Gockel
@Travis Absolutely. I'm just unsure of what information the OP is trying to get.
Pascal Thivent
Great - thanks Pascal. More at a general level, trying to get an understanding of what is happening under the covers.
Marty Pitt
@Marty Logging should help then (logging SQL queries by turning show_sql on would be also a good idea too IMO). Good luck!
Pascal Thivent
A: 

I'm not really sure, but I'd try turning up the logging level of the org.hibernate loggers and see if that information is logged (it's most likely at the "debug" or lower level).

Jack Leow
+1  A: 

Hibernate Statistics should tell you all you need to know:

Enabling: Hibernate Core Reference

Using: Hibernate: Use Hibernate Statistics When Optimizing Queries

API: org.hibernate.stat.Statistics

davidemm