• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loading and Garbage Collection of EntityClasses by Hibernate

 
Stanley Walker
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Started learning hibernate a few days back. Was wondering about something. I wanted to know the lifecycle of the entity classes? Specifically when are they loaded and when are they garbage collected?

1>When we start an application, or the first time we open a hibernate session the COnfiguration factory would take care to load all the entity classes into memory based on the configuration files.Am i wrong here? or are the entity loaded only as and when required based on lazy initialization property?

2>However I would like to know if this happens, when are the entity classes garbage collected? if the application holds no references to the entity classes outside the DAO layer, are the entity classes garbage collected on closing the session? Just like it happens with jdbc ResultSet objects on closing a jdbc connection?

3> If so does it mean, on consequent opening of a session, all the entity classes loaded again?

Would appreciate any help, and I apologize before hand if the question or my understanding is stupid.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1>When we start an application, or the first time we open a hibernate session the COnfiguration factory would take care to load all the entity classes into memory based on the configuration files.Am i wrong here? or are the entity loaded only as and when required based on lazy initialization property?

When you configure a SessionFactory (not the first time you open a Session) the mappings are loaded. This is different from loading specific entity instances, so lazy initialization plays no part.


2>However I would like to know if this happens, when are the entity classes garbage collected? if the application holds no references to the entity classes outside the DAO layer, are the entity classes garbage collected on closing the session? Just like it happens with jdbc ResultSet objects on closing a jdbc connection?

Hibernate doesn't do any garbage collection. As with all Java objects, entities are garbage collected when the GC deems them safe to do so, ultimately when there are no references to the object. This might be when the Session is closed.


3> If so does it mean, on consequent opening of a session, all the entity classes loaded again?

All the entities you load in the course of a Session are loaded. These may be existing instances or may be new ones.

Remember, this is a database backed application, so GC is probably not the bottleneck you need to worry most about - that would typically be database round trips and inefficient SQL .


 
Stanley Walker
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Paul, clears up many of my doubts.
Just wanted to clarify one more issue, the second time i configure a sessionFactory.. will hibernate load all the entity classes again into memory? or only those which have already been garbage collected?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you configure a SesionFactory a second time?
 
Stanley Walker
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was considering the application that i am developing as a POC.
My entry point to the service is method in an ApplicationService class, in which at the outset i call a custom class which returns me a hibernate session and in the end i close the session. In case i access this method twice from my UI, what will happen?

code snippet:


what will happen if for some reason i invoke the above method twice? what will CustomHibernateSessionWrapper.getSession() perform? will it configure all the cfg files again?


}
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

what will CustomHibernateSessionWrapper.getSession() perform? will it configure all the cfg files again?

If you have written it to do this then yes, though this would be a mistake. Configuring a SessionFactory is a heavy-weight operation, you should do it once in your application's life cycle and reuse it. Opening/closing a Session is light weight.
 
Stanley Walker
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,
Thank you for your replies.
I was wondering the same thing myself.
Would it be an elegant solution if i check if an existing session exists, then return the same, if not open a new one.
Then during session.close() how do i ensure that only inserts/updates in the current context be committed or rollbacked?

I am sorry if i misunderstand anything, i am coming from a jdbc bckgrnd in which during server startup connection pool was open through a datasource. we used to simply retrieve the connections from this pool.

How can i ensure that i do not open concurrent session? if i enable connection pooling in hibernate, will the same hold true as in jdbc applications?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Would it be an elegant solution if i check if an existing session exists, then return the same, if not open a new one.

No. Like I said its the SessionFactory that is heavy weight and a thing you should configure only once. How you use the Session is down to the specifics of your application. You load entities into your Session, do what you need to do with them then close (or flush) your Session. That's the normal behaviour.

Think of a Session like you would a Connection (except that it also included useful caching and dirty checking that prevents database round trips). A SessionFactory is more like a DataSource. You wouldn't configure a DataSource every time you wanted to use it!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic