Announcement Announcement Module
No announcement yet.
spring + hibernate + ehcache 2nd level cache? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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:
    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
     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
    result row: EntityKey[com.mytest.model.CityZip#66]
    Initializing object from ResultSet: [com.mytest.model.CityZip#66]
    Hydrating entity: [com.mytest.model.CityZip#66]
    total objects hydrated: 19
    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:
    List tmp = getHibernateTemplate().find(query, city);
    ehcache xml file has:
        <cache name="com.mytest.model.CityZip"
    hibernate xml file has:
    	<class name="com.mytest.model.CityZipZip" table="cityzip">
    		<cache usage="read-only"/>
    		<id name="id" column="cityzip_id">
    			<generator class="identity"/>
    		<property name="zipcode" column="zipcode"/>
    		<property name="city" column="city"/>

  • #2
    Take a look at the EhCache documentation from the home page ( EhCache is the default cache provider for Hibernate. There are also some entries inside the reference documentation of Hibernate. Note that Spring can't actually help you much with caching - it's yours and HB job to actually hit the cache.
    Note that for effective cache use you need to use identities which is not what your are doing with find (think of session.load). However you can define caching for queries but for that you have to check the documentation of HB and EhCache but note that no matter what approach you take, caching is a LOT more effective with identities than queries.