-->
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.  [ 3 posts ] 
Author Message
 Post subject: how to dynamically configure a SessionFactory at runtime
PostPosted: Tue Dec 06, 2005 12:36 pm 
Newbie

Joined: Mon Aug 15, 2005 3:57 pm
Posts: 13
I have a helper class that uses the ThreadLocal Session pattern to generate the SessionFactory. However, I need to dynamically configure the mySQL database that hibernate connects to so the SessionFactory in my helper class isn't declared static. Instead I instantiate a HibernateHelper object in my main app, passing the database name to connect to. I'm curious if this is an acceptable way to do things, are there any negative side effects, or if there is a better way to dynamically configure a SessionFactory at runtime. Here's my helper class:

thanks in advance!

public class HibernateHelper
{
public HibernateHelper(String dbNameIn) // database to connect to
{
connectURL = connectURL + dbNameIn;
sessionFactory = new Configuration()
.setProperty("hibernate.connection.url", connectURL)
.configure()
.buildSessionFactory();
}

public Session getSession()
{
Session session = (Session) HibernateHelper.session.get();
if (session == null)
{
session = sessionFactory.openSession();
HibernateHelper.session.set(session);
}
return session;
}

private String connectURL = "jdbc:mysql://localhost/";
private static final ThreadLocal session = new ThreadLocal();
private static final ThreadLocal transaction = new ThreadLocal();
// SessionFactory would normally be static but not here since the configuration
// is determined at runtime
private final SessionFactory sessionFactory;
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 06, 2005 2:08 pm 
Beginner
Beginner

Joined: Wed Apr 13, 2005 10:34 am
Posts: 38
I think this is quite common for supporting multiple databases anyway. I'm using a similar approach, storing one "HibernateHelper" instance per database in a static map.

No problems so far...


Top
 Profile  
 
 Post subject: Glad to hear of your positive experience with this approach!
PostPosted: Tue Dec 06, 2005 2:15 pm 
Newbie

Joined: Mon Aug 15, 2005 3:57 pm
Posts: 13
Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.