search
HomeBackend DevelopmentC#.Net TutorialRegarding hibernate cache issues:
Regarding hibernate cache issues:Dec 20, 2016 pm 02:19 PM
hibernate

1. About hibernate caching:
1.1.1. Basic caching principle
Hibernate cache is divided into two levels. The first level is stored in the session and is called the first level cache. It is included by default and cannot be uninstalled.

The second level is the process-level cache controlled by sessionFactory. It is a globally shared cache, and any query method that calls the second-level cache will benefit from it. Second-level cache will only work if it is configured correctly. At the same time, the corresponding method must be used to obtain data from the cache when performing conditional queries. For example, Query.iterate() method, load, get method, etc. It must be noted that the session.find method always obtains data from the database and will not obtain data from the second-level cache, even if it contains the required data.

The implementation process of using cache when querying is: first query whether the required data is available in the first-level cache. If not, query the second-level cache. If it is not found in the second-level cache, then query the database. It should be noted that the query speed of these three methods decreases in sequence.
1.2. Existing problems
1.2.1. Problems with the first-level cache and the reasons for using the second-level cache
Because the lifetime of the Session is often very short, the lifetime of the fastest first-level cache that exists inside the Session is also very short. Short, so the hit rate of the first level cache is very low. Its improvement in system performance is also very limited. Of course, the main function of this Session internal cache is to keep the Session's internal data state synchronized. It is not provided by hibernate to greatly improve system performance.
In order to improve the performance of using hibernate, in addition to some conventional methods that need attention, such as:
using lazy loading, urgent external connections, query filtering, etc., you also need to configure hibernate's second-level cache. Its improvement in overall system performance often has immediate results!
(Through my experience in previous projects, there will generally be a 3~4 times performance improvement)

1.2.2. The problem of N+1 queries
When executing conditional queries, the iterate() method has the famous "n+ 1" query problem, that is to say, in the first query, the iterate method will execute the number of query results that meet the conditions plus one (n+1) query. However, this problem only exists during the first query, and the performance will be greatly improved when executing the same query later. This method is suitable for querying business data with large amounts of data.
But note: When the amount of data is particularly large (such as pipeline data, etc.), you need to configure its specific cache strategy for this persistent object, such as setting the maximum number of records that exist in the cache, the cache existence time and other parameters to avoid The system loads a large amount of data into the memory at the same time, causing rapid exhaustion of memory resources, which in turn reduces system performance! ! !

1.3. Other considerations for using hibernate’s second-level cache:
1.3.1. About the validity of data
In addition, hibernate will maintain the data in the second-level cache by itself to ensure that the data in the cache and the real data in the database are The consistency! Whenever you pass an object by calling the save(), update(), or saveOrUpdate() methods, or obtain an object using the load(), get(), list(), iterate(), or scroll() methods , the object will be added to the Session's internal cache. When the flush() method is subsequently called, the state of the object is synchronized with the database.

That is to say, when deleting, updating, or adding data, the cache is updated at the same time. Of course this also includes L2 cache!

As long as the hibernate API is called to perform database-related work. Hibernate will automatically ensure the validity of cached data for you! !

However, if you use JDBC to bypass hibernate and directly perform operations on the database. At this time, Hibernate will not/cannot sense the changes made to the database on its own, and it can no longer guarantee the validity of the data in the cache! !

This is also a problem common to all ORM products. Fortunately, Hibernate exposes the Cache clearing method for us, which provides us with an opportunity to manually ensure data validity! !
The first-level cache and the second-level cache have corresponding clearing methods.缓 The method of removal provided by the secondary cache is:
Press the object CLASS empty cache
Press the object CLASS and the primary key ID of the object to clear the cache data in the set of the object of the object.

1.3.2. Suitable situations for use
Not all situations are suitable for using second-level cache, and it needs to be decided according to the specific situation. At the same time, you can configure a specific cache strategy for a persistent object.

Suitable for the use of second-level cache:
1. The data will not be modified by a third party;

