Hibernate Level 2 Cache

Source: Internet
Author: User

The second-level cache is also called the process-level cache or SessionFactory-level cache. The second-level cache can be shared by all sessions. In other words, the second-level cache can use the content in the second-level cache. the life cycle of the second-level cache is the same as that of SessionFactory (heavyweight, one database and one SessionFactory). SessionFactory can manage the second-level cache. the second-level cache has its own implementation in Hibernate called HashTable, but it is not recommended for commercial use and can be used for testing. hibernate's second-level Cache has a dedicated Cache Policy Provider (Cache Providers), such as EHCache and OSCache. in this article, we will mainly introduce the usage of Hibernate second-level cache EHCache. vcnQsZLDLyP3Su9Ov1MK/Examples Response + response FtrxhY29vbtLReWVzKHLToc7lY7XEoaNuKXllcyhjZm9ja3PDrmNyZXEp "src =" http://www.bkjia.com/uploads/allimg/131209/0K15233N-0.png "/> Configure and use level-2 Cache EHCache. * Set ehcache. copy the xml file to src (in Hibernate3 \ etc \ ehcache. xml Path. find the configuration file from the hibernate-related example) for example: default configuration. <defaultCache maxElementsInMemory = "10000" eternal = "false" timeToIdleSeconds = "120" timeToLiveSeconds = "120" overflowToDisk = "true"/> the configuration is described as follows: <! -- By default, classes can be configured separately --> <! -- Maximum number of objects --> <! -- Whether it expires --> <! -- How long has it not been accessed --> <! -- If the limit is exceeded, put it into the disk --> you can configure <cache name = "sampleCache2" maxElementsInMemory = "1000" eternal = "true" timeToIdleSeconds = "0" timeToLiveSeconds = "0" overflowToDisk =" false "/> * In hibernate. cfg. <propertyname = "hibernate. cache. provider_class "> org. hibernate. cache. ehCacheProvider </property> * enables level-2 caching, which is also its default configuration. cfg. enable the second-level cache configuration in the xml configuration file. <propertyname = "hibernate. cache. use_second_level_cache "> tr Ue </property> * specifies which object classes use second-level cache. You can use the <cache> label in the ing file or in hibernate. cfg. in xml files, pay attention to the usage of the policy. Generally, read-only and read-write are used. for example, unified configuration in the configuration file. (I will configure the Student class here) <class-cacheusage = "read-only" class = "com. tgb. hibernate. student "/> * The above completes the basic EHCache configuration, and the next step is to use the Load query in the specific code, for example, in two sessions, because we have configured the second-level cache, therefore, in the second Load process, the query statement is not issued, but the data is directly obtained from the second-level cache. the test code is as follows: [java]/*** Level 2 Cache test code **/public class CacheTest extends TestCase {/*** enable Level 2 Cache * load query in two sessions */public void testCache () {session Session = null; try {// get session = HibernateUtils. getSession (); // start the transaction session. beginTransaction (); // the first time the Student student Student = (Student) session whose id is 1 is queried by load. load (Student. class, 1); System. out. println ("student. name = "+ student. getName (); // submit the session. getTransaction (). commit ();} catch (Exception e) {e. printStackTrace (); session. getT Ransaction (). rollback ();} finally {HibernateUtils. closeSession (session);} try {session = HibernateUtils. getSession (); session. beginTransaction (); the second time the Student student Student = (Student) session whose id is 1 is queried by load. load (Student. class, 1); // No query statement is issued. Because the second-level cache is configured, the session can share the data in the second-level cache. // The second-level cache is a process-level cache System. out. println ("student. name = "+ student. getName (); session. getTransaction (). commit ();} catch (Exception e) {e. prin TStackTrace (); session. getTransaction (). rollback ();} finally {HibernateUtils. closeSession (session) ;}}above is how to use the EHCache product of Hibernate secondary cache. of course, the second-level cache has its own applicable scenarios. suitable for storing data in the second-level cache? 1. Rarely modified data 2 is not very important data, allowing occasional Concurrent Data 3 will not be accessed concurrently 4. Reference data refers to the constant data for reference supply, it has a limited number of instances, and its instances will be referenced by many other types of instances. The instances are rarely or never modified. Is it not suitable for storing data in the second-level cache? 1. Frequently-modified data 2. Financial data cannot be shared with other applications. Summary cache is generated for performance, but do not take it for granted that the cache will certainly improve performance. This is only true if you can control it and the conditions are appropriate. There are still many limitations on the second-level cache of hibernate. If you do not understand the principle, you may encounter N + 1 problems. Improper use may also lead to dirty Data Reading.

Related Article

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.