Hibernate cache study

Hibernate cache Raiders (2007-07-18 21:51:37) Tags: hibernate Category: Hibernate

Many people are not very familiar with the secondary cache, or there is a wrong understanding, I have always wanted to write an article tell us about the hibernate second cache, and finally help it.

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.

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

Query cache

First of all, need to configure 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, optional

The second line specifies to use cacheRegion is myCacheRegion, that is, you can do to each query in a separate cache configuration, use the designated setCacheRegion do need to configure it inside ehcache.xml:

<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

For the query cache, the cache key is based on the hql generated sql, together with the parameters, such as paging information (through the log output can be seen, but its output is not very readable, it is best to check it out code).

For example, hql:

from Cat c where c.name like?

Generated along the following sql:

select * from cat c where c.name like?

Parameter is the "tiger%", then the query cache key * about * this is the string (I am writing from memory is not accurate, but also the read to understand):

select * from cat c where c.name like?, parameter: tiger%

In this way, to ensure that the same query, the same parameters with the same under the conditions of key.

Now talk about the cached value, if the way is the list of words, value here is not the entire result set, but this string of inquiries by the ID. In other words, regardless of method or iterate method list, the first query, the query the way they are usually the way they are the same, list the implementation of a sql, iterate the implementation of 1 + N article, by the acts of more than filled their the cache. However, the same conditions when the second query, and iterate on the conduct of all the same, according to the key cache to be found inside the cache value, value is a string of id, and then to the class to the cache inside one by one out the load. This is done to save memory.

Can be seen, query cache related need to open the class cache. list and the implementation of the first iterate method, the query cache are both filled and filled with class caching.
There is also a very easily overlooked important issues, that is open after the query cache, even the list also may encounter the problem of 1 + N! The same conditions when the first list, because it can not find the cache, regardless of the existence of class caching data, always send a sql statement to the database to obtain all the data, and then fill the cache and query cache class. However, when the second performance, the problem comes, if your cache timeout class relatively short, and now all overtime class cache, but also query the cache, then the list method in the future to obtain string id will go to the database one by one load! Therefore, class cache timeout must not be shorter than the query cache timeout settings! If you have set up a daze time, to ensure that class time trance cached query cache is greater than the survival time. There are other circumstances, such as class cache evict a mandatory procedure, which took note of on your own.

In addition, if the select query contains hql words, then the query cache is inside the value of the entire result set.

When hibernate update the database, it queries how to know which cache to update it?

hibernate in one place, the maintenance of each table was last updated, in fact, that is, on the above specified net.sf.hibernate.cache.UpdateTimestampsCache inside the cache configuration.

Hibernate update when the time passed, hibernate will know what the impact of the updated table. It then update the table was last updated. Each cache has a generation time and the cache table of the query, when the hibernate query whether there is a cache, if cache exists, it must remove the cache and the cache generated by the query table, and then to find these tables were last updated, if there is a table in time to generate updated, then the cache is invalid.

As can be seen, as long as the updated a table, then all the tables related to the failure of the query cache, so query cache hit rate may be relatively low.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Hibernate cache study

  • 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

Recent
Recent Entries
Tag Cloud
Random Entries