Hibernate cache mechanism and hibernate Cache Mechanism

Source: Internet
Author: User

Hibernate cache mechanism and hibernate Cache Mechanism

I. why (why use Hibernate cache ?)

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

To reduce the frequency of applications accessing physical data sources and improve the running performance of applications.

Data in the cache is the replication of data in the physical data source. during runtime, the application reads and writes data from the cache. Data in the cache and the physical data source will be synchronized at a specific time or event.

 

Ii. what (what is the principle of Hibernate cache ?) Hibernate cache includes two types: Hibernate level-1 cache and Hibernate level-2 cache.

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

The Session cannot be uninstalled, and the Session cache is the cache of the transaction scope (the lifecycle of the Session Object usually corresponds to a database transaction or an application transaction ).

In the level-1 cache, each instance of the persistence class has a unique OID.


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

Because the life cycle of the SessionFactory object corresponds to the whole process of the application, The Hibernate second-level cache is a cache of process scope or cluster scope, which may cause concurrency problems, therefore, an appropriate concurrent access policy is required, which provides a transaction isolation level for cached data.

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

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 for storing in the second-level cache?
1) rarely modified data
2) not very important data, allowing occasional Concurrent Data
3) data that will not be accessed concurrently
4) constant data
Is it not suitable for storing data in the second-level cache?
1) frequently modified data
2) concurrent access data, such as financial data, is absolutely not allowed.
3) data shared with other applications.


3. The implementation of delayed Session loading should solve two problems: closing the connection normally and ensuring that the request accesses the same session.

Hibernate session is an advanced encapsulation of java. SQL. Connection. A session corresponds to a Connection.

After the http request ends, the session is closed correctly (the filter achieves normal Closing of the session). Delayed loading must ensure that the session is the same (the session is bound to ThreadLocal ).


4. How does Hibernate search for objects to apply cache?
When Hibernate accesses the Data Object Based on the ID, it first queries the data object from the Session level cache;

No. If the second-level cache is configured, the second-level cache is used;

If none of them are found, query the database again, and then update the cache when the results are deleted, updated, and added to the cache by ID.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.