-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: How many SessionFactory i can use in one application?
PostPosted: Tue Sep 15, 2009 5:26 am 
Newbie

Joined: Sun Sep 13, 2009 12:56 am
Posts: 4
Hi

In one application, how many SessionFactory i can use ?


Top
 Profile  
 
 Post subject: Re: How many SessionFactory i can use in one application?
PostPosted: Tue Sep 15, 2009 8:14 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
A SessionFactory is pretty heavyweight, so, we recommend creating one and caching it in a singleton type of way. Then, you can create as many Session objects from it as you like.

http://www.hiberbook.com/HiberBookWeb/l ... hhibernate

Quote:
The SessionFactory and Resource Allocation

The SessionFactory itself is a fairly resource intensive object to create. In the first few Hibernate tutorials on this website, we'll typically create SessionFactory objects whenever we need them, but in practice, it is imperative to control how often the SessionFactory gets created. Minimizing the number of SessionFactory objects you create can be done by perhaps making the SessionFactory a static variable that is accessed through a Singleton design pattern, or better yet, in a J2EE environment, simply have the SessionFactory bound to, and accessed through, the JNDI naming service of the J2EE application server. You really only need one SessionFactory in any application. Controlling the number of SessionFactory objects that get created, and for that matter, destroyed, will help to minimize unnecessary resource allocations.

On the other hand, the Hibernate Session, which is produced by the SessionFactory, is a very efficient object that you don't have to feel guilty about creating willy-nilly. A single SessionFactory is great at efficiently pumping out Hibernate Session objects whenever you need them. Limit the number of SessionFactory objects that are created in an enterprise application to one, but feel guilt-free about generating Hibernate Sessions whenever you need them.

Sessions and the org.hibernate.SessionFactory

It doesn't take a genius to figure out that the job of the SessionFactory is to pump out Hibernate Session objects. Obtaining the Session is actually a pretty straight forward endeavor once you have access to the SessionFactory, as you simply have to invoke the factory's getCurrentSession() method.


Code:
AnnotationConfiguration config =
                    new AnnotationConfiguration();
config.addAnnotatedClass(User.class);
config.configure();
// new SchemaExport(config).create(true, true);
SessionFactory factory = config.buildSessionFactory();
Session session = factory.getCurrentSession();
/* a session is around, but
the User is still transient! */
User u = new User();  u.setPassword("abc123");


http://www.hiberbook.com/HiberBookWeb/l ... hhibernate

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.