CodeSmith Community
Your Code. Your Way. Faster!

Runtime Connection Switching

Latest post 07-21-2008 12:15 PM by blake05. 20 replies.
  • 09-07-2006 1:16 AM In reply to

    • NetFan
    • Top 500 Contributor
    • Joined on 07-20-2006
    • Posts 10
    • Points 270

    Re: Runtime Connection Switching

    Hi,Robert

       thanks a lot for you help!

       I did it what you told me to do. But I still got the errors.

       Could you chang the ConnectionString to an invalid database in the web.config,  then you add a correct ConnectionString in the Page_Init, and Change the DataSourceControl's Provider, then you'll find the errors.

      17279, 17280 for you reference!
      thanks again!

    NetFan

    • Post Points: 5
  • 11-07-2007 9:01 AM In reply to

    • JoshGough
    • Not Ranked
    • Joined on 11-06-2007
    • Posts 3
    • Points 15

    Re: Runtime Connection Switching

    Hey Bobby,

    I was wondering if you ever figured out a new way for this situation. I think I have a similar situation described here:

     

    http://community.codesmithtools.com/forums/p/7596/28519.aspx#28519

     

    Thanks!

    Josh 

     

     

    • Post Points: 5
  • 07-08-2008 5:36 AM In reply to

    • jota
    • Not Ranked
    • Joined on 10-27-2006
    • Lisboa, Portugal
    • Posts 4
    • Points 45

    Re: Runtime Connection Switching

    Hello, I'm having some problems with this feature, maybe someone can help.

    Here's my setup: I'm using Nettiers 2.2 and a Domain Model, with EntLib 3.1. I am just generating the data access, no web service or web interfaces.

    I have a single connection string in the app.config, and in my code I can successfully change the connection string dynamically:

    ConnectionScope.Current.ConnectionStringKey = "mySecondConnectionString";
    ConnectionScope.Current.DynamicConnectionString = "Data Source=localhost;Initial Catalog=MyDatabase;Integrated Security=True";
    MyDataAccessCall();

    Problems are:

    1) how do I get back to the default database (the one configured in the app.config file)? I tried setting the ConnectionStringKey to the name of the CS Key in app.config, and it doesn't work. This seems to be related to the following:

     2) how do I change to another database after having done a data access with a dynamic connection string?

     It seems that from the moment I change the connection scope and do a data access, the data source is locked. So if I do:

    ConnectionScope.Current.ConnectionStringKey = "mySecondConnectionString";
    ConnectionScope.Current.DynamicConnectionString = "Data Source=localhost;Initial Catalog=MyDatabase;Integrated Security=True";
    MyDataAccessCall();

    ConnectionScope.Current.ConnectionStringKey = "myThirdConnectionString";
    ConnectionScope.Current.DynamicConnectionString = "Data Source=localhost;Initial Catalog=MyThirdDatabase;Integrated Security=True";
    MyDataAccessCall();

    The second "MyDataAccessCall()" still gets data from MyDatabase, and not MyThirdDatabase.

    Any ideas?

    Note: "MyDataAccessCall()" just includes a GetAll() call such as this:

     TList<MyNS.Domain.MyEntity> partners = MyNS.Domain.MyEntity.GetAll();

     Any help is greatly welcome. Thanks in advance.

    João Pedro Martins

    • Post Points: 5
  • 07-17-2008 12:29 PM In reply to

    • jota
    • Not Ranked
    • Joined on 10-27-2006
    • Lisboa, Portugal
    • Posts 4
    • Points 45

    Re: Runtime Connection Switching

    I've been debugging through the code, and it seems to be the "problem" is in ConnectionScope.cs (SqlClient project), in the "DataProvider" property.

    The comment at the top states:

            /// To use a dynamic connection, you must set both the
            /// DynamicConnectionString and a unique ConnectionStringKey properties;

    However, the code only looks for this dynamic connection string if dataProvider == null. Since I'm doing sequential accesses to different databases, dataProvider is != null, and my dynamic connection setting is ignored.

    Am I correct? and is there a way around this behaviour?

    • Post Points: 5
  • 07-18-2008 6:04 AM In reply to

    • jota
    • Not Ranked
    • Joined on 10-27-2006
    • Lisboa, Portugal
    • Posts 4
    • Points 45

    Re: Runtime Connection Switching

    On a second note: doing dynamic database access at the service or domain level requires at least two calls: setting the connection string key and then doing the data access. This approach doesn't seem to work in multithreading scenarios unless I do locks in my multithreaded app. I'm using BizTalk Server with NetTiers, and these problems keep creeping up. Ideas?

    • Post Points: 35
  • 07-21-2008 12:15 PM In reply to

    • blake05
    • Top 25 Contributor
    • Joined on 04-03-2008
    • Wisconsin
    • Posts 351
    • Points 6,205

    Re: Runtime Connection Switching

    Hello

    I've never had to do what your implementing... but I was curious and still trying to figure out why you would need to switch datasources at runtime. There are many better ways to go about updating a clustered enviroment...

    Thanks

    -Blake Niemyjski

    Blake Niemyjski

    CodeSmith Tools, LLC Support Specialist

    Blog: http://windowscoding.com/blogs/blake/

    ----------------------------------------------------------------------
     Member of the .NetTiers team | Visit http://www.nettiers.com
    ----------------------------------------------------------------------

    • Post Points: 5
Page 2 of 2 (21 items) < Previous 1 2 | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems