Hibernate cache summary

1. Why use Hibernate cache?

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

In order to reduce the frequency of the application accessing the physical data source, 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, and synchronizes the data in the cache and the physical data source at a specific moment or event.

 

Second, what is the principle of Hibernate caching?

Hibernate cache includes two categories: Hibernate first-level cache and Hibernate second-level cache.

1.Hibernate first level cache is also known as "Session cache".

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

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


2.Hibernate secondary cache is also known as "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, and concurrency problems may occur. Therefore, an appropriate concurrent access strategy needs to be adopted, which is the cached data. Transaction isolation levels are provided.

The second level cache is optional and is a configurable plugin that is not enabled by the SessionFactory by default.

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

What kind of data is suitable for storing in the second level cache?   
1) Data that is rarely modified   
2) Data that is not very important, allowing occasional concurrent data   
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 with concurrent access is absolutely not allowed, such as financial data, and   
3) Data shared with other applications is absolutely not allowed.

  

3. How does Hibernate lookup objects apply caching?
When Hibernate accesses the data object according to the ID, it first checks from the Session first-level cache;

If it is not found, if the second-level cache is configured, then check it from the second-level cache;

If you can't find it, query the database again, and put the results into the cache according to the ID. When deleting, updating, and adding data, the cache is updated at the same time.

1. Why use Hibernate cache?

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

In order to reduce the frequency of the application accessing the physical data source, 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, and synchronizes the data in the cache and the physical data source at a specific moment or event.

 

Second, what is the principle of Hibernate caching?

Hibernate cache includes two categories: Hibernate first-level cache and Hibernate second-level cache.

1.Hibernate first level cache is also known as "Session cache".

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

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


2.Hibernate secondary cache is also known as "SessionFactory cache".

由于SessionFactory对象的生命周期和应用程序的整个过程对应,因此Hibernate二级缓存是进程范围或者集群范围的缓存,有可能出现并发问题,因此需要采用适当的并发访问策略,该策略为被缓存的数据提供了事务隔离级别。

第二级缓存是可选的,是一个可配置的插件,默认下SessionFactory不会启用这个插件。

Hibernate提供了org.hibernate.cache.CacheProvider接口,它充当缓存插件与Hibernate之间的适配器。

什么样的数据适合存放到第二级缓存中?   
1) 很少被修改的数据   
2) 不是很重要的数据,允许出现偶尔并发的数据   
3) 不会被并发访问的数据   
4) 常量数据   
不适合存放到第二级缓存的数据?   
1) 经常被修改的数据   
2) 绝对不允许出现并发访问的数据,如财务数据,绝对不允许出现并发   
3) 与其他应用共享的数据。

  

3.Hibernate查找对象如何应用缓存?
当Hibernate根据ID访问数据对象的时候,首先从Session一级缓存中查;

查不到,如果配置了二级缓存,那么从二级缓存中查;

如果都查不到,再查询数据库,把结果按照ID放入到缓存删除、更新、增加数据的时候,同时更新缓存。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326341747&siteId=291194637