Generally, it is best not to configure the second-level cache for data that will be modified by other than hibernate to avoid causing inconsistent data. However, if this data needs to be cached for performance reasons and may be modified by a third party such as SQL, you can also configure a secondary cache for it. It's just that at this time, you need to manually call the cache clearing method after the SQL is modified. To ensure data consistency

2. The data size is within the acceptable range;

If the amount of data in the data table is particularly large, it is not suitable for secondary cache at this time. The reason is that too much cached data may cause memory resource constraints, which in turn reduces performance.

If the amount of data in the data table is particularly huge, only the newer part of the data is often used. At this time, you can also configure a second-level cache for it. However, the caching strategy of its persistence class must be configured separately, such as the maximum number of caches, cache expiration time, etc., and these parameters should be reduced to a reasonable range (too high will cause memory resource constraints, and if it is too low, caching will be of little significance).

3. The data update frequency is low;

For data with too high data update frequency, the cost of frequently synchronizing the data in the cache may be equivalent to the benefits obtained from querying the data in the cache, and the disadvantages and benefits are offset. Caching is of little significance at this time.


4. Non-critical data (not financial data, etc.)

Financial data, etc. are very important data, and invalid data is absolutely not allowed to appear or be used, so it is best not to use the second-level cache for security reasons at this time.
Because at this time the importance of "correctness" is far greater than the importance of "high performance".

2. Recommendations for using hibernate cache in the current system
1.4. Current situation
There are three situations in general systems that will bypass hibernate to perform database operations:

1. Multiple application systems access a database at the same time
In this case, use hibernate Second-level cache will inevitably cause data inconsistency.
At this time, detailed design is required. For example, avoid simultaneous write operations to the same data table in the design, use various levels of locking mechanisms in the database, etc.

2. Dynamic table related

The so-called "dynamic table" refers to a data table that is automatically created according to the user's operating system when the system is running.

For example, "custom form" and other functional modules that belong to the nature of user-defined extension development, because the data table is created at runtime, hibernate mapping cannot be performed. Therefore, the operation on it can only be direct database JDBC operation that bypasses hibernate.

If the data in the dynamic table is not cached at this time, there will be no data inconsistency problem.

If you design your own cache mechanism at this time, just call your own cache synchronization method.对3. When using SQL to delete the Hibernate persistence object table in batches, after performing batch deletions at this time, there will be deleted data in the cache.

Analysis:

When Article 3 (sql batch deletion) is executed, subsequent queries can only be in the following three ways:

a. session.find() method:
According to the previous summary, the find method will not query the second query. level cached data, but directly queries the database.

So there is no problem of data validity.

b. When calling the iterate method to execute a conditional query:
According to the execution method of the iterate query method, it will query the database for the id value that meets the conditions every time, and then obtain the data from the cache based on this id. When there is no such id in the cache, Only the data of id will be executed in the database query;
If this record has been deleted directly by sql, iterate will not query the id when executing the id query. Therefore, even if this record exists in the cache, it will not be obtained by the customer, and there will be no inconsistency. (This situation has been verified by testing)

c. Use the get or load method to execute the query by id:

Objectively, expired data will be queried at this time. But because the SQL batch deletion in the system is generally

for intermediate related data tables, for

The query of the intermediate association table generally uses conditional query. The probability of querying a certain association relationship by id is very low, so this problem does not exist!

If a value object really needs to query an association relationship by id, at the same time And because of the large amount of data, SQL is used to perform batch deletion. When these two conditions are met, in order to ensure that the query by id gets the correct result, you can use the method of manually clearing the data of this object in the second-level cache!!
(This situation is less likely to occur)

1.5 . Recommendations
1. It is recommended not to use SQL to directly update the data of the data persistence object, but batch deletion can be performed. (There are also fewer places in the system that require batch updates)

2. If you must use SQL to update data, the cached data of this object must be cleared. Call
SessionFactory.evict(class)
SessionFactory.evict(class,id)
and other methods.

3. When the amount of batch deletion data is not large, you can directly use hibernate's batch deletion, so that there is no need to bypass the cache data consistency problem caused by hibernate executing SQL.

