|
One, hibernate cache
(1) hibernate supports two levels of cache, supports only the default cache;
(2) Each Session comes with a internal cache;
(3) When a Session is closed, the corresponding cache is automatically cleared;
(4) save, update, saveOrupdate, load, get, list, iterate, lock objects to the method will be stored in the cache.
(5) can be read from the cache data only: get, load, iterate
(6) Query objects by default do not read the cache, the cache if you want to support, they have passed the grammar:
query.setCacheable (true);
true
(7) After opening the query cache, only the query conditions identical to the previous query, will match the success in the cache.
(8) Criteria cache lack of support;
(9) a cache can not control the number of objects in the cache, the operation should pay attention when large quantities of data may cause memory overflow, can clear the cache.
session.clear () to clear the cache for all objects
session.evict (user) to clear the specified object
I have tried, session.clear () can be cleared away session.createQuery, but can not afford to clear session.createSQLQuery.
session.createSQLQuery is clear no one can not afford to, the following methods I have tried, no one so that the data is still wrong, the database is not the latest data:
session.setFlushMode (FlushMode.AUTO);
session.setCacheMode (CacheMode.NORMAL);
session.flush ();
session.clear ();
sqlQuery.setCacheable (false);
The only way is cleared closed session. . . Silent, hibernate Is SB, SB, or I will not use? Is there a special clean-up session.createSQLQuery cached? This cache is a BUG can not afford to clean it?
Two, hibernate second level cache
save, update, saveOrupdate, load, get, list, query, Criteria method will fill the secondary cache
get, load, iterate will fetch data from the secondary cache
session.save (user) If user primary key "native" generation is not into the L2 cache.
(1) Turn the secondary cache
< Property name = "cache.use_second_level_cache"> true < / property>
(2) specifies the implementation class for the secondary cache hibernate
< Property name = "cache.provider_class">
org.hibernate.cache.OSCacheProvider
< / Property>
(3) creating a profile for OSCache cache (required hibernate_Advance_Surpport_lib)
src / oscache.properties
Modify the configuration:
cache.capacity = 1000 Specifies the cache can accommodate the number of objects
(4) specify what classes need to put the secondary cache, need long-term use to only necessary objects into the second-level cache
< Class-cache class = "entity.PetInfo" usage = "read-only" /> // not allowed to update objects in the cache
< Class-cache class = "entity.PetInfo" usage = "read-write" /> // allow updating the object cache
Or in the orm file:
< Class name = "entity.PetInfo" table = "PetInfo" schema = "dbo" catalog = "epet">
< Cache usage = "read-only" />
...
< / Class>
(5) If you need to clear the cache, use the following syntax
sessionFactory.evict (User.class) to clear all user |
|
|
|