Hibernate (16): Hibernate level Two cache (i)

Source: Internet
Author: User

    • Hibernate cache

cache: A very general concept in the computer domain. It is between an application and a persistent data storage source, such as a file on disk or a database, and it works by reducing the frequency with which the application directly reads the permanent data storage source, thereby improving the performance of the application. The data in the cache is a copy of the data in the data storage source. The physical media that is cached is usually memory.

Two levels of cache are available in Hibernate

1) The first level of cache is the session-level cache, which is a transaction-scoped cache. This level of caching is managed by Hibernate.

2) The second level of caching is the cache at the sesssionfactory level, which is a process-wide cache.

    • Sessionfactory levels of Caching

The sesssionfactory cache can be divided into two categories

1) Built-in cache: Hibernate comes with, not uninstall. Normally during hibernate initialization, hibernate puts the mapping metadata and predefined SQL statements into the sessionfactory cache, which is the assignment of the data in the mapping file (the data in the. hbm.xml file), which is read-only.

2) External cache (level two cache): a configurable cache plug-in. By default, Sessionfactory does not enable this cache plug-in. The data in the external cache is a copy of the database data, and the external cached physical media can be either memory or hard disk.

    • Hibernate level two cache application scope

1) data that fits into the level two cache:

is rarely modified

Not very important data, allowing occasional concurrency problems

2) data that does not fit into the level two cache

are often modified

Financial data, never allow concurrency problems

Data that is shared with other applications

    • Hibernate level two cache architecture diagram

    • Concurrent access policy for secondary caches

1) It is also possible to have a variety of concurrency problems when two concurrent transactions access the same data cached by the persistence layer.

2) Level Two cache can set the following 4 types of concurrent access policies, each of which corresponds to a transaction isolation boundary:

√ Non-strict read/write (Nonstrict-read-write): There is no guarantee that the cache is consistent with the data in the database. Provides the Read uncommited transaction isolation level, which can be used for data that is rarely modified and allows dirty reads.

√ Read-write (Read-write): Provides the Read commited transaction isolation level. For data that is often read but rarely modified, this isolation type can be used because it prevents dirty reads.

√ Transaction type (transactional): Only applicable in managed environments, it provides repeateable read transaction isolation level. For data that is often read but rarely modified, this isolation strategy can be used to prevent dirty reads and non-repeatable reads.

√ Read-only (READ-ONLY): Provides the serializable data isolation level, which can be used for data that has never been modified.

Hibernate (16): Hibernate level Two cache (i)

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.