Hibernate's evict and clear

Source: Internet
Author: User

Excerpt from Baidu know: http://zhidao.baidu.com/question/63663640.html

Q: First create a student, then call the Session.save method, and then call the evict method to clear the student object out of the cache, and then commit the transaction, but error: Exception in thread "main" Org.hibernate.AssertionFailure:possible Nonthreadsafe access to session but if I'm not using the evict method, but the clear method is used to clear the cache, the program is not wrong. A: session.evict (obj) clears the specified Buffer object Session.clear (), clears all objects in the buffer, but does not include the objects in the operation. Therefore, hibernate executes the following sequence, (1) generates a transaction object, and marks the current session in the transaction state (note: database-level transactions are not started at this time). (2) The application uses S.save to save the object, this time the session will put this object into Entityentries, to mark the object has been associated with the current session, because the application to save the object, the session is also registered in the insertions application of this insert behavior (behavior includes: such as reference, Object ID, Session, persisted Class). (3) S.evict detach the object from the S session, and S will move the object out of the entityentries. (4) Transaction commits, all caches need to be flush into the database, session initiates a transaction, and submits all previously registered operations in the order of Insert,update,......, Delete (note: The update will not be performed until all insert executions have completed. Special handling here can also make a mess of your program, such as the need to control the execution order of operations, to be good at using flush), now the object is not in Entityentries, but in the execution of the insert behavior only need to access insertions is sufficient, So there is no exception at this time. An exception occurs after an insert notifies the session that the object has been inserted, and this step requires the EXISTSINDATABASE flag of the object in Entityentries to be set to true, since the object does not exist in entityentries. Hibernate now thinks that insertions and entityentries may be out of sync because of thread-safety issues (or whether hibernate developers are considering how to handle the example, and if not, it might be a bug), So a net.Sf.hibernate.AssertionFailure is thrown, the program terminates the general error that the S.save will be executed immediately, and the object is prematurely removed from the session, resulting in the session of the insertions and entityentries the content of the different steps. So when we do this, it's important to know when hibernate will flush the data into the database and not detach the manipulated object from the session until it's flush. The workaround is to add Session.flush after save.

Hibernate's evict 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.