[Original]java Web Learning Note 78:hibernate Learning Path---session overview, session cache (hibernate cache), isolation level of the database, setting isolation level in MYSQL, setting isolation level in Hibernate

Source: Internet
Author: User
Tags java web

The purpose of this blog: ① summary of their own learning process, equivalent to study notes ② to share their own experience to everyone, learn from each other, communication, not commercialcontent inevitably appear problems, welcome to correct, exchange, discussion, you can leave a message, can also be contacted by the following ways. I Internet technology enthusiasts, Internet technology enthusiastsWeibo: Ivan is in 0221qq:951226918

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- -----------------------------

1.Session Overview

1) Session interface is the most important interface that Hibernate provides to the application to manipulate the database, which provides basic methods for saving, updating, deleting, and loading Java objects .

2) TheSession has a cache, and the object in the cache is called the persisted object, which corresponds to the related record 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.

3) Standing on a persistent point of view, 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.

2.Session Cache

  1) A series of Java collections are included in the implementation of the session interface, which forms 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

2) Session cache reduces the frequency of Hibernate application access to the database.

3) about the session cache operation

    

  Flush Cache

①flush:session Updating a database by changing the properties of objects in the cache

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

   Explicitly calling the flush () method of the Session

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

When an application performs some query (HQL, Criteria) operation, if the properties of persisted objects in the cache have changed, the cache is flush first to ensure that the query results reflect the latest state of the persisted object

exceptions to the ③flush cache: If the object uses the native generator to generate the OID, then when the object is saved by the Save () method of the Session, the I that inserts the entity into the database is immediately executed The nsert statement.

The difference between the commit () and flush () methods: 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 .

   

Understanding: Setting a point in time to refresh the cache:

     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

    

Reflesh (): Forces the state of an object in the cache to be consistent with the record of the database. Forces the SELECT statement to be sent so that the state of the object in the session cache is consistent with the corresponding record in the data table

Clear (): Clean up the cache

  

4) isolation level of the database

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.

② the isolation of database transactions: 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 known as 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

The ④ database provides 4 types of transaction isolation levels:

5) Set the isolation level in MYSQL

① each MySQL program starts, it gets a separate database connection. Each database connection has a global variable @ @tx_isolation that represents the current transaction isolation level. MySQL default Isolation level is repeatable Read

② View the current isolation level: SELECT @ @tx_isolation;

③ sets the isolation level of the current MySQL connection: Set TRANSACTION isolation levels Read Committed;

④ set the global isolation level of the database system: Set global transaction isolation levels Read Committed;

6) Setting the isolation level in Hibernate

The ①JDBC database connection uses the default isolation level of the database system. You can set the isolation level 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 set the isolation level of a transaction by specifying the Hibernate.connection.isolation property for the Hibernate mapping file

[Original]java Web Learning Note 78:hibernate Learning Path---session overview, session cache (hibernate cache), isolation level of the database, setting the isolation level in MYSQL, hibernate Setting the isolation level in

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.