Hibernate Learning (iv) Hibernate cache & Snapshot

Source: Internet
Author: User

caching: High efficiency

Hardware CPU cache memory for hard disk cache

IO Stream caching for software

Hibernate's first-level cache is also designed to operate the database efficiently.

Proof of a slow existence:

Person p=session. Get (Person.class, 1);

Person P1=session.get (person.class,2);

Person P2=session.get (person.class,3);

System.out.println (P=P1);

The console output is:

SELECT * from the person where id=1;

True

Caching principle:

Snapshot

When the code changes to:

Note: The name value of the first data in the original Database person table is "Zhang San"

Person P=session.get (person.class,1);

P.setname ("Zhangsan");

P.setname ("Zhang San");

After execution, the console appears as

SELECT * from the person where id=1;

View the database in the person table with a data ID of 1 name= "Zhang San"

This applies to the snapshot of hibernate:

Increase efficiency: 1. Improve query efficiency (caching principle) 2. Reduce unnecessary modification statement send (snapshot)

To see Hibernate object status from the cache:

1. Transient status: No ID, no in session cache

2. Persistent status: With ID, in session cache

3. Free/Managed Status: There is no ID in the cache

A deeper understanding of the snapshot: The following code intercepts some of the main code in the test method

.........

Person P=new person (); Instantaneous state

P.setid (1); Free State, with ID not cached in session

Session.update (P); Persistent state, with ID cached in session

Person P=session.get (Person.class, 1);

Tx.commit ();

.........

=new person in person P (); Set a breakpoint, debug mode to execute the view console in the SQL statement display

Until run to person p= session.get (person.class, 1);  Console output update person set name=?   Age=?     Sex=? where id=?

This behavior occurs because the object is persisted through update, and the object is not put into a snapshot by the Get method.

When the object commits, the Nineth step is executed: The object is compared to the objects in the snapshot, and if it is not the same, the UPDATE statement is executed; The UPDATE statement is executed without a snapshot

Hibernate Learning (iv) Hibernate cache & Snapshot

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.