I'm trying to make use of ehcache with hibernate. According to SecondLevelCacheStatistics the cache is only populated and hibernate never tries to read anything from the cache. According to my novice eyes, the configuration and usage of hibernate/ehcache looks correct, so I need some more experienced eyes to tell me what may be wrong.
Edit: I'm using hibernate 3.2.0.cr4 and ehcache 1.2 (and spring 2.0 and Oracle 10g)
the test-POJO:
Code:
package my.test;
public class Dummy {
private String name;
private String description;
private Long id;
public Dummy() {}
// vanilla getter/setter methods removed for clarity
}
hibernate-mapping:
Code:
<class name="my.test.Dummy">
<cache usage="read-write"/>
<id name="id" type="long"/>
<property name="description" type="string"/>
<property name="name" type="string"/>
</class>
ehcache.xml (which is located in my classpath):
Code:
<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="5000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="50000"
timeToLiveSeconds="50000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="50000"
/>
<cache name="my.test.Dummy"
maxElementsInMemory="10"
eternal="true"
overflowToDisk="false"
/>
</ehcache>
relevant properties for the hibernate session (which is configured by spring):
Code:
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=false
(As you see, I've turned query-cache off.)
testclass/method:
Code:
public void testDummyCache() {
System.out.println("cacheStats = " + sessionFactory.getStatistics().getSecondLevelCacheStatistics("my.test.Dummy").toString());
for (int i = 1; i < 4; i++) {
Session session = sessionFactory.openSession();
session.setCacheMode(CacheMode.NORMAL);
Transaction tx = session.beginTransaction();
now = System.currentTimeMillis();
Collection dummies = session.createQuery("from Dummy as d order by d.name").list();
System.out.print("time run " + i + ": " + (System.currentTimeMillis() - now));
System.out.println(", cacheStats = " + sessionFactory.getStatistics().getSecondLevelCacheStatistics("my.test.Dummy").toString());
System.out.println("trying to load: " + session.load(Dummy.class, new Long(i)));
tx.commit();
session.close();
assertNotNull(dummies);
assertEquals(3, dummies.size());
}
System.out.println("final cacheStats = " + sessionFactory.getStatistics().getSecondLevelCacheStatistics("my.test.Dummy").toString());
}
result/output when running the testclass:
Code:
cacheStats = SecondLevelCacheStatistics[hitCount=0,missCount=0,putCount=0,elementCountInMemory=0,elementCountOnDisk=0,sizeInMemory=0]
time run 1: 16, cacheStats = SecondLevelCacheStatistics[hitCount=0,missCount=0,putCount=0,elementCountInMemory=3,elementCountOnDisk=0,sizeInMemory=3399]
trying to load: my.test.Dummy@1bb3a11
time run 2: 0, cacheStats = SecondLevelCacheStatistics[hitCount=0,missCount=0,putCount=0,elementCountInMemory=3,elementCountOnDisk=0,sizeInMemory=3399]
trying to load: my.test.Dummy@facfa5
time run 3: 0, cacheStats = SecondLevelCacheStatistics[hitCount=0,missCount=0,putCount=0,elementCountInMemory=3,elementCountOnDisk=0,sizeInMemory=3399]
trying to load: my.test.Dummy@4ee543
final cacheStats = SecondLevelCacheStatistics[hitCount=0,missCount=0,putCount=0,elementCountInMemory=3,elementCountOnDisk=0,sizeInMemory=3399]
As you see in the output, the SecondLevelCacheStatistics reports that the 3 elements located in the "DUMMY" Oracle table are stored in the second level cache. However, it seems that Hibernate always accesses the DB directly and never attempts to read anything from the cache. (hitCount=0, missCount=0). How do I make Hibernate try to look in the cache?