Configuring Hibernate's Level Two cache

Source: Internet
Author: User

1. Add a level two cache configuration to the Applicationcontex.xml file:
  <!--configuring Hibernate sessionfactory--<BeanId="Sessionfactory"class="Org.springframework.orm.hibernate3.LocalSessionFactoryBean"><PropertyName="DataSource"><Refbean="DataSource"/></Property><PropertyName="Mappingresources"><List><Value>com/persia/model/person.hbm.xml</Value></List></Property><PropertyName="Hibernateproperties"> <value> Hibernate.dialect=org.hibernate.dialect.mysql5dialect hibernate.hbm2ddl.auto=update hibernate.show_sql= False Hibernate.format_sql=false hibernate.cache.use_second_level_cache=true // Use level two cache  hibernate.cache.use_query_cache=false //do not use query caching because the hit ratio is not high  Hibernate.cache.provider_class=org.hibernate.cache.ehcacheprovider //use Ehcache  </value > </property</bean> 
2. Add the ehcache.xml cache configuration file:
<?xml version= "1.0" encoding= "UTF-8"?><!--defaultcache node is the default cache policy maxelementsinmemory the maximum allowable number of objects in memory eternal sets whether objects in the cache will never expire Overflowtodisk To store an overflow object on a hard disk timetoidleseconds specifies how long the cache object is idle, the expired object is erased timetoliveseconds the total lifetime of the specified cache object Diskpersistent when the JVM ends Is whether to persist the object diskexpirythreadintervalseconds specifies the polling time of the listener thread dedicated to purging expired objects--<Ehcache><DiskstorePath="D:\cache"/><Defaultcachemaxelementsinmemory="1000"Eternal="False"overflowtodisk="True"timetoidleseconds="120"timetoliveseconds="180"Diskpersistent="False"diskexpirythreadintervalseconds="60"/><cache name="Com.persia.model.Person" maxelementsinmemory=" eternal=" "false" overflowtodisk="true" timetoidleseconds="timetoliveseconds=" 600 " diskpersistent="false"/></ehcache>        
3. Add cache settings to the entity Bean's mapping file:
<?xml version= "1.0" encoding= "UTF-8"?><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://hibernate.sourceforge. Net/hibernate-mapping-3.0.dtd "><hibernate-MappingPackage="Com.persia.model"><ClassName="Person"table="Person"><CacheUsage="Read-write"region="Com.persia.model.Person"/><IdName="id"><generator class= " Native "/> </id< Span style= "color: #0000ff;" >> <property  "name" length= " "not-null=/> </class> </hibernate-mapping >             
4. Test whether the cache is used

Gets from the database for the first time, then caches, closes the database, makes a second fetch, and sees if it can get it.

@Testvoid Testgetperson () {System.out.println (Ps.getperson (7). GetName ());  try {System.out.println ("catch (interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace () ;} System.out.println (" get from cache:" +ps.getperson (7). GetName ());}    
Results:

Wisdom Podcast 4
Please close the database within 15 seconds
Get from Cache: Wisdom Podcast 4

To view the cache files for a disk:

Category: Spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.