-->
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.  [ 4 posts ] 
Author Message
 Post subject: Calling buildSessionFactory() many many times
PostPosted: Wed Feb 11, 2004 6:44 pm 
Beginner
Beginner

Joined: Thu Oct 23, 2003 1:43 pm
Posts: 23
Hi,

We have a class that does multiple methods like insert,update,delete and so forth. Each of these methods has different mapping based on the bussines logic. I heard that initializing buildsessionfactory in each method leads to a problem and the suggestion was to initialize them while application starts up by specifying all the hbm files like the below:

What is the difference between the two way of building sessionfactory.


Way 1: (Just once somewhere in the init of startup servlet)
sf = new Configuration().addClass(Education.class)
.addClass(Resume.class)
.addClass(Role.class)
.addClass(Skill.class)
.addClass(SkillGroup.class)
.addClass(User.class)
.addClass(UserRole.class)
.buildSessionFactory();

Way 2: (Application calls this method as and when requred)
[i]
[b]Is there anything wrong if we do:[/b][/i]
public void add(User user){

Configuration cfg = null;
SessionFactory sessions = null;
Session sess = null;

try {
cfg = new Configuration();
cfg.addClass(User.class)

sessions = cfg.buildSessionFactory();
sess = sessions.openSession();
}
catch (Exception e)
{
throw(new DatabaseException(e));
}

try {
sess.saveOrUpdate(user);
sess.flush();
sess.connection().commit();
}
catch (Exception e) {
throw(new DatabaseException(e));
}
finally {
try {
sess.close();
} catch(Exception e) {
throw(new DatabaseException(e));
}
}
}

Thanks a bunch


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 6:46 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You should really build the session factory only once, it is heavyweight, and building it every time will really KILL your performance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 7:04 pm 
Beginner
Beginner

Joined: Thu Oct 23, 2003 1:43 pm
Posts: 23
Hi,
Thanks for your immediate response. The example that I gave you in the last post is just for the sake of discussion. In reality, we have many many hbm files with different hierarchy. I'm just concerned it might be a problem to specify all hbm files at once with different hierarchy and build the session factory.

Eg:

In one package, we have 5 hbm files. Class A contains collection of Class B which contains collection of Class C and so forth. In another package we have similar hierarchy. As per you suggestion, I guess doing this way is not a problem, am I right?

sf = new Configuration().addClass(pkg1.A.class)
.addClass(pkg1.A.class)
.addClass(pkg1.A.class)
.addClass(pkg2.B.class)
.addClass(pkg2.B.class)
.addClass(pkg2.B.class)
.addClass(pkg2.B.class)
.buildSessionFactory();

Best Regards.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 7:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
This should not be a problem, they are different classes after all.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.