[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.