Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does the hibernate cache save?

Hi: I am confused about what is cached by the hibernate cache.

From the docs, I know there are there types of cache in hibernate.

First-level:the transaction-level. It seems that the entity to be persisted by a session are cached here.

Second-level cache: I really have no idea about this cache,from the api,it said this cache work at the sessionfactory level... I can not understand.

Query cache: This is easily understand for me(maybe I am wrong). It cache the query parameter along with the related entity.

This is all I know about the cache in hibernate.

Anyone can give me more details?

BTW,I am not good at the sql/jdbc work,so I also want to know how should I know about the sql/jdbc to learn more about hibernate(any docs?)? I do not want to be just a hibernate user since it is just a tool,what I want is learn something from this wonderful framwork. :)

like image 668
hguser Avatar asked Nov 02 '10 12:11

hguser


People also ask

What is the use of cache in Hibernate?

Caching is a mechanism to enhance the performance of a system. It is a buffer memorythat lies between the application and the database. Cache memory stores recently used data items in order to reduce the number of database hits as much as possible.

Where does Hibernate store cache?

Hibernate Session is it's first level cache. It is object in heap, so it is in RAM.

What is Hibernate Session cache?

Hibernate. Hibernate Basics, Hibernate Caching. Caching is a facility provided by ORM frameworks that helps the users to get fast-running web applications while helping the framework itself to reduce the number of queries made to the database in a single transaction.

What is L1 and L2 cache in Hibernate?

L1 Cache is the cache that exists per Hibernate session, and this cache is not shared among threads. This cache makes use of Hibernate's own caching. L2 Cache is a cache that survives beyond a Hibernate session, and can be shared among threads.


1 Answers

First Level Cache

This cache is enabled by default. When NHibernate is loading an entity by its unique id from the database then it is automatically put into the so called identity map. This identity map represents the first level cache.

The life-time of the first level cache is coupled to the current session. As soon as the current session is closed the content of the respective first level cache is cleared. Once an entity is in the first level cache a subsequent operation that wants to load the very same entity inside the current session retrieves this entity from the cache and no roundtrip to the database is needed.

One of the main reasons behind this entity map is to avoid the situation that two different instances in memory can represent the same database record (or entity).

(adapted from NH FAQ)

Second Level Cache

This cache is above the single session scope cache. With this cache you save many roundtrips to the database, since the first time anyone in any session loads determined entity, it remains in the cache, so the next request (from any other session) will get it from the cache.

To use L2 Cache you will need a cache provider, such as SysCache or Memcache. These guys will store the values of the cached entities as an array os strings hashed by the entity's id. It's important to note that it's not the .net object that is cached, but only its values.

It is said that it's tied to the session factory because the cache will work for sessions created by determined session factory, so sessions created by different session factories will not share cache. Is the concept of a session factory clear to you?

Query Cache

This one is really simple, but there is a catch. It will cache the query and its parameters, but it will not cache the entities returned. It will only cache their Ids, so it only makes sense with L2 cache, because it contains the values associated to that Id.

NHiernate's FAQ entry about cache:

https://web.archive.org/web/20110514214657/http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/11/09/first-and-second-level-caching-in-nhibernate.aspx

like image 89
Pedro Avatar answered Oct 03 '22 19:10

Pedro