-->
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.  [ 13 posts ] 
Author Message
 Post subject: Changing the connection string at runtime
PostPosted: Thu Sep 18, 2008 9:25 am 
Beginner
Beginner

Joined: Thu Aug 14, 2008 5:31 pm
Posts: 20
Hi,

My config file is as followed :
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">MyConnectionString</property>
    <property name="show_sql">true</property>
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <mapping assembly="DataTransfer"/>
  </session-factory>
</hibernate-configuration>


Now I'm trying to change the connection string at runtime. I do it as followed :

Code:
private static ISession GetSession()
    {     
      Configuration cfg = new Configuration();
      cfg.SetProperty("hibernate.connection.connection_string", "AnotherConnectionString");
     
      ISessionFactory sessionFactory = cfg.BuildSessionFactory();
      cfg.Configure();
      ISession session = sessionFactory.OpenSession();
     
      return session;
    }


Once I called cfg.Configure(), the property "hibernate.connection.connection_string" is reset to what it was in the config file. So how can I force nhibernate to use the new connection string that I pass to the Configuration object in my code ?

Is there something I'm doing wrong ?


Last edited by graphicsxp on Sun Sep 21, 2008 4:46 am, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 18, 2008 9:35 am 
Beginner
Beginner

Joined: Thu Aug 14, 2008 5:31 pm
Posts: 20
duplicate message. sorry.


Last edited by graphicsxp on Sun Sep 21, 2008 4:46 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 21, 2008 4:28 am 
Beginner
Beginner

Joined: Thu Aug 14, 2008 5:31 pm
Posts: 20
Anyone has an idea about this ? It must be something lot of people do all the time ! I really need to find a solution urgently.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2008 2:43 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Quote:
Configure NHibernate using the <hibernate-configuration> section from the application config file, if found, or the file hibernate.cfg.xml otherwise.


Configure() actually (re-)reads the settings from the file. I think that's already done with "new Configuration()". Try without the explicit call.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2008 4:49 am 
Beginner
Beginner

Joined: Thu Aug 14, 2008 5:31 pm
Posts: 20
Thanks for helping ! I'm really getting frustrated with this.

Are you suggesting I should not call cfg.Configure(); ?

I've tried that but I get the following exception :

Quote:
The hibernate.connection.driver_class must be specified in the NHibernate configuration section.


So I've tried to redefine all the properties in the code as such :
Code:
cfg.SetProperty("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
        cfg.SetProperty("connection.driver_class", "NHibernate.Driver.SqlClientDriver");
        cfg.SetProperty("dialect", "NHibernate.Dialect.MsSql2005Dialect");
        cfg.SetProperty("hibernate.connection.connection_string", _cnnMgr.GetConnectionString("anotherConnectionString");
        cfg.SetProperty("connection.connection_string", _cnnMgr.GetConnectionString("anotherConnectionString");


But I'm still getting the same error...

I can't understand why SetProperty just doesnt do the job, as it should be its purpose to change a property at runtime (in my case the connection string)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2008 4:53 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Which NHibernate version do you use ? Is your hibernate config in App.config or hibernate.cfg.xml ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2008 4:55 am 
Beginner
Beginner

Joined: Thu Aug 14, 2008 5:31 pm
Posts: 20
I use version 1.2.1.4000

My config is in hibernate.cfg.xml.

Does this help ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2008 5:00 am 
Beginner
Beginner

Joined: Thu Aug 14, 2008 5:31 pm
Posts: 20
I use version 1.2.1.4000

My config is in hibernate.cfg.xml.

Does this help ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2008 5:05 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I just checked the docs. Seems that you don't need Configure() when you use App.config. With hibernate.cfg.xml you need it. Have you tried setting the property first ?

Code:
Configuration cfg = new Configuration();
cfg.SetProperty(...);
ISessionFactory sessionFactory = cfg.Configure().BuildSessionFactory();

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2008 5:22 am 
Beginner
Beginner

Joined: Thu Aug 14, 2008 5:31 pm
Posts: 20
If I do it as you say, I got no exception raised.

After cfg.SetProperty is called, the connection string in cfg is the one I'm setting in the code, which is what I want.

However as soon as cfg.Configure().BuildSessionFactory() is called, the connection string is reset to the one in the hbiernate.cfg.xml file.

This is crazy... :(


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2008 6:40 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Oops, sorry, my example isn't doing what I was talking about. I meant you should try and call Configure() and then set the connection string.

Code:
Configuration cfg = new Configuration();
cfg.Configure().SetProperty(...);
ISessionFactory sessionFactory = cfg.BuildSessionFactory();

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2008 6:48 am 
Beginner
Beginner

Joined: Thu Aug 14, 2008 5:31 pm
Posts: 20
Yes ! That seems to have done the trick !

Thanks a lot for your help, much appreciated.

Actually would you mind taking a quick look at my other issue (http://forum.hibernate.org/viewtopic.php?t=990735), maybe you know the answer...


cheers


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 23, 2009 3:55 am 
Regular
Regular

Joined: Wed Feb 11, 2009 10:58 am
Posts: 55
thanks needed that, too :)


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