Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate & EHCache : how does maxElementsInMemory work?

I've configured EHCache with a defaultCache (for elements), a StandardQueryCache (for queries) and UpdateTimestampsCache (for what I believe is to keep track of database updates...but I don't really get what it excactly does).

I've set the maxElementsInMemory for each of these caches, but what I don't get is what this number controls for the StandardQueryCache and for the UpdateTimestampsCache. I get that the number of entities that can be cached in the default cache must not exceed 10000, but the query cache does not cache elements. It caches primary keys (as far as I understand).

Does this then mean that the maxElementsInMemory for the StandardQueryCache controls the number of "rows" from results, or does it control the number of primary key pairs for elements it may have?

What about the UpdateTimestampsCache? Does it keep track of the last time an entity was updated, the last time a table was updated...or something else? What number should I use for maxElementsInMemory for this one?

Thanks!

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" monitoring="autodetect"     dynamicConfig="true">
  <defaultCache 
    maxElementsInMemory="10000"
    eternal="false"
    timeToIdleSeconds="3600"
    timeToLiveSeconds="3600">
  </defaultCache>

  <cache
    name="org.hibernate.cache.internal.StandardQueryCache"
    maxElementsInMemory="10000"
    eternal="false"
    timeToIdleSeconds="3600"
    timeToLiveSeconds="3600">
  </cache>

  <cache
    name="org.hibernate.cache.spi.UpdateTimestampsCache"
    maxElementsInMemory="10000"
    eternal="true">
  </cache>

</ehcache>
like image 394
sbrattla Avatar asked Sep 21 '12 05:09

sbrattla


People also ask

What is Hibernate used for?

Hibernate is an open source object relational mapping (ORM) tool that provides a framework to map object-oriented domain models to relational databases for web applications. Object relational mapping is based on the containerization of objects and the abstraction that provides that capacity.

Is Hibernate still used?

No, Hibernate is not deprecated.

Why is Hibernate so popular?

It helps with intrinsic problems in interfacing otherwise incompatible databases. Java Hibernate, as an ORM tool, helps to bridge that gap between incompatible databases. Automatic SQL Query Generation - There is a common belief that developers don't need to understand how to write SQL queries.

What is Hibernate principle?

Hibernate is a Java framework that simplifies the development of Java application to interact with the database. It is an open source, lightweight, ORM (Object Relational Mapping) tool. Hibernate implements the specifications of JPA (Java Persistence API) for data persistence.


1 Answers

For query caching, the results from each query result is one entry in the StandardQueryCache region. So your cache there is currently set up to cache 10000 different query results in the default/unnamed region. Queries set to use a named region (Query#setCacheRegion) write to a different cache region.

These results need to be "invalidated" whenever the underlying data is changed. That is the purpose of UpdateTimestampsCache. When Hibernate writes to tables it makes entries into the UpdateTimestampsCache (this process is only enabled when the query cache is enabled as it is explicitly part of invalidating these cached query results). When reading back cached query results, we check the timestamp cached with the query results against the timestamps of all the tables it uses to determine if the results are still valid. Its best really to not limit this region if possible. If you need to, the best number is the number of tables in your underlying domain model. Otherwise, cached query results might start getting invalidated when not necesary. Hard to imagine you have 10000 tables though, so you are probably fine there.

like image 122
Steve Ebersole Avatar answered Oct 21 '22 17:10

Steve Ebersole