Detailed explanation of caching technology in Hibernate Framework

This paper describes the caching technology in Hibernate framework. Share with you for your reference, as follows:

The cache of Hibernate framework is divided into session cache and sessionfactory cache, also known as L1 cache and L2 cache.

L1 cache:

The first level cache is a session level cache. Its life cycle is very short. It corresponds to the session and is managed by hibernate. It belongs to the transaction scope cache. When the program calls the load () method, get () method, save () method, saveOrUpdate () method, update () method or query interface method of session, hibernate will cache the entity object; When querying the entity object through the load () method or get () method, hibernate will query in the cache first. When the entity object cannot be found, hibernate will issue SQL statements to query in the database, which improves the use efficiency of hibernate.

For example:

When the program checks the user object for the first time through the get () method, hibernate will issue an SQL statement to query. At this time, hibernate caches its user object at the first level; When querying through the get () method again, hibernate will not issue SQL statements because the user name already exists in the first level cache. Program running results:

Note: the lifecycle of L1 cache corresponds to sessions. It will not be shared between sessions. Entity objects cached in other sessions cannot be obtained in different sessions

L2 cache:

L2 cache is a sessionfactory level cache, and its life cycle is consistent with that of sessionfactory. L2 cache can be shared among multiple sessions and belongs to process wide or cluster wide cache.

L2 cache is a pluggable cache plug-in. Its use needs the support of third-party cache products. In the Hibernate framework, the usage policy of L2 cache is configured through hibernate configuration file.

1. Add the cache configuration file ehcache xml

2. Set hibernate configuration file.

Example:

The L2 cache is shared between sessions, so the same object can be loaded in different sessions. Hibernate will issue only one SQL statement. When the object is loaded the second time, hibernate will get the object from the cache.

Program results:

For L2 cache, some infrequently updated data or reference data can be used, and its performance will be significantly improved. However, if L2 cache is applied to frequently changing data, there will be some problems in performance.

I hope this article will be helpful to your Java programming based on Hibernate framework.

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
分享
二维码
< <上一篇
下一篇>>