How to determine the number of 2nd level cache entries per region?

Is there a way to determine the number of 2nd level cache entries for a given region?

Since Hibernate isn’t caching itself I don’t think there’s a way to obtain tier statistik. But Hibernate is managing it’s own statistik data, so maybe we can manage somehow to determine the number of entries?

Many thanks for any information

In Hibernate, you can obtain statistics related to the second-level cache, including the number of entries for a given region, using the Hibernate Statistics API. This API provides a way to access and manipulate various statistical data collected by Hibernate, which includes details about the second-level cache.

To achieve this, you need to enable the collection of statistics and then use the Statistics and SecondLevelCacheStatistics classes provided by Hibernate. Here’s how you can do it:

Enable Statistics Collection:
You need to enable statistics collection in your Hibernate configuration. This can be done in your hibernate.cfg.xml file or through your Java configuration.

XML Configuration:

<property name="hibernate.generate_statistics">true</property>

Java Configuration:

configuration.setProperty("hibernate.generate_statistics", "true");

Accessing the Statistics API:

You can access the Statistics object from the SessionFactory. From this, you can retrieve the SecondLevelCacheStatistics for a specific cache region.

Here’s an example of how to do this:

import org.hibernate.SessionFactory;
import org.hibernate.stat.SecondLevelCacheStatistics;
import org.hibernate.stat.Statistics;

public class CacheStatisticsUtil {

    private SessionFactory sessionFactory;

    public CacheStatisticsUtil(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public long getCacheEntriesCount(String regionName) {
        Statistics statistics = sessionFactory.getStatistics();
        SecondLevelCacheStatistics cacheStats = statistics.getSecondLevelCacheStatistics(regionName);
        if (cacheStats != null) {
            return cacheStats.getElementCountInMemory();
        }
        return 0;
    }

    public static void main(String[] args) {
        // Assuming you have a configured SessionFactory instance
        SessionFactory sessionFactory = ...;

        CacheStatisticsUtil cacheUtil = new CacheStatisticsUtil(sessionFactory);
        String regionName = "yourCacheRegionName";  // replace with your actual cache region name
        long entriesCount = cacheUtil.getCacheEntriesCount(regionName);

        System.out.println("Number of cache entries in region '" + regionName + "': " + entriesCount);
    }
}

In this example:

  • SessionFactory: The Hibernate SessionFactory is assumed to be correctly configured and instantiated.
  • Statistics: The Statistics object is obtained from the SessionFactory.
  • SecondLevelCacheStatistics: For the given cache region name, the SecondLevelCacheStatistics object is retrieved.
  • Element Count: The method getElementCountInMemory() returns the number of cache entries in memory for the specified region.

Notes:

  • Ensure that statistics are enabled in your Hibernate configuration, as they are disabled by default for performance reasons.

  • The region name (yourCacheRegionName) should match the name you have configured for your cache regions in Hibernate.

By using the Statistics and SecondLevelCacheStatistics classes, you can monitor and retrieve various metrics related to the second-level cache, including the number of entries, hit/miss ratios, and more. This approach allows you to programmatically access and log cache statistics as needed.