In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
I. Overview of Hibernate caching
There are two levels of caching in Hibernate, the first level cache and the second level cache.
1. The first-level cache is the Session-level cache, which belongs to the scope of things, and the first-level cache is managed by hibernate.
two。 Secondary cache is sessionFactory-level cache, which belongs to process-wide cache. Secondary cache can be divided into "built-in cache" and "external cache". Built-in cache: when hibernate creates sessionFactory, it loads .hbn.xml files and initializes some default sql statements in memory. The built-in cache is read-only. External cache (secondary cache). The default sessionFactory does not start this cache plug-in. The data in the external cache is the replication of data in the database, and the physical media stored can be memory or hard disk.
Second, Hibernate first-level cache
Session caching has two main functions:
1. Reduce the frequency of accessing the database.
two。 Ensure that the relevant records in the database are consistent with the relevant objects in the cache.
Session executes relevant SQL statements based on the latest attributes of dirty objects, thus ensuring that the relevant records in the database are synchronized with the response objects in the cache. By default, session cleans up the cache at the following time points:
1. When the application calls the commit () method of Transaction, commit () cleans the cache before committing the transaction to the database.
two。 The application performs some query operations when the properties of the persisted object in the cache change.
3. Explicitly call the flush () method of Session.
Session provides two ways to manage caches:
1.evict (Object obj); clears a specific persistent object from the cache.
2.clear (); clears all persistent objects in the cache.
Third, Hibernate secondary cache
1. To use the Hibernate secondary cache:
1)。 Add the jar package and configuration file to the second-level cache plug-in:
i. Copy\ hibernate-release-4.2.4.Final\ lib\ optional\ ehcache\ * .jar to the classpath of the current Hibrenate application.
ii. Copy hibernate-release-4.2.4.Final\ project\ etc\ ehcachexml to the classpath of the current WEB application
2)。 Configure hibernate.cfg.xml download
i. Configure a secondary cache with hibernate enabled
True
ii. Configure the products used by the hibernate secondary cache
Org.hibernate.cache.ehcache.EhCacheRegionFactory
iii. Configure the secondary cache of hibernate for which classes
In fact, you can also configure which classes to use secondary cache and what the secondary cache strategy is in the .hbm.xml file. download
2)。 Configuration of secondary cache at the collection level
i. Configure to use secondary caching for collections
It can also be configured in the .hbm.xml file
ii. Note: you also need to configure that the persistence classes corresponding to the elements in the collection also use secondary caching! Otherwise, there will be n more SQL statements.
3)。 Configuration file for ehcache: ehcache.xml
4)。 Query caching: by default, the set cache is not valid for HQL and QBC queries, but can be made valid in the following ways
i. Declare query caching enabled in the hibernate configuration file
True
ii. Call the setCacheable (true) method of Query or Criteria
@ Test
Public void testQueryCache () {
Query query = session.createQuery ("FROM Employee")
Query.setCacheable (true)
List emps = query.list ()
System.out.println (emps.size ())
Emps = query.list ()
System.out.println (emps.size ())
Criteria criteria = session.createCriteria (Employee.class)
Criteria.setCacheable (true)
}
iii. Query cache depends on secondary cache (the premise of using query cache is to configure hibernate secondary cache)
Example: download
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.