Hibernate Session Cache

Source: Internet
Author: User

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.

1: Querying Objects with reflection

News News Session.get ( News) Class, 1);        SYSTEM.OUT.PRINTLN (news);         = (News) Session.get (news).  Class, 1);        System.out.println (NEWS2);

But there's only one SQL

This is the session cache. Also called a first-level cache. The data in the cache is called a persisted object.

Stand in the perspective of persistence. Hibernate divides objects into 4 states: persistent. Temporary. Free. Delete.

The session cache has three methods.

1:flush () method.

Its role: The data objects in the cache are consistent with the data objects in the database. The SQL statement is sent. However, the transaction is not committed.

To ensure consistent data. The flush () method is called in the following.

1: The call is displayed. Session.flush ();

2: When the transaction is committed. Flush () is called first. Then commit the transaction.

3: Perform hql or QBC queries.

4: If the ID of the record is generated by the native (the form of the underlying database self-increment). After the Save () method is called. Send the INSERT statement immediately.

The source of the Commit () method: In the Abstracttransaction class.

1 @Override2      Public voidCommit ()throwshibernateexception {3         if(Localstatus! =localstatus.active) {4             Throw NewTransactionException ("Transaction not successfully started" );5         }6 7Log.debug ("Committing" );8 9 beforetransactioncommit ();Ten  One         Try { A docommit (); -Localstatus =localstatus.committed; - aftertransactioncompletion (status.status_committed); the         } -         Catch(Exception e) { -Localstatus =Localstatus.failed_commit; - aftertransactioncompletion (status.status_unknown); +             Throw NewTransactionException ("Commit Failed", e); -         } +         finally { A invalidate (); at afteraftercompletion (); -         } -}

Commits the transaction on line 12. Line 9th is beforetransactioncommit ();

What to do before committing a transaction?

In the Jdbctransaction class.

1 protected voidBeforetransactioncommit () {2Transactioncoordinator (). Sendbeforetransactioncompletionnotifications ( This );3 4         //Basically, if we are the driver of the transaction perform a managed flush prior to5         //physically committing the transaction6         if(Isdriver &&!)transactioncoordinator (). Gettransactioncontext (). Isflushmodenever ()) {7             //If an exception occurs during flush, user must call rollback ()8 transactioncoordinator (). Gettransactioncontext (). Managedflush ();9         }Ten  One         if(isdriver) { ATransactioncoordinator (). Gettransactioncontext (). Beforetransactioncompletion ( This ); -         } -}

Manage transactions in the 8th row. In the TransactionContext class

 Public void Managedflush ();
  in the Sessionimpl class
1publicvoid Managedflush () {2 if (isClosed ()) { 3 log.trace ("skipping Auto-flush due to session closed" ); 4 return ; 5 }6 log.trace ("Automatically flushing session" ); 7 Flush (); 8 }

The flush () method is called on line 7th.

2:refresh (): forces the state of an object in the cache to match the state of the objects recorded in the database.

Hit the breakpoint. Then now go to the database to modify the data.

The data obtained are the same.

Use the Refresh () method

This is because hibernate is in the isolation level in MySQL. The default is repeatable read. Although a select was re-read. But still read the previous SELECT statement.

In the Hibernate.cfg.xml

1 <!--set Isolation LEVEL--2         <property name= "Connection.isolation" >2</property>

1. Read Uncommited UNCOMMITTED data
2. Read commited reading submitted data
4. REPEATABLE Read REPEATABLE reading
8. Serializeable serialization

3:clear () Cleanup cache

Hibernate Session 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.