aspose file tools*
The moose likes Object Relational Mapping and the fly likes hibernate cache Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate cache" Watch "hibernate cache" New topic
Author

hibernate cache

Mathew Lee
Ranch Hand

Joined: Jun 08, 2009
Posts: 238



Hi,

I am new to hibernate cache. I was not clear on first, second, third etc levels of cache. why we use cache and where and how we use it. Any ideas, resources, links, sample code highly appreciated. thanks in advance.
Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90
Mathew,
Cache is an inconsistent store of information that can be used for scalability and to improve performance. Hibernate provides three kinds of Cache mechanism.

1. Session ( By Default, cannot be turned off).
2. SessionFactory ( Second level cache).
3. Query cache.


First level of cache is nothing but org.hibernate.Session. This will track all the dirty states of POJOs being done when you open a session and also will hinder to many SQL queries hitting at Database.

Second level cache is SessionFactory which is an immutable object holding information about everything from the configuration file and helps to create multiple Session objects for the application to use. When you create multiple Session objects from the SessionFactory, it will track all the session objects and queries made out of it. So it will help caching queries created across Session objects and reduce multiple trips to DB. This type of cache is provided many third party providers.

Third level cache is Query cache. When you enable second level cache in the configuration, you are good to get query cache also. All you have to do is, just enable the query cache in the configuration. This will save the fields and values retrieved from each query in cache.

I have given a very little info about hibernate cache as i am not well versed. There is a lot more about it. You can get it from so many sources available in the net. but this will give you a start, i hope.



Thanks,
Abu.A


Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


2. SessionFactory ( Second level cache).

The SessionFactory is not a cache. You can configure a second level cache at this level but that is a different thing.


Third level cache is Query cache. When you enable second level cache in the configuration, you are good to get query cache also

Its not really a "third level" cache, and you don't get it just by configuring a second level. You can configure a second level cache associated with the SesisonFactory.


So it will help caching queries created across Session objects and reduce multiple trips to DB

The second level cache has no effect on queries. It will reduce round trips when entities are loaded into the session.


This will save the fields and values retrieved from each query in cache.

No, it will save the query and the identifiers of the entities returned in the results set for that query (which is why query cache without second level cache makes no sense)


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90
Thanks Paul.. You showed some light on my understanding of Hibernate cache...
 
jQuery in Action, 2nd edition
 
subject: hibernate cache