Announcement Announcement Module
Collapse
No announcement yet.
How do I setup EhCache with Spring and Hibernate? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How do I setup EhCache with Spring and Hibernate?

    Hi,

    I'm using Hibernate from Spring, and am now looking to setup EhCache with it.

    I'm currently configuring Hibernate from my applicationContext.xml. I presumed I could just set the cache provider in this configuration, i.e.

    <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.Provider</prop>

    and then create an ehcache.xml file for the cache configuration. I've tried this, but unfortunately I get the following error:

    The following exception was logged org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'sessionFactory' defined in resource [/WEB-INF/applicationContext.xml] of ServletContext: Initialization of bean failed; nested exception is net.sf.hibernate.HibernateException: Could not instantiate Cache
    net.sf.hibernate.HibernateException: Could not instantiate Cache

    The ehcache.xml configuration file is definitely getting picked up. Presumably I'm going about it the wrong way?

    Thanks, Owen

  • #2
    ehcache is actually the default 2nd level cache in Hibernate. Specifying this provider or not specifying it should actually thus make no difference at all.

    Now if you are getting this failure when you try to use the cache, perhaps either your ehcache jar file is missing, or maybe your config for ehcache (you can rely on the default config too) is bad.

    Comment


    • #3
      If you setup your own ehcache.xml file - make sure you have diskStore and defaultCache defined. You only need diskStore if overflowToDisk="true".

      http://www.systemmobile.com/blog/ind...with_hibernate

      Here's a sample:
      Code:
      <ehcache>
          <!-- Required elements -->
          <diskStore path="java.io.tmpdir"/>
          <defaultCache
              maxElementsInMemory="10000"
              eternal="false"
              timeToIdleSeconds="120"
              timeToLiveSeconds="120"
              overflowToDisk="true"/>
      
          <!-- Cache settings per class -->
          <cache name="org.appfuse.model.User"
              maxElementsInMemory="1000"
              eternal="false"
              timeToIdleSeconds="300"
              timeToLiveSeconds="600"
              overflowToDisk="true"/>
      </ehcache>

      Comment


      • #4
        Fix it, thanks for your ideas.

        Thanks for your help guys.

        In the end it turned out to be that my timeToIdleSeconds and timeToLiveSeconds were set too high (in the cache config), so caused problems when Hibernate tried to create the cache instances. Although, has to be said, the exception message was not very useful.

        Still can't have everything. Thanks for your help.

        Cheers, Owen

        Comment


        • #5
          how to use spring and hibernate together

          hello friend...,
          i'm a biginner in "SPRING". i have done small example both in spring and hibernate seperately. now i have started to do a small application by using both.
          in my application , i am getting 'name', 'age' firlds from a jsp and through hibernate i have to insert into the database.

          so friends, can u help me in this case?
          thanking you..................

          Comment


          • #6
            Mapping Ehcache.xml i spring descriptor

            Hi,

            I want to refresh the cache each 12 hours, I already set up my Ehcache.xml file, however I don't know how to map it in my Spring descriptor that looks like:

            <bean id="mySessionFactory"
            class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
            <property name="dataSource" ref="myDataSource" />
            <property name="mappingResources">
            <list>
            <value>validador/resources/hibernate/Autorizaciones.hbm.xml</value>
            <value>validador/resources/hibernate/MsgGestor.hbm.xml</value>
            </list>
            </property>
            <property name="hibernateProperties">
            <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Orac le9Dialect</prop>
            <prop key="hibernate.cache.provider_class"> org.hibernate.cache.EhCacheProvider</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
            </props>
            </property>
            </bean>



            Please tell me the way I can have my Ehcache.xml properties file being activated by Spring.

            Thank you!!!

            Comment


            • #7
              You don't have to map the file you just need to ensure its on the classpath.

              This might be useful as well.
              http://ehcache.sourceforge.net/docum...hibernate.html

              Comment


              • #8
                Help required while using ehCache in my app using spring and hibernate

                Hi,

                I have been facing this issue from sometime while using ehCache in my application. am totally new to these technologies..

                This is what am doing after going thru various posts.

                My ehCache.xml

                HTML Code:
                <defaultCache
                            maxElementsInMemory="10000"
                            eternal="true"
                            timeToIdleSeconds="10"
                            timeToLiveSeconds="10"
                            overflowToDisk="true"
                            diskSpoolBufferSizeMB="300"
                            maxElementsOnDisk="10000000"
                            diskPersistent="false"
                            diskExpiryThreadIntervalSeconds="10"
                            memoryStoreEvictionPolicy="LRU"
                            /
                            
                      cache name="com.test.entity.Contact"
                		   maxElementsInMemory="50"
                		   eternal="true"
                		   overflowToDisk="false"
                	/
                My Hibernat config file
                HTML Code:
                <]beans:property name="hibernateProperties">
                			<beans:props>
                				<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</beans:prop>
                				<beans:prop key="hibernate.show_sql">true</beans:prop>
                				<beans:prop key="hibernate.connection.release_mode">auto</beans:prop>
                				<beans:prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</beans:prop>
                				<beans:prop key="hibernate.cache.use_second_level_cache">true</beans:prop> 
                				<beans:prop key="hibernate.cache.use_query_cache">true</beans:prop> 
                				<beans:prop key="hibernate.auto_close_session">true</beans:prop>
                				<beans:prop key="c3p0.min_size">10</beans:prop>
                				<beans:prop key="c3p0.max_size">100</beans:prop>
                				<beans:prop key="c3p0.timeout">10</beans:prop>
                				<beans:prop key="c3p0.acquireRetryAttempts">30</beans:prop>
                				<beans:prop key="c3p0.acquireIncrement">5</beans:prop>
                			    <beans:prop key="c3p0.idleConnectionTestPeriod">300</beans:prop>				 				
                				<beans:prop key="c3p0.initialPoolSize">20</beans:prop>
                				<beans:prop key="c3p0.maxPoolSize">100</beans:prop>
                				<beans:prop key="c3p0.maxIdleTime">300</beans:prop>
                				<beans:prop key="c3p0.maxStatements">50</beans:prop>
                				<beans:prop key="c3p0.minPoolSize">10</beans:prop>
                				<beans:prop key="hibernate.generate_statistics">true</beans:prop>
                			</beans:props>
                                </beans:property>
                My hbm File( contact.hbm.xml)
                Added below line.
                <cache usage="read-only" />

                After doing all this log says its falling into default cache configuration and no exceptions are thrown. But the DB is again getting hit evrytime the same query is getting executed.


                Could some one let me know what am missing in this? above are the only changes i have done and not touched the DAOs and the methods actually.

                Any kind of helo would be appreciated since am very much in need of this urgently..

                Thanks,
                Arun

                Comment

                Working...
                X