Level 1 cache, level 2 cache, and lazy loading in Hibernate, and level 1 cache in hibernate

Source: Internet
Author: User

Level 1 cache, level 2 cache, and lazy loading (transfer) in Hibernate, and level 1 cache in hibernate
1. Why cache?

Hibernate uses cache to reduce the number of accesses to the database, thus improving the execution efficiency of hibernate. There are two types of cache in hibernate: level-1 cache and level-2 cache.

2. Level 1 Cache

The level-1 cache in Hibenate is also called the session cache. When the save/saveOrUpdate/get/load/list/iterator method of the session is called, the object will be put into the session cache.

The first-level cache can reduce the number of database accesses within the session range. It is only valid within the session range, and the session is closed. The first-level cache is invalid.

The session cache is maintained by hibernate, and users cannot operate on the cached content. to operate on the cached content, you must use the evit/clear method provided by hibernate.

Features:

It is valid only in the current session range and has a short effect. The effect is not particularly obvious!

The database has been operated many times in a short time, and the effect is obvious!

Differences between list and iterator

List:

Query all records at a time.

Will be put into the cache, but will not get data from the cache

Iterator:

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

Will be placed in the cache, and will also get data from the cache

Public void test5 () throws Exception {Session session = sf. openSession (); session. beginTransaction (); User user = new User (); user. setUserName ("Lin Daiyu"); session. save (user); user. setUserName (""); session. save (user); session. getTransaction (). commit (); session. close ();}

 

Because of the primary cache function, the user object is saved only once.

3. Secondary Cache

Hibernate provides an application-level cache that can access cached data across multiple sessions, that is, different sessions. This cache is also called a second-level cache.

The second-level cache provided by Hibernate is implemented by default and is a plug-and-play cache framework! If you want to use the second-level cache, you only need to configure it in hibernate. cfg. xml. If you do not want to use it, remove it directly without affecting the code. If you think that the framework provided by hibernate is not easy to use, you can change to another cache framework or implement the cache framework yourself.

Enable Level 2 Cache:

By default, list () is stored only in the cache and is not retrieved from the first-level cache. You can configure the query cache so that list () queries can retrieve data from the second-level cache.

<! -- Enable Level 2 cache --> <property name = "hibernate. cache. use_second_level_cache"> true </property> <! -- Specify the cache framework used --> <property name = "hibernate. cache. provider_class"> org. hibernate. cache. HashtableCacheProvider </property> <! -- Enable query cache --> <property name = "hibernate. cache. use_query_cache"> true </property>

 

Specify the class that requires second-level cache:

If the set cache is set, the element object to which the set belongs should also be placed in the second-level cache, that is, Employee.

<! -- Specify which classes need to be added to the second-level cache --> <class-cache class = "com. juaner. department. employee "usage =" read-only "/> <class-cache class =" com. juaner. department. dept "usage =" read-only "/> <! -- Set cache. the type of the set must also be placed in the second-level cache --> <collection-cache collection = "com. juaner. department. dept. emps "usage =" read-only "/>

 

Use Level 2 Cache:

If the query cache is set, you need to manually set setCacheable (true ).

@ Test public void test1 () {Session session = sf. openSession (); session. beginTransaction (); // setCacheable indicates to find data from the second-level cache or put it into the second-level cache. The list does not retrieve data from the first-level cache // read data from the cache, the Query conditions must be consistent // The cache mechanism is Map <condition, result> 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 time. You only need to call the get method to query the database immediately.

Load: by default, lazy loading is used to query data from the database.

5. Lazy Loading

It only queries the database when data is used. This is the lazy loading feature of hibernate.

Lazy Value

True

False: Disable lazy loading.

Extra improves efficiency when the set data is loaded in a lazy manner and sends the query SQL statement to the database only when the data is actually used. If the size ()/isEmpty () method of the set is called, only statistics are collected, not really query data!

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.