In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces which methods to support Hibernate cache, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Which methods support Hibernate caching
* get ()
* load ()
* iterate () (query entity objects)
Save ()
Query caching only works on query.list ()
First-level cache test:
1.Load test: issue two load queries in the same session (1)
2.Get test: issue two get queries in the same session (1)
3.iterate test: issue two iterator queries in the same session (1 (id) + NMagne1 (id))
4.Iterate query attribute test: query attributes are issued twice in the same session (twice, iterate queries common attributes, first-level cache does not cache, so sql is issued)
5. In the same session, first save, and then issue load to query the save data-save is cached
6. First call load query in the same session, then execute sessio.clear () or session.evict (), and then call load query (2 times)
Sessio.clear () or session.evict () can manage the first-level cache, which cannot be cancelled, but can be managed. The above statement issues a sql because the entity in the first-level cache is cleared.
7. Add 1000 pieces of data to the database in batch
/ / force session to persist the data for each piece of data and clear the cache at the same time to avoid memory overflow caused by large amounts of data
Enable the second-level cache test:
1. Open two load queries out of two session (get is the same as load, once)
2. Open two session, call load respectively, and then use sessionFactory to clear the secondary cache (2 times)
3. Interaction between primary cache and secondary cache
Session.setCacheMode (CacheMode.GET); / / is set to only read from the secondary cache and not write data to the secondary cache (2)
Session.setCacheMode (CacheMode.PUT); / / is set to only write data to the secondary cache and not read the data (2 times)
Hibernate cache query test: enable hibernate query cache test:
1. Enable query cache, disable secondary cache, open a session, and call query.list (query attributes) (once)
2. Enable query cache, disable secondary cache, open two session, and call query.list (query attributes) respectively.
The second time, the database is not queried because the query cache life cycle has nothing to do with the session life cycle.
3. Enable query cache, disable secondary cache, open two session, and call query.iterate (query attributes) respectively.
Second, query the database, because the query cache only works on query.list (), not on query.iterate (), that is to say, query.iterate () does not use query cache
4. Close the query cache, turn off the secondary cache, open two session, and call query.list (query entity objects) to query the database, because list will issue query sql every time by default.
5. Open query cache, close secondary cache, open two session, respectively call query.list (query entity object) the second to query the database, N sql statements will be issued. Because query cache is turned on and secondary cache is turned off, the query cache will cache the id of entity objects, so hibernate will query the corresponding entities according to the id of entity objects. If there is no corresponding entity in the cache. Then a sql statement querying according to the entity id will be issued, otherwise the sql will not be issued and the data in the cache will be used
6. Enable query cache, enable secondary cache, enable two session, call query.list (query entity object) respectively, the second will not issue sql, because secondary cache and query cache are enabled, query cache caches the id list of entity objects, and hibernate will obtain the corresponding data in the secondary cache according to the id list of entity objects.
Integration and use of second-level cache in the project
1. Copy the echcache.xml file to the src. The secondary cache hibernate is turned off by default and enabled manually.
two。 Open the secondary cache and modify the hibernate.cfg.xml file
< property name="hibernate.cache.user_second_level_cache">True
< /property>3. Specify cache product provider
< property name="hibernate.cache.provider_calss">Org.hibernate.cache.EhCacheProvider
< /property>4. Specify which entity classes use secondary caching (two methods, the second is recommended)
* species: in * .hbm.xml, in
< id>Join before
< cache usage="read-only" />, using a secondary cache
Second: in the hibernate.cfg.xml configuration file, in the
< mapping resource="com/Studnet.hbm.xml" />Add the following:
< class-cache class=" com.Studnet" usage="read-only" />Summary:
Don't take it for granted that Hibernate caching will improve performance, only if you can handle it and the conditions are right. There are still many secondary cache restrictions in hibernate, and the inconvenience of using jdbc may greatly reduce the update performance. If you misuse it without understanding the principle, there may be a problem of 1N. Improper use may also lead to reading dirty data.
If you can't stand the limitations of hibernate, do your own caching at the application level.
The higher the level of caching, the better the effect. It's like even though the disk has a cache, the database still has to implement its own cache, and even though the database has a cache, our application still has to do the cache. Because the underlying cache does not know what the high-level data is going to do with it, it can only be general-purpose, while the high-level cache can be targeted, so the effect of caching at a higher level is better.
The view on performance is reasonable performance rather than * performance. What is reasonable? Users can feel (or sensitive) within the scope, does not become a physical bottleneck in business processing, such as the often said response within 3 seconds.
Thank you for reading this article carefully. I hope the article "which methods support Hibernate caching" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.