This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Where do I initialize and store Hibernates SessionFactory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Where do I initialize and store Hibernates SessionFactory" Watch "Where do I initialize and store Hibernates SessionFactory" New topic
Author

Where do I initialize and store Hibernates SessionFactory

david allen
Ranch Hand

Joined: Sep 27, 2002
Posts: 185
I am working on a web application using tomcat 5 and hibernate. However, I am unsure about the creation and initialization of SessionFactory.

Do I create the SessionFactory once at startup and then store it in the Servlet context? Or is it ok to initialize the SessionFactory with each request? (This does not sound right).

I am using HibernateUtil. The initialization of the SessionFactory is in the static block. Does this mean that the SessionFactory is only every initialized once or does it mean it is initialized each time a new thread accesses it? i.e. local thread.

Thank you for your help.
david
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Configuring a SessionFactory is a big job, so only do it once. It doesn't make any sense to do this for every request; your data model will remain pretty static. I don't know how your HibernateUtil class will do it (where is it from?).


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
david allen
Ranch Hand

Joined: Sep 27, 2002
Posts: 185
I am using HibernateUtil.java which is found on the Hibernate web site.

Regards

David
 
GeeCON Prague 2014
 
subject: Where do I initialize and store Hibernates SessionFactory