Hibernate session Cache

Source: Internet
Author: User

Session Overview

The Session interface is the primary interface that Hibernate provides to the application to manipulate the database, providing a basic way to save, update, delete, and load Java objects.

The Session has a cache, and objects in the cache are called persisted objects, which correspond to related records in the database. Session can update the database synchronously by executing the relevant SQL statements at some point in time, according to the changes in the objects in the cache, which is called flush cache.

Standing in the perspective of persistence, Hibernate divides objects into 4 states: persistent state, temporary state, Free State, delete state. The specific method of the Session allows the object to transition from one state to another.


Session Cache

The implementation of the session interface contains a series of Java collections that form the session cache. As long as the Session instance does not have an end-of-life cycle and the cache is not cleaned, the objects stored in its cache will not end the life cycle

Session caching reduces the frequency with which Hibernate applications access the database.




Operation Session Cache



Flush ()

Flush: Keeps the records in the database tables consistent with the state of the objects in the session cache. In order to remain consistent, the corresponding SQL statement may be sent.

by default, the Session refreshes the cache at the following point in time:

Explicitly invoking the flush () method of the Session may send the SQL statement (why it is used, because the query gets the state in the database that is consistent with the state of the current object), but does not commit the transaction (the SQL statement is sent, the transaction must be committed to update the database)

When the application calls Transaction's commit () method, the method first flush (update) and then commits the transaction to the database

When an application performs some query (HQL, Criteria) operation, if the properties of the persisted object in the cache have changed, the cache is flush first to ensure that the query results reflect the latest state of the persisted object (which is, of course, only updated within the scope of the transaction and not synchronized to the database. Only commit transactions are updated to the database)

exception to flush cache: If an object uses the native generator to generate an OID (the ID of the record is generated by the underlying database using the self-increment), the INSERT statement that inserts the entity into the database is immediately executed when the object is saved by calling the Save () method of the Session Synchronize to the database. Because the Save method must guarantee that the ID exists. (If the ID is generated by hibernate, the insert synchronization to the database is sent only when the commit transaction is committed)the difference between the commit () and flush () methods is that flush executes a series of SQL statements, but does not commit the transaction; The commit method calls the flush () method before committing the transaction. Means committing a transaction means permanently saving the database operation.


set the point in time when the cache is refreshed (understood, usually not changed)

If you want to change the default time point for Flush, you can explicitly set the time of flush by using the Setflushmode () method of the Session


Refresh ()

Forces a SELECT statement to be sent so that the state of the object in the session cache is consistent with the corresponding record in the database table. Controlled by the database isolation level. (You must understand the database isolation level, as shown below)


isolation level of the database

For multiple transactions running concurrently, when these transactions access the same data in the database, if the necessary isolation mechanism is not taken, it can cause various concurrency problems:

Dirty reads: For two things T1, T2, T1 read the fields that have been T2 updated but have not yet been committed. Then, if T2 rolls back, the content read by T1 is temporary and invalid.

Non-repeatable READ: For two things T1, T2, T1 read a field, and T2 updated the field. After that, T1 reads the same field again, and the value is different.

Phantom reading: For two things T1, T2, T1 reads a field from a table, and then T2 inserts some new rows into the table. Then, if T1 reads the same table again, it will have a few more rows.

Database transaction Isolation: The database system must have the ability to isolate and run each transaction concurrently, 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 a variety of transaction isolation levels, which correspond to different levels of interference, the higher the isolation level, the better the consistency of data, but the weaker the concurrency
4 transaction isolation levels provided by the database:

2 Kinds of transaction isolation levels supported by Oracle: READ commited, SERIALIZABLE. The default transaction isolation level for Oracle is:READ commited

Mysql supports the transaction isolation level in 4. The default transaction isolation level for Mysql is: Repeatable READ
Setting the isolation level in MYSQL
    • A separate database connection is obtained for each MySQL program that is started. Each database connection has a global variable @ @tx_isolation that represents the current transaction isolation level. MySQL default Isolation level is repeatable Read
    • To view the current isolation level: SELECT @ @tx_isolation;
    • To set the isolation level for 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;

Setting the isolation level in Hibernate
    • The JDBC database connection uses the default isolation level of the database system. The isolation level can be set explicitly in Hibernate's configuration file. Each isolation level corresponds to an integer: –1.read uncommited–2.read commited–4. Repeatable read–8.serializeable
    • Hibernate sets the isolation level of a transaction by specifying the Hibernate.connection.isolation property for the Hibernate mapping file

Clear () clean up the 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.