Hibernate's second-level cache

Source: Internet
Author: User

Hibernate's second-level cache

I. INTRODUCTION

  Gaving King once told others that the most dazzling thing about Hibernate was Hibernate's caching mechanism. Hibernate uses a caching mechanism to reduce the application's access to physical data sources. The hibernate cache includes a first-level cache and a level two cache. The first-level cache, also known as "session cache", cannot be uninstalled. In this paper, we introduce the two-level cache of Hibernate.

Two. Level Two cache

  Hibernate's Level Two cache, also known as the "Sessionfactory cache", is optional and is a configurable plugin due to the lifetime of the Sessionfactory object and the entire application process. By default, Sessionfactory does not enable this plugin. Hibernate5.2 provides the org.

Hibernate.cache.ehcache. the Ehcacheregionfactory class, which acts as an adapter between the cache plug-in and hibernate.

  So what kind of data is appropriate for a level two cache?

A. Data that is rarely modified

B. Data that is not very important

C. Data that will not be accessed concurrently

C. Constant data

So what kind of data doesn't fit in a level two cache?

A. Data that is often modified

B. Data with concurrent access is never allowed. such as financial data, it is absolutely not allowed to appear concurrency

C. Data shared with other apps

Three. Implementation of level two cache

Add the following configuration to the 3.1 hibernate.cfg.xml

<!--whether to turn on level two caching -< Propertyname= "Cache.use_query_cache">True</ Property>        <!--To configure the required classes for level two caching -< Propertyname= "Cache.region.factory_class">Org.hibernate.cache.ehcache.EhCacheRegionFactory</ Property>        <!--Configure enable query caching -< Propertyname= "Cache.use_query_cache">True</ Property>

The realization of the annotation form of 3.2 Pojo class

@Entity @table (name= "EMP") @Cacheable (value=true) @Cache (usage=cacheconcurrencystrategy.read_only) Public classEmployee {@Id @Column (name= "Empno") @GenericGenerator (name= "Assignedgenerator", strategy= "Assigned") @GeneratedValue (generator= "Assignedgenerator")    Private intID; @Column (Name= "Ename")    PrivateString ename; @Column (Name= "Job")    PrivateString Job; @Column (Name= "HireDate")    PrivateDate hiredate; @Column (Name= "Sal")    PrivateDouble Salary; @Column (Name= "Comm")    PrivateDouble Comm; //Setter and Getter}

3.3 Cache files

Create a file in the SRC directory named Encache.xml, the file's class capacity is as follows

<Ehcache>    <DiskstorePath= "D:/hibernatecache"/>    <Defaultcachemaxelementsinmemory= "Ten"Eternal= "false"Timetoidleseconds= "+"Timetoliveseconds= "+"Overflowtodisk= "true"/>            <Cachename= "Com.demo.hibernate.one2many.Employee"maxelementsinmemory= "Ten"Eternal= "false"Timetoidleseconds= "+"Timetoliveseconds= "All"Overflowtodisk= "true"/></Ehcache>

3.4 Testing

 Public classHibernateqbctest {Privatesessionfactory SF; @Before Public voidgetsessionfactory () {SF=oraclesessionutils.getsessionfactory (); }        //Query All@Test Public voidList () {Session session=sf.opensession (); Transaction TX=session.begintransaction (); List<Employee> emplist = Session.createcriteria (Employee.class). List ();  for(Employee e:emplist) {System.out.println (E.getename ());         } tx.commit ();                  Session.close (); //The following query does not send SQLSession Session1 =sf.opensession (); Transaction tx1=session1.begintransaction (); Employee e= Session1.get (Employee.class, 7369);           System.out.println (E.getename ());         Tx1.commit ();    Session1.close (); }}

Hibernate's second-level cache

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.