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.
Joined: Aug 12, 2009
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.