Hibernate session Cache and Java object life cycle

Source: Internet
Author: User

I. Java object life cycle1. In Java, using the new keyword to create a Java object, the JVM allocates a piece of memory space for the object. As long as this variable is referenced, he is always in memory. If it is not referenced by any variables (including indirect references), then the object will be reclaimed by the garbage collector. Here's a snippet of code to explain:
Customer C=new customer ();  Order o1=new Order ();  Order 02=new Order ();  O1.setcustomer (c);  C.getorders (). Add (O1);  O1=null;  O2=null;  C=null;
The two-way correlation between customer and order is established in line four and fifth. means that there is a reference to O1 in C, and O1 has a reference to C.*Java Collection has one important feature: a reference to a Java object is stored in the collection . When O1=null is executed, although the variable O1 no longer points to the Order1 object, it can still navigate to the Order1 object through C.getorders (), so the Order1 object does not end the life cycle. When O2=null is executed, the Order2 object is no longer referenced by any reference variable and can be recycled. when the c=null is executed, the customer is not referenced by any variable, ends the life cycle, and the corresponding Order1 object is not referenced by any reference variable, and the life cycle is ended. If you want a Java object A to be in the life cycle, you must ensure that a variable references him or other object B in the life cycle to navigate to this object A. Second, session cacheThe implementation in the session interface contains a series of Java collections that make up the session cache, as long as the session instance does not have an end-of-life cycle, and the objects stored in the cache do not end the life cycle. When a customer is persisted with the Save () method, the Customer object is added to the cache, and when the Get method view of the session loads a customer instance, the The session first determines whether the customer object already exists in the cache. the three main functions of session caching:1. Reduce the number of database accesses. 2. When there is a cyclic correlation between persisted objects in the cache, Sess guarantees that the dead loop that accesses the object graph is not present. 3. Ensure that the relevant records in the database are kept in sync with the corresponding townships in the cache.
when the session cleans up the cache, the dirty check is done automatically, and if the objects in the session cache are found to be inconsistent with the database records, the database will be updated synchronously according to the object's latest properties. third, dirty data check and clean cache mechanismwhen a customer object is added to the session cache, the session copies a snapshot of the value type property of the Customer object, and when the cache is cleaned, a dirty check is made to compare whether the properties of the customer object have changed Updates the database synchronously if it changes to a dirty object. Shows the process of dirty checking and synchronizing the database.
time to clean up the cache:

One more thing: After my test, although the INSERT statement was printed immediately after the session's Save method was called, the data in the database was not updated, that is, there was no real insert.
we can also show ourselves the time of the specified cleanup cache: The following methods
 the show call flush method cleanup cache is typically used for the following occasions:1. Insert, delete, update a persisted object raises a trigger in the database. 2. In the application, mix hibernate and JDBCThe 3.JDBC driver is not robust enough to automatically clean up cache mode and does not work properly.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hibernate session Cache and Java object life cycle

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.