Home » Java » Hibernate II cache mechanism

Hibernate II cache mechanism

Hibernate caching mechanism


One, why (why use the Hibernate cache)?
Hibernate is a persistent layer framework that frequently accesses physical databases
To reduce the frequency of access to the physical data source by the application,
This improves the application's performance
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,
At specific times or events, the data of the cache and the physical data source are synchronized with.

Two, what (what is the Hibernate caching principle?)
Hibernate cache consists of two broad categories:
Hibernate first level cache and Hibernate two level cache
The 1.Hibernate level cache is also called the Session cache
Session built in cannot be uninstalled,
The Session cache is the scope of the transaction cache
(the life cycle of a Session object typically corresponds to a database transaction or an application transaction)
In the first level cache, each instance of the persistence class has a unique OID.
The 2.Hibernate two level cache is also called the SessionFactory cache
Because of the life cycle of the SessionFactory object and the entire process of the application,
Therefore, the Hibernate two level cache is the scope of the process or the scope of the cluster cache,
Concurrency problems are possible, so appropriate concurrency access policies are needed,
This policy provides the transaction isolation level for cached data
The second level cache is optional and is a configurable plug-in,
By default, this plug-in is not enabled by SessionFactory
Hibernate provides the org.hibernate.cache.CacheProvider interface,
It acts as an adapter between the cache plug-in and the 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 important, allowing occasional concurrent data
3) data that will not be accessed concurrently
4) constant data

Data unsuitable for storage in level second cache

1) data that is constantly modified
2) absolutely do not allow concurrent access to data, such as financial data, is absolutely not allowed to occur concurrent
3) shared data with other applications

3.Session's deferred load implementation solves two problems:
 closes the connection normally and ensures that the same session. is accessed in the request
Hibernate session is an advanced package of java.sql.Connection,
A session corresponds to a Connection.
When the HTTP request is finished, the session is correctly closed (the filter implements the normal shutdown of session); the deferred load must be guaranteed to be the same session (session bound in ThreadLocal).