19. First-level caching (session-level caching)

Source: Internet
Author: User

Hibernate-level caching, also known as sesssion cache

The first-level cache lifecycle is short and the lifecycle of the session is consistent, and the primary cache is called the session-level cache or transaction-level cache. is primarily used to cache entity objects without caching the normal properties of entity objects

Methods to support first-level caching:

* GET ()

* Load ()

* Iterate (query entity objects)

How to manage the first-level caching:

* Session.clear (), Session.evict ()

How to avoid a one-time large amount of Entity data warehousing results in memory overflow

* Flush First, then clear

If the volume of data is particularly large, consider a JDBC implementation, and if JDBC does not meet the requirements, consider adopting a specific import tool for the data itself

test Case (the test case is based on a One-to-many bidirectional association map):

Package com.bjsxt.hibernate;

Import java.io.Serializable;

Import org.hibernate.Session;

Import Junit.framework.TestCase;

public class Cachelevel1test extends TestCase {

/**

* Two load queries issued in the same session

*/

public void TestCache1 () {

Session session = NULL;

try {

Session = Hibernateutils.getsession ();

Session.begintransaction ();

Student Student = (Student) session.load (Student.class, 1);

System.out.println ("student.name=" + student.getname ());

does not emit SQL because the load uses caching

Student = (student) session.load (Student.class, 1);

System.out.println ("student.name=" + student.getname ());

Session.gettransaction (). commit ();

}catch (Exception e) {

E.printstacktrace ();

Session.gettransaction (). rollback ();

}finally {

Hibernateutils.closesession (session);

}

}

/**

* Send two get queries in the same session

*/

public void TestCache2 () {

Session session = NULL;

try {

Session = Hibernateutils.getsession ();

Session.begintransaction ();

Student Student = (Student) session.get (Student.class, 1);

System.out.println ("student.name=" + student.getname ());

SQL is not emitted because get uses caching

Student = (student) Session.get (Student.class, 1);

System.out.println ("student.name=" + student.getname ());

Session.gettransaction (). commit ();

}catch (Exception e) {

E.printstacktrace ();

Session.gettransaction (). rollback ();

}finally {

Hibernateutils.closesession (session);

}

}

/**

* Two iterate query entity objects in the same session

*/

public void TestCache3 () {

Session session = NULL;

try {

Session = Hibernateutils.getsession ();

Session.begintransaction ();

Student Student = (Student) session.createquery ("from Student s where s.id=1"). Iterate (). Next ();

System.out.println ("student.name=" + student.getname ());

SQL that emits the query ID and does not emit SQL for querying entity objects because iterate uses the cache

Student = (student) session.createquery ("from student s where s.id=1"). Iterate (). Next ();

System.out.println ("student.name=" + student.getname ());

Session.gettransaction (). commit ();

}catch (Exception e) {

E.printstacktrace ();

Session.gettransaction (). rollback ();

          &

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.