• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Loading and Garbage Collection of EntityClasses by Hibernate

 
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 101
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you configure a SesionFactory a second time?
 
Stanley Walker
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 101
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic