Detailed interpretation of Hibernate cache mechanism

1、 Why (why use hibernate cache?)

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

In order to reduce the frequency of applications accessing physical data sources, so as to improve the running performance of applications.

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 run time. The data in the cache and the physical data source will be synchronized at a specific time or event.

2、 What is the principle of Hibernate cache? Hibernate cache includes two categories: Hibernate L1 cache and Hibernate L2 cache.

1. Hibernate L1 cache is also called "session cache".

The session built-in cannot be unloaded. The session cache is a transaction wide cache (the life cycle of the session object usually corresponds to a database transaction or an application transaction).

In the L1 cache, each instance of the persistent class has a unique oid.

2. Hibernate L2 cache is also called "session factory cache".

Since the life cycle of the sessionfactory object corresponds to the whole process of the application, hibernate L2 cache is a process wide or cluster wide cache, which may have concurrency problems. Therefore, it is necessary to adopt an appropriate concurrent access strategy, which provides a transaction isolation level for the cached data.

The second level cache is optional and is a configurable plug-in. By default, sessionfactory will not enable this plug-in.

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

What kind of data is suitable for storage in the second level cache?   

1) Rarely modified data

2) Data that is not very important allows occasional concurrent data

3) Data that will not be accessed concurrently

4) Constant data

Data not suitable for storage in the second level cache?   

1) Frequently modified data

2) Concurrent access to data, such as financial data, is absolutely not allowed

3) Data shared with other applications.

3. The delay loading implementation of session needs to solve two problems: closing the connection normally and ensuring that the same session is accessed in the request.

Hibernate session is Java sql. A layer of high-level encapsulation of connection. A session corresponds to a connection.

Close the session correctly after the HTTP request (the filter realizes the normal closing of the session); the delayed loading must ensure that it is the same session (the session is bound to ThreadLocal).

4. How does hibernate find objects and apply caching?

When hibernate accesses a data object according to its ID, it first looks it up from the session level-1 cache; No, if L2 cache is configured, query from L2 cache; If none can be found, query the database again, put the results into the cache according to the ID, and update the cache when deleting, updating and adding data.

5. Comparison between L1 cache and L2 cache.

3、 How (how to apply hibernate caching mechanism?)

1. L1 cache management:

Evit (object obj) clears the specified persistent object from the first level cache, frees the memory resources occupied by the object, and changes the specified object from the persistent state to the unmanaged state, thus becoming a free object.

Clear() clears all persistent objects in the L1 cache to release the memory resources occupied by them.

Contains (object obj) determines whether the specified object exists in the L1 cache.

Flush () flushes the contents of the L1 cache to keep it synchronized with the database data.

2. L1 cache application: save (). When the session object calls the save () method to save an object, the object will be put into the session cache. Get() and load(). When the session object calls the get () or load () method to fetch an object from the database, the object will also be put into the session cache. Query data from the database using HQL, QBC, etc.

Only one select SQL statement is included in the output result, and customer1 = = customer2 result is true indicates that the two extracted objects are the same object. The principle is: the first time you call the get () method, hibernate first retrieves whether the lookup object is in the cache. If it is found that it is not, hibernate sends a select statement to the database to take out the corresponding object, and then puts the object into the cache for next use. The second time you call the get () method, hibernate first retrieves whether the lookup object is in the cache, If the search object is found, it will be taken out of the cache and will not be retrieved from the database.

3. L2 cache management:

Evict (class arg0, serializable arg1) clears the persistent object with the specified ID of a class from the L2 cache to free the resources occupied by the object.

sessionFactory. evict(Customer.class,new Integer(1));

Evict (class arg0) clears all persistent objects of the specified class from the L2 cache to free the memory resources occupied by them.

sessionFactory. evict(Customer.class);

Evictcollection (string arg0) clears the specified collection of all persistent objects of the specified class from the L2 cache to free the memory resources occupied by it.

sessionFactory. evictCollection("Customer.orders");

4. L2 cache configuration

Common L2 cache plug-ins

EHCache org. hibernate. cache. EhCacheProvider OSCache org. hibernate. cache. OSCacheProvider SwarmCahe org. hibernate. cache. SwarmCacheProvider JBossCache org. hibernate. cache. TreeCacheProvider

If there is a one to many relationship, if you want to cache the associated multiple parties when obtaining one party, you need to add a < cache > sub tag under the collection attribute. Here, you need to add a < cache > tag under the < class > tag in the HBM file of the associated object, otherwise hibernate will only cache oid.

summary

The above is all about the detailed interpretation of Hibernate's caching mechanism in this paper. I hope it will be helpful to you. Interested friends can refer to: quickly understand hibernate configuration file and mapping file, hibernate implementation of pessimistic lock and optimistic lock code introduction, hibernate core idea and interface introduction, etc. you can leave a message if you have any questions. Welcome to exchange and discuss.

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