Hibernate's Level Two cache usage (Spring use)

Source: Internet
Author: User

(a) Thegeneral process for Hibernate's level two cache strategy is as follows:
1) When a condition is queried, always issue a SELECT * FROM table_name where .... (select all fields) such SQL statements query the database and get all the data objects at once.
2) Put all the obtained data objects into the second level cache based on the ID.
3) When hibernate accesses the data object according to the ID, it is first checked from the session cache, and if the level two cache is configured, it is checked from the level two cache, and the database is not found, and the result is placed in the cache by ID.
4) Update the cache while deleting, updating, and adding data.
  Hibernate's level two cache policy, which is a cache policy for ID queries, has no effect on conditional queries. To do this,hibernate provides query Cache for conditional queries.
(b) What data is suitable for storage in the second level cache?
1 Rarely modified data
2 is not very important data, allowing occasional concurrent data
3 data that will not be accessed concurrently
4 reference data, refers to the supply of constant data reference, its limited number of instances, its instances will be referenced by many other instances of the class, the instance is rarely or never modified.
(iii) data that is not suitable for storage to the second level cache?
1 frequently modified data
2 financial data, never allow concurrency
3 data that is shared with other apps.
Practice part: 1: Import Ehcache-1.2.3.jar to lib. 2: Import Ehcache.xml to SRC. 3: Modify the object-relational mapping file for the persisted class that you want to configure the cache: Note:<cache> should be placed in front of the <class/> back,<id/>. 4: In spring's configuration file,hibernate section joins XML code
<!--enable level two cache, default is (false) off--<prop key= "Hibernate.cache.use_second_level_cache" >true</prop>< !--Set the query cache if you do not set the ' query cache ', hibernate caches only a single persisted object that is obtained using the load () method, if you want to cache using FindAll (), List (), Iterator (), Createcriteria (), CreateQuery () and other methods to obtain the data result set, you need to set Hibernate.cache.use_query_cache true--><prop key= "Hibernate.cache.use_ Query_cache ">true</prop> <!--Specify cache product Provider--  <prop key=" Hibernate.cache.provider_class "> Org.hibernate.cache.ehcacheprovider</prop> 
Five: In DAO, before calling the Find method query, set the use of the cache
Hibernatetemplate hibernatetemplate=this.gethibernatetemplate (); Hibernatetemplate.setcachequeries (true);// Enable level two caching. list<area> list = Hibernatetemplate.find ("from area"); Hibernatetemplate.setcachequeries (false);//close query Cache ...

  

Hibernate's Level Two cache usage (Spring use)

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.