Announcement Announcement Module
Collapse
No announcement yet.
Spring - Hibernate - EHCache - Web App Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring - Hibernate - EHCache - Web App

    Hello All,

    I am trying to enable EHCahce for my spring-hibernate app running in a web applicaiton. I am using Spring 1.1.3 and Hibernate 2.1.

    It appears that the cache is not intercepting and caching my database calls as there seems to be no reduction in the SQL displayed to the console.

    What are the steps to enable the cache with spring?

    Here is what I have done so far....

    added the below to my hibernate context file inside the sessionFactory configuration.

    Code:
    <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
            <property name="dataSource"><ref bean="topupDataSource"/></property>
            <property name="mappingResources">
                <list>
                    <value>com/macalla/topup/model/Order.hbm.xml</value>
                    ... left out for space reasons
                    <value>com/macalla/topup/model/VCustomerSearch.hbm.xml</value>
                </list>
            </property>
            <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">net.sf.hibernate.dialect.OracleDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.use_outer_join">false</prop>
                <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.Provider</prop>
                <prop key="hibernate.hibernate.cache.use_query_cache">true</prop>
            </props>
            </property>
        </bean>
    Created an EHCache.xml file ...

    Code:
    <ehcache>
    
        <diskStore path="java.io.tmpdir"/>
    
        <!-- ehcache requires a default cache -->
        <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            />
    
        <!-- The SystemValue Cache&#58; cached for 2 hrs &#40;7200 secs&#41; -->
        <cache name="com.macalla.topup.model.SystemValue"
            maxElementsInMemory="100"
            eternal="false"
            timeToIdleSeconds="7200"
            timeToLiveSeconds="7200"
            overflowToDisk="false"
            />
    
        <!-- The TransPaymentState Cache&#58; cached for infinity -->
        <cache name="com.macalla.topup.model.TransPaymentState"
            maxElementsInMemory="100"
            eternal="false"
            timeToIdleSeconds="0"
            timeToLiveSeconds="0"
            overflowToDisk="false"
            />
    
    
        <!-- Standard Qurey Cache -->
        <cache name="net.sf.hibernate.cache.StandardQueryCache"
            maxElementsInMemory="5"
            eternal="false"
            timeToLiveSeconds="120"
            overflowToDisk="false"/>
    
    ETC ETC ETC 
    
    </ehcache>
    I have also added the cache element to the mapping files. I am aware jcs is deprecated, but im using XDoclaet tags to generate stuff so this is a segment from my mapping file. I am sure that ehcache still supports this tag anyhow.

    Code:
    <jcs-cache usage="read-write" />
    Any other steps I need to do?? Do I need to create an spring managed instance of the cache manager and inject it to some component?

    I am aware of the change in the providor class for EHCache in hibernate as of version 2.1.8, but I think my version of Hibernate is pre-2.1.8, and at any rate I get an instantiation error if I try to use the new class.

    Any help appreciated. Thanks

    Colm

  • #2
    I wrote a Junit and it appears the cache is working, although not as well as I would have like, the posted configuration seems to be all that is needed, so this thread can be closed.

    Comment


    • #3
      Could you please show me an example of the cache configuration generation using xdoclet?

      Comment


      • #4
        This seems to work fine...

        Code:
        /**
         *        
         *
         *        @hibernate.class
         *         table="MCS_USERS_PRODUCTS"
         *      @hibernate.jcs-cache usage="read-write"
         *
        */
        and for method level cahce...

        Code:
        /** 
             *            @hibernate.set
             *             lazy="true"
             *             inverse="true"
             *             cascade="none"
             *            @hibernate.collection-key
             *             column="SERVICEID"
             *            @hibernate.collection-one-to-many
             *             class="com.macalla.topup.model.Product"
             *            @hibernate.jcs-cache usage="read-write"
             *         
             */
        Thats it, simply run x-doclet and it should add a <jcs-cache> element to your hbm files. <jcs-cache> is deprecated, but still works with ehcache. x-doclet does not support the new cache tag.

        Comment

        Working...
        X