SqlSugar框架连接达梦主备实现高可用及解决程序高并发条件下与数据库断连的方法
    		       		warning:
    		            这篇文章距离上次修改已过427天,其中的内容可能已经有所变动。
    		        
        		                
                
// 使用SqlSugar框架配置达梦数据库的高可用方案
public class SqlSugarDbContext
{
    private static SqlSugarClient db;
 
    // 单例模式确保全局只有一个实例
    public static SqlSugarClient Instance
    {
        get
        {
            if (db == null)
            {
                // 配置主库
                ConnectionConfig config = new ConnectionConfig()
                {
                    ConnectionString = "主库连接字符串",
                    DbType = DbType.Dm,
                    IsAutoCloseConnection = true,
                    InitKeyType = InitKeyType.Attribute
                };
 
                // 配置备库
                List<IClient> slaveClients = new List<IClient>
                {
                    new SqlSugarClient(new ConnectionConfig()
                    {
                        ConnectionString = "备库1连接字符串",
                        DbType = DbType.Dm,
                        IsAutoCloseConnection = true,
                        InitKeyType = InitKeyType.Attribute
                    }),
                    new SqlSugarClient(new ConnectionConfig()
                    {
                        ConnectionString = "备库2连接字符串",
                        DbType = DbType.Dm,
                        IsAutoCloseConnection = true,
                        InitKeyType = InitKeyType.Attribute
                    })
                };
 
                // 实例化SqlSugarClient并配置高可用
                db = new SqlSugarClient(new ClientInfo()
                {
                    IsAutoCloseConnection = true,
                    MasterDbConfig = config,
                    SlaveDbs = slaveClients
                });
            }
            return db;
        }
    }
 
    // 自定义高并发条件下的数据库重连机制
    public static void Reconnect()
    {
        var reconnectTimes = 0;
        while (true)
        {
            try
            {
                using (var db = Instance)
               
评论已关闭