• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate cache

 
Mathew Lee
Ranch Hand
Posts: 238
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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)
 
Abubacker Siddik
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul.. You showed some light on my understanding of Hibernate cache...
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic