Announcement Announcement Module
Collapse
No announcement yet.
spring + hibernate + ehcache 2nd level cache? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:
    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>

  • #2
    Take a look at the EhCache documentation from the home page (http://ehcache.sourceforge.net/documentation/). 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.

    Comment

    Working...
    X