You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
929 B
C#

using System.Linq;
using SqlSugar;
namespace EntrustSettle.Common.DB.Aop;
public class SqlSugarReuse
{
public static void AutoChangeAvailableConnect(SqlSugarClient db)
{
if (db == null) return;
// 注意如果程序启动时数据库Main无法连接包括未建库的情况则IsValidConnection为false。如果启用了会自动切换到数据库Main2
if (db.Ado.IsValidConnection()) return;
if (!BaseDBConfig.ReuseConfigs.Any()) return;
foreach (var connectionConfig in BaseDBConfig.ReuseConfigs)
{
var config = db.CurrentConnectionConfig.ConfigId;
db.ChangeDatabase(connectionConfig.ConfigId);
//移除旧的连接,只会在本次上下文移除,因为主库已经故障会导致多库事务无法使用
db.RemoveConnection(config);
if (db.Ado.IsValidConnection()) return;
}
}
}