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