Hibernate Version: 3.2.6.ga
Update respective files as follows.
ClassName1.hbm.xml
<cache usage="read-write" region="com.abc.ClassName1"/>
<query name="query1" cacheable="true">
sql query ...
</query>
asdf-Hibernate.cfg.xml
<property name="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</property>
<property name="hibernate.cache.provider_configuration_file_resource_path">
/META-INF/hibernate/abcd-ehcache.xml
</property>
<property name="cache.use_query_cache">true</property>
<property name="cache.use_second_level_cache">true</property>
asdf-ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<diskStore path="java.io.tmpdir" />
<cacheManagerEventListenerFactory class="" properties="" />
<!-- Default cache region settings -->
<defaultCache maxElementsInMemory="200" eternal="false"
timeToIdleSeconds="3000" timeToLiveSeconds="40000"
overflowToDisk="false" maxElementsOnDisk="0"
memoryStoreEvictionPolicy="LRU" />
<!-- If we didn't enable this, default settings will be applied.
<cache name="com.abc.ClassName1"
eternal="false" maxElementsInMemory="100" overflowToDisk="false"
timeToLiveSeconds="40000" timeToIdleSeconds="3000" />
-->
</ehcache>
Test Case 1:
To test Query Level Cache use for loop and query using named query.
In log you should see
DEBUG [main] (StandardQueryCache.java:117) – returning cached query results
Test Case 2:
To test Second Level Cache
Use for loop and get object using primary key
Verification Step 1:
Debug window Expression
net.sf.ehcache.CacheManager.ALL_CACHE_MANAGERS
If you open ehcache node, you should see all objects.
During debug, hitcount should be increased.
Verification Step 2:
<logger name="org.hibernate.cache">
<level value="DEBUG" />
</logger>
<logger name="org.hibernate.SQL">
<level value="DEBUG" />
</logger>
You should see clear logs in console. Telling that after first hit onwards it is returning from cache.
DEBUG [main] (ReadWriteCache.java:85) – Cache hit: com.abc.ClassName1#1
DEBUG [main] (ReadWriteCache.java:75) – Cache lookup: com.abc.ClassName1#1
DEBUG [main] (EhCache.java:68) – key: com.abc.ClassName1#1
DEBUG [main] (MemoryStore.java:135) – com.abc.ClassName1Cache: com.abc.ClassName1MemoryStore hit for com.abc.ClassName1#1
DEBUG [main] (ReadWriteCache.java:85) – Cache hit: com.abc.ClassName1#1
Links to study:
http://ehcache.sourceforge.net/documentation/configuration.html
http://ehcache.sourceforge.net/documentation/hibernate.html
http://www.hibernate.org/hib_docs/reference/en/html_single/#performance-cache
Story:
It worked few times and didn’t work few times. Struggled to resolve this.
Reason: Temp files are locked in windows explorer. Close Eclipse. Clear all temp files in temp folder. After fresh restart, it worked fine for me.
Tips:
If system memory is limited and if you define 10000 records to be cached, it will cause slow performance.
Need to be careful with read-only and read-write cache attributes
Define realistic timeToLiveSeconds. Dont give infinite numbers
Read more on each attribute and think on it.
This configuration didn’t worked out
<prop key="cache.provider_class">
net.sf.ehcache.hibernate.EhCacheProvider
</prop>
<prop key="net.sf.ehcache.configurationResourceName">
/META-INF/hibernate/asdf-ehcache.xml
</prop>
<prop key="cache.use_query_cache">true</prop>
<prop key="cache.use_second_level_cache">true</prop>