-->
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 get SessionFactory class's object with multi database
PostPosted: Tue Feb 21, 2006 12:04 pm 
Newbie

Joined: Tue Feb 21, 2006 11:31 am
Posts: 3
Hi everybody.

In my code. I need to connect multi databases at same time. I readed Hibernate manual. for only one db. I need to create a Hibernate.cfg.xml and a class to get a SessionFactory type object.

but for multi database. I think the most simple way is like this.

public class HibernateSessionUtil {
private static SessionFactory sessionFactory1;
private static SessionFactory sessionFactory2;
private static SessionFactory sessionFactory3;


public static Session currentSession(String fileName)
throws HibernateException {
if (filename == "filename1" ) {create sessionFactory1};
else if (filename == "filename2" ) {create sessionFactory2};
else if (filename == "filename3" ) {create sessionFactory3};
else return null;
}
}

but I don't think it is a good way. because it is ugly.
who can give me some ideas make those code flexible and clean.

Thanks

Joey


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 21, 2006 1:56 pm 
Beginner
Beginner

Joined: Wed Feb 08, 2006 5:45 pm
Posts: 23
Location: Phoenix, AZ
Unfortunately, according to the Advanced User's FAQ "You must configure multiple SessionFactory instances" to use multiple DBs.

The way you have described is one way of implementing it (however, I'm not sure why you're 'creating' a sessionFactory and returning it when you're getting a Session). I had the same problem myself once and I ended up making a registry (a map with key = classname and value = sessionFactory (I had each SessionFactory in it's own Class implementing an interface so I can call SessionFactory.getSession() without having to reference a specific SessionFactory)). With this way, you can see if the Object is-a classname and if it is, use the SessionFactory mapped to it.

Hope this helps,
-B

_________________
Please rate me if you found my post useful.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 22, 2006 10:10 am 
Newbie

Joined: Tue Feb 21, 2006 11:31 am
Posts: 3
Hi BLudkiewicz:

Thanks for your reply.

Joey


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.