First-level cache, level two cache, and lazy loading in hibernate

Source: Internet
Author: User
Tags sessions

1. Why Use Caching

Hibernate uses caching to reduce the number of accesses to the database, which improves hibernate execution efficiency. There are two types of caches in Hibernate: first-level cache and level two cache.

2. First level cache

Hibenate cache, also known as the session cache, when invoking the session's Save/saveorupdate/get/load/list/iterator method, will put the object in the session cache.

The first level cache can reduce the number of accesses to the database within the session range, only valid in session scope, session closed, and primary cache invalidation.

The session cache is maintained by hibernate, the user cannot manipulate the cached content, and if you want to manipulate the cache content, you must do so through the Evit/clear method provided by Hibernate.

Characteristics:

Only in the current session range is valid, the action time is short, the effect is not particularly obvious!

In a short period of time to operate the database, the effect is more obvious!

The difference between list and iterator

List

Check out all the records at once.

will be placed in the cache, but will not fetch data from the cache

Iterator:

n+1 query; n indicates the total number of records, that is, a statement is sent to query the primary key (1) of all records, and then the database query based on each primary key (n)

will be put into the cache, and the data will be taken from the cache

 Public void test5 ()throws  exception{        = sf.opensession ();        Session.begintransaction ();         New User ();        User.setusername ("Lin Daiyu");        Session.save (user);        User.setusername ("ka Bao");        Session.save (user);        Session.gettransaction (). commit ();        Session.close ();    }

Because of the first-level caching, the user object is saved only once.

3. Level Two cache

Hibernate provides application-level caching that can access cached data across multiple sessions, that is, different sessions of the session. This cache is also called a level two cache.

Hibernate provides a two level cache with a default implementation and is a pluggable caching framework! If the user wants to use level two cache, only need to configure in Hibernate.cfg.xml, do not want to use, directly remove, do not affect the code. If the user feels that hibernate provides a frame frame that is not good enough, you can switch to another cache framework or implement your own caching framework.

Turn on level two caching:

The list () default is only placed in the cache, not from the primary cache, the query cache is configured, and a list () query can be used to fetch data from the level two cache.

<!--turn on level two caching -        < Propertyname= "Hibernate.cache.use_second_level_cache">True</ Property>        <!--Specifies the cache framework used -        < Propertyname= "Hibernate.cache.provider_class">Org.hibernate.cache.HashtableCacheProvider</ Property>        <!--turn on query caching -        < Propertyname= "Hibernate.cache.use_query_cache">True</ Property>

Specify a class that requires a level two cache:

If the collection cache is set, the element object to which the collection belongs is also placed in level two cache, which is employee.

        <!--specify which classes need to be added to level two cache -        <Class-cacheclass= "Com.juaner.department.Employee"usage= "Read-only"/>        <Class-cacheclass= "Com.juaner.department.Dept"usage= "Read-only"/>        <!--collection cache, the type to which the collection belongs is also placed in level two cache -        <Collection-cacheCollection= "Com.juaner.department.Dept.emps"usage= "Read-only"/>

Use Level Two caching:

If the query cache is set, setcacheable (true) needs to be set manually.

@Test Public voidTest1 () {Session session=sf.opensession ();        Session.begintransaction (); //setcacheable specified from the level two cache, or into a level two cache, for list does not take data from the primary cache//read the data from the cache, the query criteria must be consistent//caching mechanism for map< conditions, results >Query query = Session.createquery ("from Dept"). Setcacheable (true);        System.out.println (Query.list ());        Session.gettransaction (). commit ();        Session.close (); Session Session1=sf.opensession ();        Session1.begintransaction (); Query= Session1.createquery ("from Dept"). Setcacheable (true);        System.out.println (Query.list ());        Session1.gettransaction (). commit ();    Session1.close (); }

4.get and load

Get: Load in a timely manner, just call the Get method to query the database immediately

Load: Lazy loading by default, queries to the database when data is used

5. Lazy Loading

When the data is used to query the database, this is Hibernate lazy loading feature.

Lazy value

True Use lazy loading

False off Lazy loading

Extra when the collection data lazy loading time to improve efficiency, when the actual use of data to send queries to the database SQL, if the call collection of size ()/isempty () method, just statistics, not really query data!

First-level cache, level two cache, and lazy loading in hibernate (GO)

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.