The first-level cache concept in Hibernate and the difference between flush and clear

Source: Internet
Author: User

Hibernate uses the caching mechanism to improve data query efficiency. The cache is divided into primary and two caches, a primary cache exists in the session, and a level two cache needs to be configured manually.

In the first-level cache, if the data is saved to the database, and the session is not closed, then the data is placed in the cache, the query request is issued again, hibernate first checks the cache for that data, if the data is found, Instead of initiating a query request to the database, the data in the cache is fetched directly. Take a look at the following example:

 Public class main{    publicstaticvoidthrows  Exception    {        = ( Member) hibernatesessionfactory.getsession (). Get (Member.  Class, 7);        SYSTEM.OUT.PRINTLN (member);                   = (Member) hibernatesessionfactory.getsession (). Get (Member.  Class, 7);        System.out.println (member1);    }}

The above code actually initiates a query request to the database only:

Hibernate:     select        member0_.mid as mid0_0_,        member0_.age as age0_0_,        Member0_.birthday as birthday0_0_,        member0_.name as name0_0_,        member0_.note as note0_0_, member0_.salary as        salary0_0_     From        hedb.member member0_     where        member0_.mid=?  Member [mid=7, age=22, birthday=2017-01-10, Name=admin, Note=good person!, salary=22.22]member [mid=7 , age=22, birthday=2017-01-10, Name=admin, Note=good person!, salary=22.22]

Such a mechanism would greatly improve query efficiency.

But! What if you are asked to store 100000 rows of records in bulk? In this case, the 100000 rows of records are to be cached, which can obviously create a very dangerous move, so in the actual development process, the data must be considered in batches.

Take a look at the following code:

1      Public Static voidMain (string[] args)throwsException2     {3          for(intx = 100; x < 10000; X + +)4         {5Member vo =NewMember ();6Vo.setname ("Hello");7Vo.setage (20);8Vo.setsalary (1000.0);9Vo.setbirthday (NewDate ());Ten hibernatesessionfactory.getsession (). Save (VO); One             if(x% 10 = = 0) A             { -                 //One buffer refresh per 10 records - hibernatesessionfactory.getsession (). Flush (); the hibernatesessionfactory.getsession (). Clear (); -             } -         } +  - hibernatesessionfactory.getsession (). BeginTransaction (). commit (); +}

The flush () and clear () functions of the session interface are used.

    • Flush () commits all the data in the cache to the database, that is, the data that is not persisted to the database in the cache is saved to the database.
    • Clear () clears the cache completely.

The first-level cache concept in Hibernate and the difference between flush and clear

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.