Hibernate cache Raiders II
Mainly from my experience hibernate2.1 version of the basic principles and is the same as 3.0,3.1, please forgive my stubbornness.
The session provided a hibernate cache level, each session, on the same id twice a load, does not send two sql database, but the session closed, the level of cache on the failure.
SessionFactory level 2 cache is a global cache, it can be used under different caching libraries, such as the ehcache, oscache and so on, need to set up hibernate.cache.provider_class, here we ehcache, in 2.1 is
hibernate.cache.provider_class = net.sf.hibernate.cache.EhCacheProvider
If you use the query cache, together with the
hibernate.cache.use_query_cache = true
Cache can be simple as a Map, through which key in the cache to find value.
<br /> Cache Class for a record is a PO, it is to find the basis of ID, the cache key is the ID, value is a POJO. Whether list, load, or iterate, as long as the read out an object, it will fill the cache. But do not use the cache list, and iterate will first select id check out the database, and then a id the id of a load, if there are in the cache, taken from the cache, not go to the database load. The assumption that the read-write cache, the need for:
<cache usage="read-write"/>
If you use ehcache cache to achieve is the case, need to configure ehcache.xml
<cache name="com.xxx.pojo.Foo" maxElementsInMemory="500" eternal="false" timeToLiveSeconds="7200" timeToIdleSeconds="3600" overflowToDisk="true" />
That the cache which is not eternal and will never timeout, timeToLiveSeconds is cached for each element (here is a POJO) overtime hours, if eternal = "false", over a specified period of time, this element was removed the. is a trance timeToIdleSeconds time is optional. When put to the cache inside the more than 500 elements, if overflowToDisk = "true", will be part of the cache data stored in temporary files on your hard disk inside.
Cache for each class need to be configured this way. If you do not have to configure, hibernate will warn you when to start, and then use defaultCache configuration, so that class will share a number of configurations.
When an ID through hibernate changes, hibernate will know, so to remove the cache.
You may want to do the same query, the first to list, the second re-iterate, you can use to cache the. In fact it is very difficult because when you can not judge what is the first time, and the conditions of each inquiry is not the same as usual, if there are 100 database records, id from 1 to 100, when the first list of the previous 50 id, the second time when they iterate inquiries No. 30-70 to id, then 30-50 is taken from a cache inside, 51-70 is taken from the database, a total of 1 +20 article Send sql. So I have always thought that no iterate, and always will be 1 + N problem.
(Off-topic: The assertion that the list will use a large query result set into memory the whole, very slow, and only iterate better select id, but always large paging search query, and no one will really put the whole loaded into the result set, if a 20, then, iterate the need for the implementation of a total of 21 statements, list although the choice of a number of fields than the first iterate statement select id slower, but there is only one statement, not the entire result set into hibernate also will be done to optimize database dialects, such as the use of mysql's limit, or the whole list should be faster.)
If you want to iterate on the list or the results of inquiries in the cache, it is necessary to use a query cache
<br /> Query cache configuration first hibernate.cache.use_query_cache = true
If ehcache, configuration ehcache.xml, attention is not hibernate3.0 after the package name of the net.sf
<cache name = "net.sf.hibernate.cache.StandardQueryCache"
maxElementsInMemory = "50" eternal = "false" timeToIdleSeconds = "3600"
timeToLiveSeconds = "7200" overflowToDisk = "true" />
<cache name = "net.sf.hibernate.cache.UpdateTimestampsCache"
maxElementsInMemory = "5000" eternal = "true" overflowToDisk = "true" />
And then
query.setCacheable (true); / / activate the query cache
query.setCacheRegion ( "myCacheRegion ");// designated to be used cacheRegion, the second line alternative to the use of designated cacheRegion is myCacheRegion, that is, you can do to each query in a separate cache configuration, use to do this setCacheRegion designated ehcache.xml which need to configure it:
<cache name="myCacheRegion" maxElementsInMemory="10" eternal="false" timeToIdleSeconds="3600" timeToLiveSeconds="7200" overflowToDisk="true" />
If you omit the second line, cacheRegion not set, then use the above-mentioned configuration of the standard query cache, which is net.sf.hibernate.cache.StandardQueryCache
Read and write cache and read-write cache is not strictly in the realization of the difference is that the read-write cache when the cache update the cache inside the data will be replaced by a lock, other matters, if the cache to fetch the corresponding data found was locked, and then on the direct access database query.
Hibernate2.1 to achieve in the ehcache, if the locked part of the Service have taken place in the cache abnormal, then the cache will be locked until after 60 seconds of overtime.
Not strictly locked to read and write cache does not cache data.
The use of pre-conditions for the secondary cache <br /> your database program to hibernate exclusive write access to other process updates the database, hibernate is impossible to know. Necessary for the operation of your database directly through hibernate, if you call the stored procedure, or for their own use jdbc update the database, hibernate is not aware of this. hibernate3.0 bulk update and delete the cache is not updated, but 3.1 is said to have resolved the problem.
This limit quite difficult, and sometimes do hibernate batch update, delete is very slow, but you have to optimize the jdbc can not write, it is very depressing.
SessionFactory also provided ways to remove the cache, you have to write their own JDBC, you can call these methods to remove the cache, these methods are:
void evict (Class persistentClass)
Evict all entries from the second-level cache.
void evict (Class persistentClass, Serializable id)
Evict an entry from the second-level cache.
void evictCollection (String roleName)
Evict all entries from the second-level cache.
void evictCollection (String roleName, Serializable id)
Evict an entry from the second-level cache.
void evictQueries ()
Evict any query result sets cached in the default query cache region.
void evictQueries (String cacheRegion)
Evict any query result sets cached in the named query cache region.
But I do not propose to do so, because it is difficult to maintain. For example, you may now use a JDBC batch updates the table, there are three query cache will be used in this table, the evictQueries (String cacheRegion) Remove the three query cache, and then evict (Class persistentClass) to remove the class cache , looks like complete. However, your day to add a relevant query cache may be updated to forget to remove the code here. If your jdbc code everywhere, add in your time of a query cache, but also know what other places you do the appropriate changes?
-------------------------------------------------- --
Summary:
Do not take it for granted that the cache will improve performance, only you can control it and under appropriate conditions is the case. secondary cache hibernate or more restrictions, inconvenient to use the jdbc may significantly reduce the performance update. Do not understand the principle in the case of abuse, there may be 1 + N problem. Improper use can also lead to the data read out dirty.
If you can not stand the hibernate many restrictions, then in the application or its own level of cache to do it.
The higher the level in the cache so the results will be better. Although the disk as if there is cache, database or cache in order to achieve their own, although the database cache, let's do the application or the cache. The bottom of the cache because it does not know what high-level use of these data can only be done relatively common, and high-level can be targeted to achieve the cache, so do higher-level cache, the effect should be better now .
Related Posts of Hibernate cache Raiders II
-
Hibernate.cfg.xml configuration file (including the primary key generation strategy Introduction)
Hibernate.cfg.xml configuration file: <? xml version = "1.0" encoding = "utf-8"?> <! DOCTYPE hibernate-configuration PUBLIC "- / / Hibernate / Hibernate Configuration DTD / / EN" "hibernate-configuration-2.0.dtd
-
hibernate generic generic DAO
hibernate generic generic DAO
-
Java technology: Eclipse explain the use of techniques
Editor settings: Window -> Preferences -> Java-> Editor appearance: Display line number, emphasizing symmetry shown in square brackets, to emphasize that the existing line to show Print Margins its check, Tab width set 4, print made from the ...
-
Struts2 + hibernate + spring problem user log in
dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q
-
Hibernate secondary cache
Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...
-
The level Hibernate cache
Hibernate cache level: (1) a cache is very short and the session life cycle consistent, also known as session-level cache-level cache or transaction-level cache (2) Ways of Supporting level cache: get (); load (); iterator (); only entity object cach ...
-
Hibernate's lazy strategy
hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...













Leave a Reply