What is the difference between hibernate's Level 1 cache and level 2 cache?

Source: Internet
Author: User
Before giving you a detailed description of the second-level cache of hibernate, we will first let you know the next-level cache, and then give a comprehensive introduction to the second-level cache of hibernate. Hibernate provides two levels of cache. The first level of cache is session-level cache, which belongs to the scope of transactions. This level of cache is managed by Hibernate and generally requires no intervention. The second level of cache is the sessionfactory level cache, which belongs to the process scope or cluster scope. This level of cache can be configured and changed, and can be dynamically loaded and detached. Hibernate also provides a query cache for the query results, which relies on the second-level cache. I. comparison between level-1 cache and level-2 Cache: The level-1 cache stores data in the form of a consistent object, each transaction has a separate scope of the first-level cache process or cluster, the cache is shared by all transactions in the same process or cluster. the concurrent access policy does not cause concurrency problems because each transaction has a separate first-level cache, you do not need to provide a concurrent access policy. Because multiple transactions access the same data in the second-level cache at the same time, you must provide an appropriate concurrent access policy, to ensure that no data expiration policy is provided for specific transaction isolation-level data expiration policies. Objects in the first-level cache will never expire, unless the application explicitly clears the cache or clears a specific object, a data expiration policy must be provided, such as the maximum number of objects in the memory-based cache, the maximum time allowed for an object to be cached and the maximum idle time allowed for the object to be in the cache is the memory and hard disk of the physical storage medium. The object's bulk data is first stored in the memory-based Cache. When the number of objects in the memory reaches the upper limit specified in the data expiration Policy, the other objects will be written to the hard disk-based cache. The cache software implementation includes the cache implementation provided by a third party in the hibernate session implementation, and hibernate only provides the cache adapter (cacheprovider ). It is used to integrate specific cache plug-ins into hibernate. When cache is enabled, Hibernate enables the first level of cache as long as the application saves, updates, deletes, loads, and queries database data through the session interface, copy data in the database to the cache as objects. For batch update and batch delete operations, if you do not want to enable the first-level cache, you can bypass the hibernate API, perform the specified operation directly through the jdbc api. You can configure the second-level cache on the granularity of a single collection of a single class or class. If the class instance is frequently read but rarely modified, you can consider using the second-level cache. Hibernate adds its instances to the second-level cache at runtime only when a class or set is configured. The physical medium for the first-level cache is memory. Due to limited memory capacity, you must use an appropriate retrieval policy and retrieval method to limit the number of objects to be loaded. Session's evit () method can explicitly clear specific objects in the cache, but this method is not recommended. The physical media in the second-level cache can be memory and hard disk, so the second-level cache can store a large amount of data. The maxelementsinmemory attribute value in the Data expiration policy can control the number of objects in the memory. The second-level cache management mainly includes two aspects: select the persistence class that requires the second-level cache, set the appropriate concurrent access policy: select the cache adapter, and set the appropriate data expiration policy. II. level-1 Cache Management: When an application calls session save (), update (), savaeorupdate (), get (), or load (), and the list () that calls the query interface () in the iterate () or filter () method, if the corresponding object does not exist in the session cache, Hibernate will add the object to the first cache. When cache is cleared, Hibernate synchronously updates the database based on the state changes of the objects in the cache. Session provides two methods for applications to manage the cache: evict (Object OBJ): Clear the persistence object specified by the parameter from the cache. Clear (): clears all persistent objects in the cache. Iii. hibernate second-level Cache Management: 1. the general process of the hibernate second-level cache policy is as follows: 1) When querying conditions, a select * From table_name where… is always sent .... (Select all fields) This SQL statement queries the database and obtains all data objects at a time. 2) put all the data objects obtained into the second-level cache by ID. 3) When hibernate accesses the Data Object Based on the ID, it first looks up the data object from the session level cache. If the session level-2 cache is not found, it will be found from the level-2 cache, query the database and put the result into the cache by ID. 4) when data is deleted, updated, or added, the cache is also updated. The Hibernate second-level cache policy is a cache policy for ID queries and does not work for conditional queries. Therefore, Hibernate provides a query cache for conditional queries. 2. What 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) reference data, it refers to the constant data for reference. The number of instances is limited, and its instances will be referenced by many other types of instances. The instances are rarely or never modified. 3. Is it not suitable for storing data in the second-level cache? 1) frequently modified data 2) financial data, absolutely not allowed to have concurrency 3) data shared with other applications. 4. the commonly used cache plug-in hibernater second-level cache is a plug-in. Below are several common cache plug-ins: ◆ ehcache: can be used as a process-wide cache, and the physical medium for storing data can be memory or hard disk, it supports the query cache of hibernate. ◆ Oscache: it can be used as a process-range cache. The physical medium for storing data can be memory or hard disk. It provides a wide range of cache data expiration policies and supports Hibernate query cache. ◆ Swarmcache: it can be used as a cache within the cluster, but does not support Hibernate query cache. ◆ Jbosscache: cache within the cluster. It supports transaction-type concurrent access policies and supports Hibernate query cache. 5. The main steps to configure the hibernate second-level cache: 1) Select the persistence class that requires the second-level cache and set its naming cache's concurrent access policy. This is the most important step. 2) select an appropriate cache plug-in and edit the plug-in configuration file.

 

Original article link: http://zhidao.baidu.com/question/258633780.html

 

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.