Hibernate first-level cache and level two cache

Source: Internet
Author: User

Hibernate caches between Hibernate applications and databases, caches database data for replication, and caches are used primarily to reduce the frequency of direct access to the database, thus improving application performance. Hibernate uses a two-level caching mechanism, if the first level of the cache does not query the corresponding data, you can also go to the second-level cache query, if it is not found in the second level cache, then you have to query the database. The first level cache is the session cache, and the second level cache is the Sessionfactory external cache.

1. Form of data storage

First-level caching: An interrelated persisted object.

Second level cache: bulk data for objects.

2. Scope of the cache

First-level caching: The scope of things, each with a separate first-level cache.

Second-level caching: process-wide or cluster-wide, the cache is shared across all things within the same process or cluster.


3. Concurrent access Policies

First-level caching: Because each thing has a separate first-level cache, there is no concurrency problem, so there is no need to provide a concurrency access policy.

Second-level caching: Because multiple things access the same data in a second-level cache at the same time, you must provide an appropriate concurrency access policy to guarantee a specific level of isolation.


4. Data expiration policy.

First level cache: No data expiration policy is provided. Objects in the first level cache never expire unless the application explicitly empties the cache or clears specific objects

Second-level caching: You must provide a data expiration policy, such as the maximum number of objects in a memory-based cache, the longest time that an object is allowed to be in the cache, and the maximum idle time that the object is allowed to be in the cache


5, physical media.

First level cache: memory.

Second level cache: Memory and hard disk. The bulk data of the object is first stored in the memory-based cache, and when the number of objects in memory reaches the maxelementsinmemory value of the data expiration policy, the remaining objects are written to the hard disk-based cache.


6. Cached software implementations.

First-level caching: The implementation of the Hibernate session contains the cache.

Second level cache: provided by a third party, hibernate only provides a cache adapter for integrating specific cache plugins into hibernate.

7. How to enable caching.

First-level caching: Hibernate initiates a first-level cache to copy data from the database to the cache as long as the application performs the operation of saving, updating, deleting, loading, or querying database data through the session interface. For bulk update and bulk Delete operations, if you do not want to start the first level cache, you can bypass the Hibernate API and perform bulk operations directly through the JDBC API.

Second-level caching: Users can configure a second-level cache on the strength of a single set of classes or classes. If an instance of a class is read frequently but is seldom modified, consider using a second-level cache. Only a second-level cache is configured for a class or collection, and hibernate joins its instance to the second-level cache at run time.


8. How the user manages the cache.



Hibernate first-level cache and level two cache

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.