Detailed analysis of Hibernate query cache

1、 Query cache configuration

1. In hibernate cfg. Add a query cache policy to XML, < propertyname = "hibernate. Cache. Use_query_cache" > true < / property > enable query cache policy. The default is false.

2、 Close the L2 cache and use query List() query common attributes

The code is shown below.

We can see that the console outputs the statement only once: Hibernate: select student0_ name as col_ 0_ 0_ fromt_ student student0_

From this, we can see that we have enabled the query cache. When we first query, we have put the results into the query cache. When we make the same query again the second time, we will no longer send duplicate SQL statements to the database.

3、 Close the L2 cache, enable the query cache, and use query List() query common attributes

The code is as follows.

The operation results are as follows.

Console print results:

select student0_. name as col_ 0_ 0_ fromt_ student student0_ Students in class 0 0 students in class 0 1 students in class 0 2 students in class 0 3 students in class 0 4 students in class 0 5

We can see that, similarly, we only print the query statement once. If the query cache is not enabled and the secondary cache is closed, we will query the database again. The reason why our program does not repeatedly query the database is that when the query cache is enabled, the life cycle of the query cache has nothing to do with the session.

4、 Close the L2 cache, open the query, and use query Iterate() query common attributes

The code is shown below.

The console displays the results and prints the SQL statement twice.

-------------------------------------------------------

According to this result, we find that quer Iterate () queries common attributes. It does not use query cache. Query cache is only used for query List () works.

5、 Close the L2 cache and query cache, using query List() query entity

The code is shown below.

The display results are as follows.

Print the SQL statement twice on the console.

Students in class 0 0 students in class 0 1 students in class 0 2 students in class 0 3 students in class 0 4

It can be seen that the query cache is not enabled, and the default is query List will issue query statements every time it executes.

6、 Turn off the L2 cache, turn on the query cache, and use query List() query entity

The code is shown below.

The console prints SQL as shown in the following figure.

During the first query, we issue an SQL statement to query the results. Because we have enabled the query cache, we will put the ID of the entity result set queried for the first time into the query cache, and execute query again for the second time List (), the ID will be taken out and found in the corresponding cache. Because it is cross session and cannot be found in the secondary cache, the query statement will be issued every time. If it does not exist in the secondary cache, the query statement will be issued as many times as the number of IDS.

7、 Enable L2 cache and query cache, using query List() query entity

The code is shown below.

The results are as follows

Only one SQL request is issued, when we execute query for the first time List () will be put into L2 cache and query cache. When we execute the query for the first time, we will find the corresponding ID in the cache. If it exists in the secondary cache, we will directly take the data from the secondary cache and no longer send SQL statements to the database.

8、 Query cache summary

Query cache caches the result set of common attributes, and caches the ID of the result set of entity objects. The life cycle of query cache. When the associated table is modified, the life cycle of query cache ends.

When the cache is enabled, we need to maintain the cache. If the cache is inconsistent with the data in memory, it is out of sync with the data, which may display dirty data to the user. So use caching mechanisms as needed.

summary

The above is the detailed analysis of Hibernate query cache introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>