Hibernate + ehcache secondary Cache Technology

Source: Internet
Author: User
1. First set ehcache and create the configuration file ehcache. xml. The default location is class-path, which can be placed in your src directory:
Code:
<? XML version = "1.0" encoding = "UTF-8"?>
<Ehcache>
<Diskstore Path = "Java. Io. tmpdir"/>
<Defaultcache
Maxelementsinmemory = "10000" <! -- Maximum number of caches -->
Eternal = "false" <! -- Whether the cache is persistent -->
Overflowtodisk = "true" <! -- Whether to save the data to the disk. When the system is running -->
Timetoidleseconds = "300" <! -- Destroy the cache after being idle for n seconds -->
Timetoliveseconds = "180" <! -- Destroy the cache after it has been alive for n seconds -->
Diskpersistent = "false"
Diskexpirythreadintervalseconds = "120"/>
</Ehcache>

2. Set in the hibernate configuration file:
Code:

<! -- Set the cache interface class of hibernate, which is included in the hibernate package -->
<Property name = "cache. provider_class"> org. hibernate. cache. ehcacheprovider </property>
<! -- Use query cache? -->
<Property name = "hibernate. cache. use_query_cache"> true </property>
If spring is used to call the sessionfactory of hibernate, set it as follows:
<! -- Hibernatesession factory management -->
<Bean id = "sessionfactory" class = "org. springframework. Orm. hibernate3.localsessionfactorybean">
<Property name = "datasource">
<Ref bean = "datasource"/>
</Property>
<Property name = "hibernateproperties">
<Props>
<Prop key = "hibernate. dialect"> org. hibernate. dialect. oracle9dialect </prop>
<Prop key = "connection. provider_class"> org. hibernate. Connection. c3p0connectionprovider </prop>
<Prop key = "hibernate. show_ SQL"> true </prop>
<Prop key = "hibernate. cache. use_query_cache"> true </prop>
<Prop key = "hibernate. cache. provider_class"> org. hibernate. cache. ehcacheprovider </prop>
</Props>
</Property>
<Property name = "mappingdirectorylocations">
<List>
<Value>/WEB-INF/classes/CN/rmic/manager/hibernate/</value>
</List>
</Property>
</Bean>

Note: If you do not set "query cache", Hibernate will only cache a single Persistent object obtained using the load () method. If you want to cache it, use findall (), list (), iterator (), createcriteria (), createquery (), and other methods to obtain the data result set, you need to set
Hibernate. cache. use_query_cache is true.

3. Add <Cache Usage = "read-only"/> to the HBM file.

......
[Read the full text]

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.