使用Hazelcast作为Hibernate二级缓存时,如何查看查询缓存统计?
Hey there! I’ve worked with this exact stack before, so let’s walk through the best ways to get those query cache hit stats you’re missing.
1. Enable Hibernate’s Core Statistics First
Before anything else, you need to make sure Hibernate’s own statistics engine is turned on—this is the foundation for tracking cache activity. Add these configs to your hibernate.cfg.xml or persistence.xml:
<property name="hibernate.generate_statistics">true</property> <property name="hibernate.cache.use_structured_entries">true</property>
hibernate.generate_statistics: Enables Hibernate’s internal stats collection for caches, queries, and more.hibernate.cache.use_structured_entries: Makes cache keys human-readable in Hazelcast, which helps when inspecting cache entries later.
2. Use Hibernate’s Statistics API for Precise Metrics
Hibernate’s built-in Statistics API gives you the most direct way to query query cache hits, misses, and puts. Here’s how to use it:
Example Code Snippet
// Get the SessionFactory (adjust based on your setup—Spring, JPA, etc.) SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class); Statistics stats = sessionFactory.getStatistics(); // Get stats for the default query cache (or your custom cache region) QueryCacheStatistics queryCacheStats = stats.getQueryCacheStatistics("org.hibernate.cache.internal.StandardQueryCache"); // Print key metrics System.out.println("Query Cache Hits: " + queryCacheStats.getHitCount()); System.out.println("Query Cache Misses: " + queryCacheStats.getMissCount()); System.out.println("Query Cache Puts: " + queryCacheStats.getPutCount());
If you’re using custom query cache regions (via @Cacheable with a specific region name), replace the "org.hibernate.cache.internal.StandardQueryCache" string with your region’s name.
3. Configure Hazelcast Management Center to Show Query Cache Stats
Your existing Management Center can display query cache stats, but you need to explicitly enable statistics for the Hibernate query cache map. Update your hazelcast.xml config:
<!-- Enable global Hazelcast statistics --> <management-center enabled="true" update-interval="3">http://your-mancenter-url:8080/hazelcast-mancenter</management-center> <!-- Enable stats for the default query cache map --> <map name="org.hibernate.cache.internal.StandardQueryCache"> <statistics-enabled>true</statistics-enabled> </map> <!-- Add this if you use custom query cache regions --> <map name="your-custom-query-cache-region"> <statistics-enabled>true</statistics-enabled> </map>
After restarting your cluster, log into the Management Center:
- Navigate to the Maps section
- Find the map named
org.hibernate.cache.internal.StandardQueryCache(or your custom region) - Check the Statistics tab—you’ll see hits, misses, evictions, and other cache metrics here.
4. Use JMX for Real-Time Monitoring
Both Hibernate and Hazelcast expose JMX MBeans that you can inspect with tools like JConsole or VisualVM:
- Hibernate’s Stats MBean: Look under the
org.hibernate.statdomain. TheStatisticsMBean has attributes for query cache hits/misses, or you can call operations likegetQueryCacheStatistics(). - Hazelcast’s Map MBean: Under the
com.hazelcast.mapdomain, find the MBean corresponding to your query cache map (e.g.,org.hibernate.cache.internal.StandardQueryCache). It will show real-time hit/miss counts and other cache performance metrics.
Quick Notes
- If you’re using named queries with specific cache regions, make sure those regions are configured with
statistics-enabled="true"in Hazelcast. - Double-check that your Hibernate and Hazelcast versions are compatible (5.2.12.Final and 3.12 work well together, so you’re good here).
内容的提问来源于stack exchange,提问作者mpk

