Hibernate in first-level cache and level two cache

Source: Internet
Author: User


Primary and level Two caching: the first-level cache is session-level, which means that it will start in a transaction. For example, in a transaction query the same object at the same time, it will not go to the database two times query.
And the level two cache is sessionfactory level, the different transactions can be shared, some permissions, when the user first logged in the associated permissions placed in the level two cache, so that every time you need permissions, you do not have to query the database.
Finally explain why this design: In general, We query the data is generally real-time, using level two cache certainly not, using a first-level cache to take advantage of the cache without affecting real-time.


A level two cache is used to store some more stable data, such as permissions, that take effect only if the user modifies the permission and logs on again.


First-level caching: Session-level caching
When the session is open, the cache is open
When the session is close, the cache is close
During the cache open to close, the same persisted object is loaded multiple times, only the first time the SQL statement is loaded to the database, and then the load is cached-based
However, the session from open to close the time is very short, generally in the project is at the beginning of the business method to open the session, after the call closes the session, in such a short moment of time, multiple loading of the same object is not likely, in other words, First-level caches are not available at all.

Second level cache: Sessionfactory level cache
In the same vein as the session, the LEVEL2 cache is turned on and off as the sessionfactory opens.
But in a generic application, Sessionfactory will exist as a singleton, so sessionfactory will persist throughout the lifetime of the application. Both level two caches persist until the application is closed.

This article is from the "Nothing-skywalker" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1665116

Hibernate in 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.