Hibernate Learning Notes: Understanding first-level caching and session cleanup

Source: Internet
Author: User

[Most of this article is from the internet]

Session executes some SQL statements to synchronize the state of an object in memory to a database, a process known as session cleanup.
By default, the session cleans up the cache at the following point in time.
1 when an application invokes the Net.sf.hibernate.Transacation commit () method, the commit () method cleans the cache before committing the transaction to the database.
2 When an application invokes the session's find () or iterate (), if the properties of the persisted object in the cache change, the cache is cleaned up to ensure that the query results reflect the latest state of the persisted object.
3 When the application displays the flush () method that invokes the session.
Note the difference between the commit () and the Flush () method of the session. The flush () method cleans the cache, executes a series of SQL statements, but does not commit the transaction; the commit () method calls the Flush () method first, and then

Commit the transaction after. Committing a transaction means that updates made to the database are preserved permanently.
The order in which session cleanup is performed:
1. Executes all INSERT statements that are inserted into the implementation in the order in which the application calls the Session.save () method.
2. Perform all UPDATE statements that are updated on the entity.
3. Perform all delete statements to the collection for deletion.
4. Execute all SQL statements that delete, update, or insert the elements of the collection.
5. Executes all INSERT statements that insert into the collection.
6. Executes all DELETE statements to the entity by the order in which the Session.delete () method is invoked by the application.
The Setflushmode () method of the session is used to set the time point for scavenging the cache. The Flushmode class defines three different cleanup modes: Flushmode.auto, Flushmode.commit, and Flushmode.never.

patterns for cleaning up caching

Query method of session

the Commit () method of the session

Flush () method of session

Flushmode.auto Clean Clean Clean
Flushmode.commit Do not clean up Clean Clean
Flushmode.never Do not clean up Do not clean up Do not clean up

In batch processing of large amounts of data to avoid memory overflow, you can manually intervene first-level caching:

Session.evict removes an object from the first-level cache, such as: GetSession (). evict (member);

Session.clear clears all cached objects for this session.

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.