// 使用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)
评论已关闭