The problem is determining whether the second level cache is in sleep mode

I am trying to use ehcache in my project .. I have specified the following properties in the hibernate configuration file -

config.setProperty ("hibernate.cache.provider_class", "org.hibernate.cache.EhCacheProvider"); config.setProperty ("hibernate.cache.provider_configuration_file_resource_path", "ehcache.xml"); config.setProperty ("hibernate.cache.use_second_level_cache", "true"); config.setProperty ("hibernate.cache.use_query_cache", "true");

Now I'm still not sure if the results are coming from the database or cache.

I looked around and discovered - the second level Hibernate cache is the print result where the person offers the HitCount / Misscount API

However, when I tried to use it, the hitcount and miss counter always returns 0 ... here is my code

String rName = "org.hibernate.cache.UpdateTimestampsCache"; Statistics stat = HibernateHelper.getInstance () getFactory () getStatistics () ..; long oldMissCount = stat.getSecondLevelCacheStatistics (RNAME) .getMissCount (); long oldHitCount = stat.getSecondLevelCacheStatistics (RNAME) .getHitCount (); UserDAO user = new UserDAO (); user.read (new long (1)); long newMissCount = stat.getSecondLevelCacheStatistics (RNAME) .getMissCount (); long newHitCount = stat.getSecondLevelCacheStatistics (RNAME) .getHitCount ();

if (oldHitCount + 1 == newHitCount && & & & oldMissCount + 1 == newMissCount) {System.out.println ("came from the database"); } else if (oldHitCount + 1 == newHitCount & & oldMissCount == newMissCount) {
System.out.println ("came from cache"); }

Please let me know if I use it incorrectly .. and what should be rName (region Name) in this case.

Is there any other way to determine if second level cache is working?

thanks

+4
source share
1 answer

You need to enable statistics collection:

config.setProperty("hibernate.generate_statistics", "true"); 
+2
source

Source: https://habr.com/ru/post/1333969/


All Articles