4. It is not recommended to use hibernate’s batch deletion method to delete large batches of record data.
The reason is that hibernate's batch deletion will execute 1 query statement plus n deletion statements that meet the conditions. Instead of executing one conditional delete statement at a time! !
When there is a lot of data to be deleted, there will be a huge performance bottleneck! ! ! If the amount of data to be deleted in batches is large, for example, more than 50 items, JDBC can be used to delete them directly. The advantage of this is that only one SQL delete statement is executed, and the performance will be greatly improved. At the same time, for the problem of cache data synchronization, hibernate can be used to clear the relevant data in the second-level cache.
Call SessionFactory.evict(class); SessionFactory.evict(class,id) and other methods.

So, for general application system development (not involving clusters, distributed data synchronization issues, etc.), sql execution is only called when batch deletion of the intermediate association table is performed, and the intermediate association table is generally the execution condition The query is unlikely to perform a query by id. Therefore, you can directly perform sql deletion at this time without even calling the cache clearing method. Doing so will not cause data validity problems caused by configuring the second-level cache in the future.

Taking a step back, even if the method of querying the intermediate table object by ID is actually called in the future, it can be solved by calling the method of clearing the cache.

4. Specific configuration method
According to what I know, many hibernate users superstitiously believe that "hibernate will handle performance issues for us on its own" or "hibernate will automatically handle performance issues for us" when calling its corresponding methods. "All operations call cache". The actual situation is that although hibernate provides us with a good caching mechanism and support for extended caching framework, it must be called correctly before it can work! ! Therefore, the performance problems caused by many systems using hibernate are actually not caused by hibernate being ineffective or bad, but because users do not correctly understand how to use it. On the contrary, if configured properly, hibernate's performance will make you quite "surprised". Below I will explain the specific configuration method.

ibernate provides a second-level cache interface:
net.sf.hibernate.cache.Provider,
It also provides a default implementation of net.sf.hibernate.cache.HashtableCacheProvider,
also You can configure other implementations such as ehcache, jbosscache, etc.

The specific configuration location is in the hibernate.cfg.xml file
true
net .sf.hibernate.cache.HashtableCacheProvider

Many hibernate users think that they are done when configuring this step.
Note: In fact, with this configuration, hibernate's second-level cache is not used at all. At the same time, because most of the time they close the session immediately when using hibernate, the first-level cache does not play any role. The result is that no cache is used, and all hibernate operations are performed directly on the database! ! The performance is as expected.

The correct way is that in addition to the above configuration, you should also configure the specific cache strategy of each vo object and configure it in the mapping file. For example:










The key is this , which has several options
read-only, read-write, transactional, etc. Then pay attention when executing the query. If it is a conditional query, or a query that returns all results, the session.find() method will not obtain the data in the cache. The cached data will only be adjusted when the query.iterate() method is called.

At the same time, the get and load methods will query the data in the cache.

The specific configuration methods for different cache frameworks will be different, but generally the above configuration

(In addition, for those that support transactions and clusters I will try to publish the configuration of the environment in a follow-up article)

3. Summary

In short, hibernate is effectively configured and used correctly according to different business situations and project situations, so as to maximize its strengths and avoid weaknesses. There is no one-size-fits-all solution for every situation.


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
SpringBoot项目里怎么集成HibernateSpringBoot项目里怎么集成HibernateMay 18, 2023 am 09:49 AM

在SpringBoot项目中集成Hibernate前言Hibernate是一个流行的ORM(对象关系映射)框架,它可以将Java对象映射到数据库表,从而方便地进行持久化操作。在SpringBoot项目中,集成Hibernate可以帮助我们更轻松地进行数据库操作,本文将介绍如何在SpringBoot项目中集成Hibernate,并提供相应的示例。1.引入依赖在pom.xml文件中引入以下依赖:org.springframework.bootspring-boot-starter-data-jpam

Java错误:Hibernate错误,如何处理和避免Java错误:Hibernate错误,如何处理和避免Jun 25, 2023 am 09:09 AM

