First, the definition:
A secondary cache is a process or cluster-wide cache that can be shared by all sessions and is a configurable plugin
二、二级 caching schematic diagram
Parse: Every time an object is removed from the level two cache, it is a new object.
Three, the configuration steps are as follows:
Similarly: Take employee and departmental classes as an example
① Introducing JAR Packages
② Key points:
Large Configuration Hibernate.cfg.xml turn on level two cache
③ Add the ehcache.xml file under src and get the file from etc .
④ Test level Two cache (characteristics of data bulk)
Public classTest1 {Session session; Transaction TX; Session Session2; Transaction tx2; @After Public voidaftertest () {tx.commit (); Hibernateutil.closesession (); } @Before Public voidInitData () {Session=hibernateutil.getsession (); TX=session.begintransaction (); } /** Level Two cache*/@Test Public voidTwotest () {Session=hibernateutil.getsession (); TX=session.begintransaction (); EMP EMP= (EMP) session.get (EMP).class, 2); SYSTEM.OUT.PRINTLN (EMP); Tx.commit (); System.out.println ("========================"); EMP EMP2= (EMP) session.get (EMP).class, 2); System.out.println (EMP2); System.out.println ("========================"); Session2=hibernateutil.getsession (); TX2=session2.begintransaction (); EMP Emp3= (EMP) session2.get (EMP).class, 2); System.out.println (Emp3); Tx2.commit (); System.out.println ("========================="); }
The output results are as follows:
Note: The first-level cache holds a reference to the memory object, the data in the level two cache is bulk, and the new object needs to be repackaged, so two memory addresses are different
Hibernate level Two cache configuration