Hi,
I am trying to get the ehcache to work in my app, but somehow it never hits the cache, but instead it keeps on sending the same query to the MySQL database. Probably I am misunderstanding something. This is how I fetch rows from the table:
Code:
Client client = (Client) _session.createSQLQuery("SELECT * FROM client WHERE name = :name")
.addEntity(Client.class)
.setString("name", credsDTO.clientName)
.setCacheable(true)
.setMaxResults(1)
.uniqueResult();
The Client class has a read-only cache configured. When I investigate the statistics for the respective cache I can see that rows are correctly added to the cache, but they are never being used when the same query is executed again!
This is how I get the stats:
Code:
Statistics stats = HibernateSessionFactory.getInstance().getStatistics();
SecondLevelCacheStatistics cacheStats = stats.getSecondLevelCacheStatistics(Client.class.getCanonicalName());
System.out.println("Hit count1: " + stats.getSecondLevelCacheHitCount());
System.out.println("Miss count1: " + stats.getSecondLevelCacheMissCount());
System.out.println("Put count1: " + stats.getSecondLevelCachePutCount());
System.out.println("Hit count2: " + cacheStats.getHitCount());
System.out.println("Miss count2: " + cacheStats.getMissCount());
System.out.println("Put count2: " + cacheStats.getPutCount());
System.out.println("Entries: " + cacheStats.getEntries().toString());
for (int i=0; i<stats.getSecondLevelCacheRegionNames().length; ++i) {
System.out.println("Region: " + stats.getSecondLevelCacheRegionNames()[i]);
}
And this is the output:
Code:
Hit count1: 0
Miss count1: 0
Put count1: 5
Hit count2: 0
Miss count2: 0
Put count2: 5
Entries: {1={users=1, _version=null, fullName=XXX, _lazyPropertiesUnfetched=true, name=XXX, _subclass=mysql.contentional.generated.Client}, 4={users=4, _version=null, fullName=YYY, _lazyPropertiesUnfetched=true, name=YYY, _subclass=mysql.contentional.generated.Client}}
Region: org.hibernate.cache.StandardQueryCache
Region: org.hibernate.cache.UpdateTimestampsCache
Region: mysql.contentional.generated.Client
So why do I keep seeing the same query being sent to MySQL and the hit counts are always on 0, even though exactly the same query is executed over and over again?
I think I tries all options and all combinations, so I guess I am misunderstanding something. Anyway, here are the options I am using:
Code:
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.cache.use_structured_entries">true</property>
Thank you,
Andrej