Hibernate & lt; query cache & gt; and hibernate query Cache

Source: Internet
Author: User

Hibernate <query cache> and hibernate query Cache

Query cache:

Definition: the query cache is based on the second-level cache. It can save the query results of common attributes. When querying an object, it saves the id as the key and the query result as the value, when accessing the next object, you can directly find

When querying object objects in the cache, it is particularly characteristic that multiple query id query statements are executed.

Lifecycle: When the associated table is modified, the lifecycle of the query cache ends.

Query cache configuration and usage:

1. Modify hibernate. cfg

1 <! -- When the query cache is enabled, the default value is false. --> 2 <property name = "hibernate. cache. use_query_cache"> false </property>

2. The associated object class has the same configuration as the second-level cache.

3. Enable in the program:

Query. setCacheable (true );

Enable query cache and disable secondary query:

 1 public void seleCacheTest(){ 2         try { 3             session=HibernateUtil.currentSession(); 4             session.beginTransaction(); 5             List grade=session.createQuery("select g from Grade g") 6                                 .setCacheable(true) 7                                 .list(); 8             for(int i=0;i<grade.size();i++){ 9                 Grade grad=(Grade)grade.get(i);10                 System.out.println(grad.getGradename());11             }12             13         } catch (Exception e) {14             // TODO: handle exception15         }finally{16             HibernateUtil.closeSession();17             18         }19         System.out.println("-------------");20         try {21             session=HibernateUtil.currentSession();22             session.beginTransaction();23             List grade=session.createQuery("select g from Grade g")24                                 .setCacheable(true)25                                 .list();26             for(int i=0;i<grade.size();i++){27                 Grade grad=(Grade)grade.get(i);28                 System.out.println(grad.getGradename());29             }30             31         } catch (Exception e) {32             // TODO: handle exception33         }finally{34             HibernateUtil.closeSession();35             36         }37         38     }

 

Summary:

Itetator does not support query Cache

When the list function enables the query cache, the second-level cache is disabled:

In different sessions: During the second query, n query IDs are sent.

In the same session, because of the role of the first-level cache, no matter whether or not the query cache is enabled, there is only one query statement

List:

Only one query statement is executed because the second-level cache takes effect.

 

Note the following in cache knowledge points:

The cache cannot be used at will. Try not to use it!

When using the cache, you also need to consider the dirty data issue!

Cache features at all levels:

Cache is generated during level-1 caching, such as save, get, load, and query, which cannot be disabled.

Second-level cache, second-level cache cross-session operations, good performance, but cannot query common attributes

Query cache. It is stored as a key-Value Pair and must be created in the program.

        

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.