Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know/log whether Hibernate 2nd-level cache was used or not for a query?

Currently, in order to check if the Hibernate 2nd-level cache was used for a Database query, I check my SQL log (through p6spy or logSql=true in Datasource.groovy) to see if the Grais/Hibernate triggered an SQL query. And I assume that if the query was not logged, it might mean that cache was used.

It is pretty complicated for a simple information, isn't it?

So do you know a simple way to get and log the information : "Cache was used vs. DB query was triggered" ?

EDIT: Following Pascal recommendations, I have added this trace 'org.hibernate.cache' to my log4j configuration.

like image 534
fabien7474 Avatar asked Feb 28 '10 12:02

fabien7474


2 Answers

You could activate the org.hibernate.cache category to log all second-level cache activity. To do so (according to the Grails FAQ), edit your Config.groovy file. Find the line with:

hibernate = "off"

and replace it with:

hibernate.cache = "trace,stdout"
like image 149
Pascal Thivent Avatar answered Sep 21 '22 09:09

Pascal Thivent


The short answer is that when query logging is enabled, each query is logged.

Various statistics are available via SessionFactory.getStatistics() and Session.getStatistics(). Query execution and query cache hit and miss counts are not available on the SessionStatistics.

In a test environment, where you do not have concurrent sessions, you could perform your cacheable query twice and assert that both of SessionStatistics.getQueryCacheHitCount() and SessionStatistics.getQueryExecutionCount() only increased by 1.

The Hibernate Profiler presents all of these statistics and logs most effectively.

like image 44
Lachlan Roche Avatar answered Sep 22 '22 09:09

Lachlan Roche