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. :)


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:


Need Your Help

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.