search
HomeJavajavaTutorialIntroduction to hibernate caching mechanism

Introduction to hibernate caching mechanism

Jul 17, 2017 pm 05:52 PM
hibernatemechanismcache

hibernate first-level cache

1.Hibernate first-level cache is also called "Session cache" and "session-level cache".

2. When querying an entity from the database through Session, the entity will be stored in the memory. The next time you query the same entity, it will no longer be obtained from the database, but from the memory. This is cache

3. The life cycle of the first-level cache is the same as that of the Session. When the Session is destroyed, it is also destroyed.

4. The applicable range of data in the first-level cache is within the current session.

why (Why use Hibernate cache?)

Hibernate is a persistence layer framework that often accesses physical databases .

In order to reduce the frequency of application access to physical data sources, thereby improving the running performance of the application.

The data in the cache is a copy of the data in the physical data source. The application reads and writes data from the cache at runtime. At a specific moment or event, the data in the cache and the physical data source will be synchronized


#what( What is the principle of Hibernate cache? ) Hibernate cache includes two categories: Hibernate first-level cache and Hibernate second-level cache.

1.Hibernate first-level cache is also called "Session cache".

Session built-in cannot be unloaded, and the Session cache is a transaction-scope cache (the life cycle of the Session object usually corresponds to a database transaction or an application transaction).

In the first-level cache, each instance of the persistent class has a unique OID.

2.Hibernate second-level cache is also called "SessionFactory cache".

Since the life cycle of the SessionFactory object corresponds to the entire process of the application, the Hibernate second-level cache is a process-wide or cluster-wide cache. Concurrency problems may occur, so an appropriate concurrent access strategy needs to be adopted. Policies provide transaction isolation levels for cached data.

The second level cache is optional and is a configurable plug-in. SessionFactory will not enable this plug-in by default.

Hibernate provides the org.hibernate.cache.CacheProvider interface, which acts as an adapter between the cache plug-in and Hibernate.

What kind of data is suitable to be stored in the second level cache?

1) Data that is rarely modified
2) Data that is not very important, occasional concurrent data is allowed
3) Data that will not be accessed concurrently
4) Constant data
Data that is not suitable for storage in the second level cache?
1) Data that is frequently modified
2) Data that is never allowed to be accessed concurrently, such as financial data, is never allowed to be accessed concurrently
3) Data shared with other applications.


API for managing the first-level cache

1.evict(), used to clear an object from the Session’s first-level cache.

2.clear(), used to clear all objects in the first-level cache.

Qurey.list() and Qurey.iterate()

1.Qurey.list() query data , will not search from the first-level cache, directly send the SQL statement to the database, and retain the object returned by the query in the cache.

2.Qurey.iterate() does not search from the first-level cache, but directly sends sql to the database to query the id. When other attributes of the object need to be used, the object is first searched for in the cache according to the id. If there is no Then send a sql query to the database, so using this method alone to query will cause an N+1 problem (that is, sending N+1 statements to the database to query the information of N objects)

3. If this requirement exists: The same object needs to be accessed in two different sessions. Two sql statements or more need to be sent through Qurey.list(). In order to avoid setting up a second-level cache, Query.list() in the first session , in the second session, directly obtain it from the second-level cache through Qurey.iterate() iteration

hibernate second-level cache (SessionFactory cache)

1. Add the configuration in hibernate.cfg.xml

        <!-- 开启二级缓存 --><property>true</property><!-- 二级缓存的提供类 在hibernate4.0版本以后我们都是配置这个属性来指定二级缓存的提供类--><property>org.hibernate.cache.ehcache.EhCacheRegionFactory</property><!-- 二级缓存配置文件的位置 --><property>ehcache.xml</property>
2. For details about the configuration of ehcache.xml, see


3. Configure the entity through annotations, and add @Cache(usage=CacheConcurrencyStrategy.READ_ONLY) in front of the class

Common CacheConcurrencyStrategy attribute values ​​​​are READ_ONLY (the table data corresponding to the entity is only read and then cached) and READ-WIRTE (table data can be read or updated)

4, be sure to note: hibernate's second-level cache must be the entire object. If only some attributes of the object are queried, then the object will not Cached

Query cache (SessionFactory level cache)

1. Continue based on the above second-level cache Configuration

2. Add configuration

  <property>true</property>
in hibernate.cfg.xml

2. Add @Cacheable to the entity annotation

3. Call the setCacheable(true) method after the hql statement

4. Only when the HQL query statement is exactly the same, even the parameters The settings must be the same, and the query cache is effective at this time

hibernate's three states (transient (transient state), persistent (persistent state) and detached ( Offline))


The above is the detailed content of Introduction to hibernate caching mechanism. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools