Hibernate force clears session cache clear and Flush method Flushmode settings

Source: Internet
Author: User

First, the session in the Flushmode settings:

Set the Flushmode property before the transaction is opened, Method Session.setflushmode (flushmode.always| auto| Commit| never| MANUAL).

Flushmode has 5 values to choose from: Always: Any code will flush, auto: Default mode – Auto, Commit:commit, never: Never, MANUAL: Manual mode.

Second, the session flush () method Description:

You can force synchronization from memory to the database, Method Session.flush ().

Cases:

@Test      /**     * Flush enforces synchronization with database *             /void Testflush () {Session session =  Hibernateuitl.getsessionfactory (). Getcurrentsession (); Session.begintransaction (); Teacher t = (Teacher) session.get (Teacher.  class, 3); T.setname ("yyy"); T.setname ("yyyyy"  )        

Looking at this code, we SetName () 2 times, but the program only changes the database once, at commit time.

@Test      /**     * Flush enforces synchronization with database *             /void Testflush () {Session session =  Hibernateuitl.getsessionfactory (). Getcurrentsession (); Session.begintransaction (); Teacher t = (Teacher) session.get (Teacher.  class, 3); T.setname ("yyy"); Session.flush ();  there is a flush that executes 2 times Updae and does not execute only once T.setname ("  yyyyy"        

We performed Session.flush () at the 2nd time SetName ().

And look at the SQL statements executed by hibernate.

Hibernate:     update        Teacher     set        birthday=?,        name=?,        title=?      where        id=? Hibernate:     update        Teacher     set        birthday=?,        name=?,        Title=?      where        id=?

Performed 2 Update

So it looks like the Flush method forces the database to be synchronized.

The Flush method can be set, that is, when fulsh execution can be set (the first one has a description).

Second, the session clear () method Description:

Either load or Get will first look for the cache (primary cache) if not, it will go to the database lookup, call the Clear () method, you can force clear session cache.

Cases:

 Public void testclear () {          =  hibernateuitl.getsessionfactory (). Getcurrentsession ();          Session.begintransaction ();           = (Teacher) session.get (Teacher.  Class, 3);          System.out.println (T.getname ());           = (Teacher) session.get (Teacher.  Class, 3);          System.out.println (T2.getname ());          Session.gettransaction (). commit ();      }  

The 2 Get method is used here (the Get method executes the SQL statement immediately), but because the first execution caches an entity with ID 3, there are 2 get methods that execute only one SQL statement at a time.

 Public void testclear () {          =  hibernateuitl.getsessionfactory (). Getcurrentsession ();          Session.begintransaction ();           = (Teacher) session.get (Teacher.  Class, 3);          System.out.println (T.getname ());          Session.clear (); // it's not clear. Only executes SQL statements once, with clear executing 2 times           Teacher t2 = (Teacher) session.get (Teacher.  Class, 3);          System.out.println (T2.getname ());          Session.gettransaction (). commit ();   }  

Here we execute session.clear () before the 2nd get, and we put hibernate show_sql out, and it executes 2 SQL statements.

So Session.clear () clears the cache.

Hibernate force clears session cache clear and Flush method Flushmode settings

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.