Nhibernate problem flush-mode: An SQL statement that repeatedly submits updates in a transaction

Source: Internet
Author: User
Symptom: When a session opens a transaction, insert and update are executed first, and then icriteria is executed. list (), IQUERY. list (), or some sessions. get () method, and finally itransaction. commit (). Monitoring of the executed SQL statements, it is found that nhib.pdf has repeatedly executed a lot of update SQL operations.
Cause:
1. Flush-mode Problems
Nhibatis is executing icriteria. list (), IQUERY. list (), or session. if the get () method involves a database table A, and an object in this table in the current session is modified (dirty), Nhibernate before executing the query, based on the flush-Mode settings, determine whether to apply these updates to the database and then execute the query.
Flushmode value: flushmode. auto: the dirty object in the current session is applied to the database before the query. This is also the default value of the session object; flushmode. commit, only when itransaction is executed. when commit () is used, the dirty object in the current session is applied to the database; flushmode. never, the modification of all objects in the session, will not be submitted to the database at any time (including itransaction. commit ()).
Setting Method: Set the flushmode attribute of the session object.
Exercise caution when setting the flushmode of the session, because this is one of nhibmode's methods to ensure data consistency. We recommend that you use auto.

2. Question about the iusertype equals Method
When determining whether the object in the session is dirty and whether updates are required, Nhibernate compares each attribute of each object in the session with the version of the object just loaded from the database, check for equality. If a property is of a custom type (iusertype login is implemented), incorrect equals implementation will result in Nhibernate believing that this object is dirty every time, so the database will be updated every time.
Therefore, you must reload the equals Method for custom attribute types to determine whether the correct implementation is equal.

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.