spring + hibernate + ehcache 2nd level cache?
I'm using hibernate 3.0.4 and spring 1.2.1. I'm doing a very simple proof of concept to learn hibernate. I have a table consisting of 3 columns: id, city and zipcode. the query is given the city, i want the zipcodes. I want to use the 2nd level cache to prevent a db hit for each request. everything seems to be working except the caching.
For a local db with spring, hibernate + ehcache:
1st request: ~700ms, 2nd request 40ms, 3rd request 30ms, 4th request 180ms.
Using spring's jdbc functionality, i get repsonse times along the lines of 1: ~700ms, 2: 0ms, 3: 0ms....
jdbc is working much faster. i would think if the cache was being accessed then i would have similar performance.
what are the requirements that need to be fulfilled to utilize the 2nd level cache?
here are some excerpts from my log & config files:
Code:
Second-level cache: enabled
Query cache: disabled
Cache provider: org.hibernate.cache.EhCacheProvider
Optimize cache for minimal puts: disabled
Structured second-level cache entries: disabled
<snip>
com.mytest.model.CityZip Cache: Using SpoolingLinkedHashMap implementation
initialized MemoryStore for com.mytest.model.CityZip
Initialised cache: com.mytest.model.CityZip
instantiating cache region: com.mytest.model.CityZip usage strategy: read-only
read-only cache configured for mutable class: com.mytest.model.CityZip
<snip>
result row: EntityKey[com.mytest.model.CityZip#66]
Initializing object from ResultSet: [com.mytest.model.CityZip#66]
Hydrating entity: [com.mytest.model.CityZip#66]
<snip>
total objects hydrated: 19
<snip>
resolving associations for [com.mytest.model.CityZip#35]
adding entity to second-level cache: [com.mytest.model.CityZip#35]
Caching: com.mytest.model.CityZip#35
done materializing entity [com.mytest.model.CityZip#35]
not sure what i am missing. i would think there would be some message stating it was accessing the cache.
my class com.mytest.model.CityZip has the equals and hashcode methods overridden
inside my dao which extends HibernateDaoSupport:
Code:
getHibernateTemplate().setCacheQueries(true);
List tmp = getHibernateTemplate().find(query, city);
ehcache xml file has:
Code:
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
<cache name="com.mytest.model.CityZip"
maxElementsInMemory="80"
eternal="true"
overflowToDisk="false"
/>
hibernate xml file has:
Code:
<class name="com.mytest.model.CityZipZip" table="cityzip">
<cache usage="read-only"/>
<id name="id" column="cityzip_id">
<generator class="identity"/>
</id>
<property name="zipcode" column="zipcode"/>
<property name="city" column="city"/>
</class>