aspose file tools*
The moose likes Object Relational Mapping and the fly likes What does a SessionFactory object cache? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "What does a SessionFactory object cache?" Watch "What does a SessionFactory object cache?" New topic
Author

What does a SessionFactory object cache?

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Hi all, my name is Chaitanya. I am reading Hibernate in action textbook and some online material and practicing. I read that SessionFactory is responsible to create the session objects.

What I understood is,

1. It is like a pool (same like connection pooling mechanism), once we ask for a new session object, hibernate checks the session factory for any available sessions. If there is a free session object we will get that session. If not hibernate creates a new session and return it to us. Once we finish our work the session is returned to the SessionFactory.

2. It acts like a cache. In a web application environment, suppose if we load a table using the session(for first time) and after our business logic is executed the session is returned to the SessionFactory. Now if someone asks for the same table, hibernate will return the session which has the table, from the SessionFactory instead of querying the database.

This is what I understood, I believe that I am not fully correct in my understanding. Besides I have a doubt. What if I close the session?

When I close the session, I think SessionFactory will not cache the table. If this is true we should not close the session object.

Can anyone tell me whether my understanding about the SessionFactory is correct or not.

Thank you all in advance. Good day.


Love all, trust a few, do wrong to none.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Hi professionals, can anyone tell me whether my understanding about hibernate is right or wrong?
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Any one there?
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
Hi ,

There are two level of cache in Hibernate. First level cache and second level cache.
Hibenate uses Session as first level cache by caching the fetched object into Session.
When you ask for the same object with same identifier in a session , hibernate will not try to fetch it from database , it will pick the object from session itself.
In this way hibernate gives you repetable read for free.

SessionFactory is used for second level cache and in order to use second level cache , you have to explicity enable second level cache.
In second level cache , you can cache query result, queries and provide expiration time when hibernate should update the cache when it is asked to provide data from the cache again.

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Hi Mr. Piyush, thanks for the reply. But it did not make my understanding clear.

Piyush Mangal wrote:When you ask for the same object with same identifier in a session , hibernate will not try to fetch it from database , it will pick the object from session itself


What do you mean by same object and same identifier in a session?

I think that if I say session.close() the session will be destroyed and will not be available in SessionFactory. Then there is no first level cache.

In second level cache , you can cache query result, queries and provide expiration time when hibernate should update the cache when it is asked to provide data from the cache again.


I think that the query results are in the session object and you said that SessionFactory will cache the query results.

Can you please tell me what exactly will be cached in Session and SessionFactory?

Thank you in advance.
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
This might help http://blog.dynatrace.com/2009/02/16/understanding-caching-in-hibernate-part-one-the-session-cache/
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Thnak you Piyush. I am reading that.

What is meant by "The session cache caches object within the current session."

Does it mean the POJO object is cached. We supply the POJO object for example Student, Book, Event etc using session.load(Student.class, new long(1))

Will these objects are cached in the session object?
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Thank you Piyush. Now I understood about Session and SessionFactory clearly. Thanks for helping me
 
 
subject: What does a SessionFactory object cache?