Article Directory
Ways to reclaim memory
Null references will be reclaimed memory
In Java, null references will be recycled by Java's garbage collection mechanism.
The object will allocate space when it is instantiated, that is, space will be allocated only in the third row. Assign the first address of the memory heap area to C1
In line 4, the address is also assigned to C2;
A new object will be created on the 5th line, and the new address value will be assigned to C2;
Isolated references will be reclaimed memory
There is an attribute of the order collection type in the User object.
Cache
The role of the cache is to reduce the pressure on the permanent storage source (the value database here)
Session cache
The internal implementation is always referenced through the internal interface.
Let a reference of a collection type always exist.
The essence is to put the object in the collection
SessionFactory.openSession();初始化session对象,初始化Session。
When the Session is inquired, look at the cache first, and then look at the database
when there is no. When calling get and save methods, they are all in the cache
Clean up! =
Clean up Clean up = Make cached data consistent with database data
Update the database synchronously according to the Session cache
Session.flush();清理缓存,会被roulBack回滚事务
Session.close();会永久提交,不会被回滚
The Load and get methods do not clean up the cache, and the get and load methods read the cache.
The cache will be judged first, so the database will not be read, and the dirty data (error data) of the database will not be read
Session snapshot
The save method will put the data in the cache, and create a backup (snapshot) in the
cache. When cleaning the cache, it will judge whether the data in the cache is consistent with the snapshot. If it is inconsistent, it will update the new data to the database.
Menual: Manually clean up the cache, the commit method is not performing cleanup
COMMIT: It is suitable for more queries, and the data is not safe
Session cache function
Stack overflow exception
Will not create a new object, but the OID assignment;
The life cycle of Hibernate objects
The life cycle of an entity object
Temporary-"Persistence-"Free-"Delete
Temporary object specific: the cache has records, the data table has no corresponding records
Persistence features: the database has records, the cache also has corresponding
features Free object features: the database may have records, but the cache does not correspond to the
deleted object Features: not in the cache, and plan to delete the database recording.
The close method will clear the cache object and modify the cache flag (identifying whether the Session is available) The
clean method only clears the cache
In addition to the persistent state will not be recycled, other states may be recycled, because the persistent state must have references.
Hibernate operating persistent objects
Session的Save()
Session的update()
The label of the fourth point can be used to realize that the database is not updated when it is consistent with the snapshot,
but there will be an additional operation to query the database, so it is generally not used.
Session的saveOrUpdate()
By default, the attribute is a temporary object as long as OID=null. The
premise is the referenced object
The delete method of Session