Java是一种面向对象编程语言,它被广泛地应用于软件开发领域。Hibernate是一种流行的Java持久化框架,它提供了一种简单且高效的方式来管理Java对象的持久化。然而,开发过程中经常会遇到Hibernate错误,这些错误可能会导致程序的异常终止或者不稳定。如何处理和避免Hibernate错误成为了Java开发者必须掌握的能力。本文将介绍一些常见的Hib

hibernate和mybatis有哪些区别hibernate和mybatis有哪些区别Jan 03, 2024 pm 03:35 PM

hibernate和mybatis的区别:1、实现方式;2、性能;3、对象管理的对比;4、缓存机制。详细介绍:1、实现方式,Hibernate是一个完整的对象/关系映射解决方案,将对象与数据库表进行映射,MyBatis则需要开发者手动编写SQL语句以及ResultMap;2、性能,Hibernate在开发速度上可能比MyBatis快,因为Hibernate简化了DAO层等等。

Java Hibernate中一对多和多对多关系的映射方式是什么Java Hibernate中一对多和多对多关系的映射方式是什么May 27, 2023 pm 05:06 PM

Hibernate的一对多和多对多Hibernate是一个优秀的ORM框架,它简化了Java应用程序与关系型数据库之间的数据访问。在Hibernate中,我们可以使用一对多和多对多的关系来处理复杂的数据模型。Hibernate的一对多在Hibernate中,一对多关系是指一个实体类对应多个另一个实体类。比如,一个订单(Order)可以对应多个订单项(OrderItem),一个用户(User)可以对应多个订单(Order)。要在Hibernate中实现一对多关系,需要在实体类中定义一个集合属性来存

如何在Hibernate中执行批量插入更新操作?如何在Hibernate中执行批量插入更新操作?Aug 27, 2023 pm 11:17 PM

在本文中,我们将看到如何在Hibernate中执行批量插入/更新。每当我们执行一条sql语句时,我们都是通过对数据库进行网络调用来完成的。现在,如果我们必须向数据库表中插入10个条目,那么我们必须进行10次网络调用。相反,我们可以通过使用批处理来优化网络调用。批处理允许我们在单个网络调用中执行一组SQL语句。为了理解和实施这一点,让我们定义我们的实体−@EntitypublicclassParent{@Id@GeneratedValue(strategy=GenerationType.AUTO)

Java语言中的Hibernate框架介绍Java语言中的Hibernate框架介绍Jun 10, 2023 am 11:35 AM

Hibernate是一个开放源码的ORM框架,它将关系型数据库和Java程序之间的数据映射相互绑定,这样可以使开发人员更方便地访问数据库中的数据。使用Hibernate框架可以大量减少编写SQL语句的工作,提高应用程序的开发效率和可重用性。下面从以下几个方面来介绍Hibernate框架。一、Hibernate框架的优点对象关系映射,隐藏数据库访问细节,使开发

深入了解Java框架技术栈:探索Spring MVC、Hibernate、MyBatis等常用Java框架深入了解Java框架技术栈:探索Spring MVC、Hibernate、MyBatis等常用Java框架Dec 26, 2023 pm 12:50 PM

Java框架技术栈:介绍常用的Java框架,如SpringMVC、Hibernate、MyBatis等随着Java的不断发展,越来越多的框架被开发出来以简化开发过程。其中,SpringMVC、Hibernate、MyBatis等是Java开发中最常用的框架之一。本文将介绍这些框架的基本概念和使用方法,帮助读者更好地理解和应用这些框架。第一,我们来介绍Sp

Hibernate二级缓存是如何工作的?Hibernate二级缓存是如何工作的?Sep 14, 2023 pm 07:45 PM

缓存有助于减少执行查询时的数据库网络调用。一级缓存与会话链接。它是隐式实现的。一级缓存存在直到会话对象存在为止。一旦会话对象终止/关闭,将会有没有缓存对象。二级缓存适用于多个会话对象。它是链接的与会话工厂。二级缓存对象可供所有会话使用单会话工厂。当特定会话发生时,这些缓存对象将被终止工厂已关闭。实现二级缓存我们需要添加以下依赖项才能使用二级缓存。<!--https://mvnrepository.com/artifact/net.sf.ehcache/ehcache--><de

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.