NH. Flush Mechanism

Source: Internet
Author: User

 

In the process of using NH, we generally start a transaction on the session, store the object to the database through the session, and then detach the object from the session to commit the transaction, this is a normal process.

 

But sometimes this operation will fail, because we do not perform operations directly on the database, and there is a NH that provides persistence services for us.

 

NH persistence Mechanism

 

Simply put, NH implements a cache zone on the database layer. when an object is saved or updated, NH does not actually write the object into the database, the preceding registration is performed only when the cached data is flushed into the database.

 

In the actual execution process, each session maintains all objects associated with the session and operations performed by the application on these objects through several mappings and sets, there are entityentries (Session ing of objects associated with the session), insertions (all insert operation sets), deletions (delete operation set), and updates (update operation set ). Next I will explain how hibernate works in the first example.

    • Generate a transaction object and mark the current session in the transaction state
    • Use S. save to save the object. At this time, the session puts this object into entityentries to mark that the object has been associated with the current session. Because the application saves the object, the session also registers the insert behavior of the application in insertions (behavior includes Object Reference, Object ID, session, persistent processing class ).
    • To commit a transaction, flush all the caches into the database. The session starts a transaction and submits all the previously registered operations in the order of insert, update, and delete (note: update is executed only after all insert statements are executed.ProgramMake a mess. If you need to control the execution sequence of operations, be good at using flush ).
    • When executing the insert action, you only need to access insertions. After the object is inserted, the session is notified that the object has been inserted. In this step, set the existsindatabase flag of the object in entityentries to true. If the object does not exist in entityentries, at this time, Hibernate considers that insertions and entityentries may cause non-synchronization due to thread security issues, so a net. SF. hibernate. assertionfailure is thrown and the program is terminated.

 

Because of the cache mechanism of NH, in many books, the author will mention that transactions are recommended. In this way, you can avoid writing session. Save () or other data into the cache, but it is not executed in the database.

Another way of not using transactions is to flush the transaction after an operation is executed.

 

Reference: http://wenku.baidu.com/view/c91a3f5c804d2b160b4ec033.html

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.