Hibernate Entry caching mechanism

Source: Internet
Author: User

Reprinted from: http://www.cnblogs.com/blackdeng/p/7027465.html

What is the session cache?

Session cache: In the implementation of the session interface contains a series of collections (list), which are used to save the session created/modified/queried objects, these collections (list) constitute the session cache. As long as the session instance does not have an end-of-life cycle, objects stored in its cache will not end the life cycle

The first-level cache is the default open session each time the data is queried, the number is taken from the primary cache, if there is a direct return of data, if the cache does not fetch data, query the database, and put the query results into the cache.

For example, when you use a Save () method to save an object, this time as long as the cache does not empty, then in subsequent operations can not access the database by accessing the cache for query operations.

How do I manipulate session cache data?

Session.flush: The cache in the state of the non-synchronized objects first synchronized to the database memory (not saved to the database physical hard disk), and labeled just completed synchronization of the object state is synchronized, the actual execution of a series of SQL statements, but do not commit transactions,; But not submitted)

Transation.commit: First call the Flush () method and then commit the transaction. means committing a transaction means permanently saving the database operation.

Session.refresh: Refresh, synchronize session and database, execute query, display the latest information of the database, update the state of locally cached objects. (use query statements to flush memory and keep the memory information consistent with the database).

Session.evict (obj): Clears the object in the parameter from the session cache.

Session.clear: Empty cache, equivalent to List.removeall ();

Session.close: Clears the session cache and closes the session resource.

What is a snapshot?

When the session loads the object, a copy of the object is created in the session, and the copy's property value is the same as the value in the database, and the copy becomes a snapshot. When the session cleans up the cache, it determines which properties of the object have changed by comparing the object's current properties and the snapshot's property values.

* Executed SQL statements that have changed

* No changes no longer execute statements

(Get the database information before executing the memory statement to determine if you need to perform the delete or change statement)

The snapshot mechanism effectively reduces the database operations.

What is Hibernate status?

Hibernate divides objects into 4 different states:

Temporary status: New Java object, this time has not been associated with hibernate. The next step is to enter the persistence State through Save ().

Persistent state: There is already a session in which only the synchronization will be updated to the database.

Free State: Has been cleared out of session, this time can be saved by reference, similar to the temporary state, but can use the lock () method to return to the persistent state

Delete State: The deletion state after the Delete method has been executed. Cannot go back to persistent state

Hibernate Entry caching mechanism

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.