亚音速3.0.0.3多个数据库连接故障转移 [英] subsonic 3.0.0.3 multiple database connection failover

查看:213
本文介绍了亚音速3.0.0.3多个数据库连接故障转移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC和亚音速3.0.0.3,但我似乎无法牵制多个数据库连接的特定点。

am using MVC and Subsonic 3.0.0.3 but i cant seem to pin down a specific point for multiple database connection.

通常在正常.NET我有我的web.config文件中的2个字符串
并且有一个数据库类为我的项目,该DB类中我会做这样的事情:

normally in normal .net i would have my 2 strings in the web.config file and have a database class for my project, within this db class i would do something like this:

try
        {
            conn.ConnectionString = server1;
            conn.Open();
        }
        catch (MySqlException)
        {
            conn.ConnectionString = server2;
            conn.Open();
        }

我想牵制在亚音速的创建文件的一个地方,这样的事情将是最好的地方,也许一个最新的例子就如何实现这一目标。我用Google搜索等,但显示的例子是一个较旧的亚音速。

I am trying to pin down the one place in subsonic's created files where something like this would be best to place and maybe an up to date example on how to achieve it. I have googled etc but the examples shown are for an older subsonic.

非常感谢

推荐答案

如果您在Context.tt在第35行看看,你会看到下面的code:

If you look in Context.tt at line 35 you'll see the following code:

public <#=DatabaseName#>DB() 
{ 
    DataProvider = ProviderFactory.GetProvider("<#=ConnectionStringName#>");
    Init();
}

这是那里的供应商越来越为你设置,所以如果你在Settings.ttinclude在第20行中的connectionStringName后添加BackupConnectionStringName变量,那么你应该能够检查你的连接工作和用户的后备如果不是。例如:

This is where the provider is getting setup for you so if you add a BackupConnectionStringName variable in Settings.ttinclude after the ConnectionStringName at line 20 then you should be able to check your connection is working and user your fallback if not. For example:

public <#=DatabaseName#>DB() 
{ 
    DataProvider = ProviderFactory.GetProvider("<#=ConnectionStringName#>");
    Init();
    try
    {
        DataProvider.CreateConnection();
    }
    catch(SqlException)
    {
       DataProvider = ProviderFactory.GetProvider("<#=BackupConnectionStringName#>");
       Init(); 
    }
}

NB您可能需要做一些清理,以确保连接不剩通过创建连接开放。

NB You may need to do some clean up to make sure a connection is not left open by CreateConnection.

这篇关于亚音速3.0.0.3多个数据库连接故障转移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