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: Connecting to multiple databases
PostPosted: Sat Aug 12, 2006 9:12 pm 
Newbie

Joined: Sat Aug 12, 2006 8:59 pm
Posts: 1
Hi,
I have the following problems with Nhibernate :

a) I need to connect to multiple databases in my web application and my connection string is determined by users belonging to different regions.. So how to i change the connection string on a session.

I have my Nhibernate objects mapped in different assemblies for example, i have my logon related objects in Core and rest for other object in App. when a user visits a site, iam opening a session in global.asax session start method. This works fine, but when i try to change a connection string on the session based on the user's region from the backend DB, i get an exception as unknow entity xxx for each object that is not in the Core since iam trying to refer to object in the other assembly.

SO how do i change the assembly and connection string on the Session programattically wherein the connection string info comes from a CoreDB and i need to make use of this connection string with objects from other assembly.

Can anybody send me code that programmatically changes session properties on a single session. If it is not possible to change properties on a session and use the same session object, can i have some Session Manager kind of stuff where i can create a new session and change its connection properties dynamically.

any helpis appreciated


Top
 Profile  
 
 Post subject: Re: Connecting to multiple databases
PostPosted: Tue Aug 15, 2006 9:53 am 
Beginner
Beginner

Joined: Wed Aug 03, 2005 8:06 am
Posts: 40
Location: Netherlands
You can not change the assembly of a session. This would allow as session to do an ‘expensive’ job that you want to do only once when creating the NHibernate Configuration. However, it is possible to change the connection properties of a Configuration and then get a new session from its session factory, so you don't have to change the session's connection properties.
We did this in a small conversion project where we had to move data from one database to another, hope this snippet will help:

this._cfg.Properties.Clear();
this._cfg.Properties.Add("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
switch (provider)
{
case Provider.MsSql2000:
this._cfg.Properties.Add("hibernate.dialect", "NHibernate.Dialect.MsSql2000Dialect");
this._cfg.Properties.Add("hibernate.connection.driver_class", "NHibernate.Driver.SqlClientDriver");
this._cfg.Properties.Add("hibernate.connection.connection_string", connectionString);
break;
case Provider.MsAccess:
this._cfg.Properties.Add("hibernate.dialect","NHibernate.JetDriver.JetDialect, NHibernate.JetDriver");
this._cfg.Properties.Add("hibernate.connection.driver_class", "NHibernate.JetDriver.JetDriver, NHibernate.JetDriver");
string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + connectionString;
this._cfg.Properties.Add("hibernate.connection.connection_string", conString);
break;
}

where _cfg is a singleton NHibernate Configuration instance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 12:11 pm 
Beginner
Beginner

Joined: Fri Sep 02, 2005 12:13 pm
Posts: 44
Location: Denver, CO
You'd probably be interested in reading this thread as well: http://forum.hibernate.org/viewtopic.ph ... +databases

Billy


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.