The answer to hibernate caching mechanism in interview

Source: Internet
Author: User

This is a frequently asked question in the interview, you can answer according to the following ideas, you have to answer perfectly. First of all, the function of hibernate cache (that is, why use a caching mechanism), and then specifically, Hibernate cache classification,Finally, we can give a concrete example. 
the role of hibernate caching:
Hibernate is a persistent layer framework that accesses physical databases frequently to reduce the frequency of application access to physical data sources, thereby improving application 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, and at a particular moment or event synchronizes the cache and the data from the physical data source
Hibernate cache Classification:
Hibernate caching consists of two main categories: Hibernate cache and Hibernate level two cache
Hibernate cache is also known as "session cache", it is built-in, cannot be unloaded (cannot be uninstalled means that this cache is not optional, must have some features, can not cancel the session cache). Because the life cycle of a Session object usually corresponds to a database transaction or an application transaction, its cache is a transaction-scoped cache. The first level of caching is required, is not allowed and in fact cannot be dismounted. In the first level cache, each instance of a persisted class has a unique OID.
Hibernate level Two cache, also known as "Sessionfactory cache", is a process-wide or cluster-wide cache because the Sessionfactory object's life cycle corresponds to the entire process of the application, so hibernate level two caches are Concurrency problems are possible, so an appropriate concurrency access policy is required that provides the transaction isolation level for cached data. The second-level cache is optional and is a configurable plug-in, which, by default, is not enabled by Sessionfactory.

what data is suitable for storage in the second level cache?
1 rarely modified data
2 is not very important data, allowing occasional concurrent data
3 data that will not be accessed concurrently
4 Constant Data
not suitable for storing data in a second level cache?
1 frequently modified data
2. It is absolutely not allowed to have concurrent access data, such as financial data, is never allowed to appear concurrency
3 data that is shared with other apps.

How does Hibernate find objects apply caching?
when hibernate accesses the data object according to the ID, it is first checked from the session level cache, and if a level two cache is configured, it is checked from the level two cache, and if none are found, then the database is queried and the result is placed in the cache by ID .
Update the cache while deleting, updating, and adding data

Hibernate managing Cache Instances
Whenever we manage the hibernate cache (managing the caches), when you pass an object to the Save (), update () or Saveorupdate () method, or use load (), get (), List ( ), the iterate (), or the scroll () method to obtain an object, the object is added to the internal cache of the session.
when the Flush () method is subsequently called, the state of the object is synchronized with the database. If you do not want this synchronization to happen, or if you are working on a large number of objects and need to manage memory effectively, you can call the evict () method to remove these objects and their collections from the first-level cache.

This is all very detailed, and it is estimated that the interviewer has a good impression of you. However, do not cover everything, you can pick some of the content you know to say, not very understand the content can be omitted, lest out of blunder.

The answer to hibernate caching mechanism in interview

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.