Hibernate level Two cache configuration

Source: Internet
Author: User

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

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.