Hibernate session cache

Source: Internet
Author: User

Session Overview

Session interface is the most important interface that Hibernate provides to applications to manipulate databases. It provides basic methods for saving, updating, deleting, and loading Java objects.

Session has a cache. Objects in the cache are called persistent objects, which correspond to relevant records in the database. sessions can execute related SQL statements according to the changes of objects in the cache at certain time points to synchronously update the database. This process is called flush cache)

From the perspective of persistence, Hibernate divides objects into four states: Persistence State, temporary state, free state, and delete state. The Session method can convert objects from one state to another.


Session cache

The implementation of the Session Interface contains a series of Java sets which constitute the Session cache. as long as the Session instance does not end its lifecycle and the cache is not cleared, the objects stored in the Session instance will not end its lifecycle.

Session caching reduces the frequency of Hibernate applications accessing the database.



<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PGJyPgo8L3A + cjxomt6y2df3ifnlc3npb24gu7q051_vade + CjxwPjxpbWcgc3JjPQ = "http://www.2cto.com/uploadfile/Collfiles/20140602/20140602090525337.png" alt = "\">


Flush ()

Flush: Make the records in the database table consistent with the status of the objects in the session cache. To maintain consistency, the corresponding SQL statement may be sent.

By default, the Session refreshes the cache at the following time points:

When the Session flush () method is explicitly called, an SQL statement may be sent (why is it possible? The SQL update will not be sent because the database status in the query result is consistent with the current object status ), but no transaction will be committed (the database will be updated only after the SQL statement is sent)

When the application calls the commit () method of Transaction, the method is flush (updated) first, and then the Transaction is committed to the database.

When the application executes some query (HQL, Criteria) operations, if the attributes of the persistence object in the cache have changed, the cache will be flushed first, to ensure that the query results can reflect the latest status of the Persistent Object (of course, this situation is only updated within the transaction scope, and has not been synchronized to the database, only the committed transactions will be updated to the database)

Exception of flush cache: if the object uses the native generator to generate an OID (The Record id is generated by the underlying database using auto-increment), when the Session save () is called () method To save the object, the insert statement that inserts the object into the database is immediately executed to synchronize to the database. because the id must exist after the save method. (If the id is generated by hibernate, the insert statement is sent to the database only when the commit commits the transaction.) The difference between commit () and flush () Methods: flush executes a series of SQL statements, but no transaction is committed. The commit method first calls the flush () method and then commits the transaction. it means that the transaction is committed and the database operations are permanently saved.


Set the cache refresh time point (understand, usually do not change)

If you want to change the default flush time point, you can explicitly set the flush time point through the setFlushMode () method of the Session.


Refresh ()

The select statement is forcibly sent to make the state of the objects in the session cache consistent with the corresponding records in the database table. Controlled by the database isolation level. (You must understand the database isolation level, as shown below)


Database isolation level

For multiple transactions running at the same time, when these transactions access the same data in the database, if the necessary isolation mechanism is not adopted, various concurrency problems will occur:

Dirty read: For two things T1, T2, T1 read fields that have been updated by T2 but not submitted. if T2 is rolled back, the content read by T1 is temporary and invalid.

Repeatable reading: one field is read for T1, T2, and T1, and then T2. after T2. T1 reads the same field again, the value is different.

Phantom read: For two things T1, T2, T1 read a field from a table, and T2 inserts some new rows in the table. then, if T1 reads the same table again, there will be several more rows.

Database transaction isolation: the database system must be able to isolate and concurrently run various transactions so that they do not affect each other and avoid various concurrency problems.

The degree to which a transaction is isolated from other transactions is called the isolation level. the database specifies multiple transaction isolation levels. Different isolation levels correspond to different levels of interference. The higher the isolation level, the better the data consistency, but the weaker the concurrency.
Four transaction isolation levels provided by the database: mysql program, a separate database connection will be obtained. each database connection has a global variable @ tx_isolation, indicating the current transaction isolation level. the default isolation level of MySQL is Repeatable Read to view the current isolation level: SELECT @ tx_isolation;
Set the isolation level of the current mySQL connection:
Set transaction isolation level read committed;
Set the global isolation level of the database system:
Set global transaction isolation level read committed;

In Hibernate, set the isolation level. JDBC database connection uses the default isolation level of the database system. you can explicitly set the isolation level in the Hibernate configuration file. each isolation level corresponds to an integer:-1. READ UNCOMMITED-2.READ COMMITED-4.REPEATABLE READ-8.SERIALIZEABLEHibernate by specifying Hibernate for the hibernate ing file. connection. isolation attribute to set the transaction isolation level

Clear () Clear Cache








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.