1.如果存在事务所有操作都走主库,不存在事务 修改、写入、删除走主库,查询操作走从库
2.HitRate 越大走这个从库的概率越大
假设A从库 HitRate设置为5
假设B从库 HitRate设置为10
A的概率为33.3% B的概率为66.6%
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString,//主库 DbType = DbType.SqlServer, IsAutoCloseConnection = true, //从库 SlaveConnectionConfigs = new List<SlaveConnectionConfig>() { new SlaveConnectionConfig() { HitRate=10, ConnectionString=Config.ConnectionString2 } , new SlaveConnectionConfig() { HitRate=10, ConnectionString=Config.ConnectionString2 } } });
ORM是只把读和写进行了请求处理,数据库的主从复制,数据库的同步需要去数据库里面配置
例如: SqlServer有订阅 或者 alwayson等
1、新功能:5.0.8 用法和Queryable一样
Db.MasterQueryable<T>().Where(x=>x.id==1).ToList() //强制走主库 //5.1.3.22修复了异步不会生效
2、事务
//事务中全部会走主库
3、通过配置走主库
//方式1.强制配置 db.Ado.IsDisableMasterSlaveSeparation=true
上面是默认开启读写分离,反向模式是相反的,默认关闭读写分离
5.0.8.6-preview02
//SqlSugarClient SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString,//主库 DbType = DbType.SqlServer, IsAutoCloseConnection = true, //从库 SlaveConnectionConfigs = new List<SlaveConnectionConfig>() { new SlaveConnectionConfig() { HitRate=10, ConnectionString=Config.ConnectionString2 } , new SlaveConnectionConfig() { HitRate=10, ConnectionString=Config.ConnectionString2 } } }); db.Ado.IsDisableMasterSlaveSeparation=true;//全局禁止读写分离 //SqlSugarScope static SqlSugarScope db = new SqlSugarScope (new ConnectionConfig() { ConnectionString = Config.ConnectionString,//主库 DbType = DbType.SqlServer, IsAutoCloseConnection = true, //从库 SlaveConnectionConfigs = new List<SlaveConnectionConfig>() { new SlaveConnectionConfig() { HitRate=10, ConnectionString=Config.ConnectionString2 } , new SlaveConnectionConfig() { HitRate=10, ConnectionString=Config.ConnectionString2 } } },db=>{ //写在全局配置里面 db.Ado.IsDisableMasterSlaveSeparation=true;//全局禁止读写分离 }); //强制查询从库(注意:事务中还是会查主库) db.SlaveQueryable<Order>().ToList();//用法和Queryable一样、 //默认查主库 db.Queryable<Order>().ToList();
SqlSugarScope在单例模式下存密码失效,升级到5.0.5.1
SqlSugarClient 没有问题
2016 © donet5.comApache Licence 2.0