Session cache in Hibernate, hibernatesession

Source: Internet
Author: User

Session cache in Hibernate, hibernatesession
1. Session cache:
1). The implementation of the Session Interface contains a series of Java sets.,These Java sets constitute the Session cache.Used for storageSessionAssociated object(There are many ways to associate Session objects. Example: session. get(Class,OID), Session. update(), Session. save()...). As long as the Session instance does not end its lifecycle,If the cache is not cleared, the objects stored in the cache will not end the lifecycle. Session caching reduces the frequency of Hibernate applications accessing the database.
2). How to operate the Session cache (learn more ).
① If session. get is called()When an object is loaded from the database, the object will be included in the Session cache.
News news= (News)Session. get(News. class,1);
//Will SQL be sent to the database? SQL statement not sent,Instead, get the object reference (snapshot) News news2 from the Session cache.= (News)Session. get(News. class,1);
②. Clear Session()Method to clear the Session cache
News news= (News)Session. get(News. class,1);
//Clear session cache session. clear();//Will SQL be sent to the database? Yes! The Session cache is cleared! News news2= (News)Session. get(News. class,1);
③. SessionFlush()Method: Clear Cache-To make the database record consistent with the object state in the Session cache, an SQL statement may be sent (if the database record is inconsistent with the object state in the Session, an SQL statement will be sent; otherwise, no SQL statement will be sent)
I. By default, the cache is cleared before the transaction is committed.
II. If the primary key generation method uses the underlying auto-increment mode of the database, the Session save()Method, the cache is cleared and the INSERT statement is executed, instead of waiting until the transaction is committed. Hibernate requires that the object associated with the Session must have the OID corresponding to the data table record, which means that the save()The primary key must have an OID, and the underlying auto-increment mode generates a primary key. You must execute INSERT to obtain the primary key value.
//If you use the MySQL underlying auto-increment method to generate a primary key, save()The INSERT statement session. save will be sent.(News);System. out. println(News. getId());
III. When using HQL (Hibernate Query Language) to Query records, it does not pass the Session cache! Directly query the database, and the query results are the latest! Therefore, you must clear the cache session. save before performing HQL queries.(News);
//This will cause the cache to be cleared.= (News)Session. createQuery("FROM News n WHERE n. id =? ").SetInteger(0,News. getId()).UniqueResult();
IV. commit()And flush ()Differences between methods:FlushExecute a series of SQL statements without committing transactions. The commit method first calls flush()Method, and then commit the transaction. This means that the committed transaction will be permanently saved for database operations.
④. Refresh()Method: Make sure that the state of the object in the Session cache is consistent with that in the database record. Therefore, a SELECT statement is forcibly sent. Note that the default isolation level of MySQL is read reptable. Therefore, you must set the transaction isolation level to see the effect of the experiment.
<! -- Set the transaction isolation level of Hibernate to read committed --><Property name="Connection. isolation">2</Property>




Cache issue during hibernate session Query

HibernateSessionFactory. getSession (){
Session session = sessionThread. get ();
If (session = null |! Session. isOpen ()){
If (sessionFactory = null) rebuildSessionFactory ();
Session = sessionFactory. openSession ();
SessionThread. set (session );
}
Return session;
}
First of all, your session retrieval is unreasonable. Generally, session retrieval is in the factory mode, and the session cache is disabled every time you use it.

Hibernate session cache questions

I am also reading this book. It is a concept that "object is associated with session" and "object is saved in session cache.
In session delete (a), if a is a persistent object (already in the session cache), the session executes the delete statement to delete the corresponding records in the database.
If a is a free object, first associate the free object a with the session (in fact, it is loaded into the session cache), turn it into a persistent object, and then execute the delete statement.
The add, delete, modify, and delete operations of the session are object-oriented. You should carefully study chapter 4 manipulating persistent objects to see how the session associates temporary and free objects with the session cache;
How to change a persistent object to a free object or a temporary object!

Related Article

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.