From 02c0b845ef2804750da5ba9207b99eabc0c25f50 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Tue, 24 Oct 2017 16:19:14 +0800 Subject: [PATCH] Update MasterSlave Demo --- Src/Asp.Net/SqlServerTest/Config.cs | 2 + .../SqlServerTest/Demos/A_MasterSlave.cs | 38 ++++++++++++++++-- Src/Asp.Net/SqlServerTest/Program.cs | 1 + .../Abstract/AdoProvider/AdoProvider.cs | 39 +++++++++++++++++-- Src/Asp.Net/SqlSugar/Utilities/UtilRandom.cs | 2 +- 5 files changed, 74 insertions(+), 8 deletions(-) diff --git a/Src/Asp.Net/SqlServerTest/Config.cs b/Src/Asp.Net/SqlServerTest/Config.cs index 540bf35ba..7d87d0ee6 100644 --- a/Src/Asp.Net/SqlServerTest/Config.cs +++ b/Src/Asp.Net/SqlServerTest/Config.cs @@ -9,5 +9,7 @@ namespace OrmTest public class Config { public static string ConnectionString = "server=.;uid=sa;pwd=sasa;database=SqlSugar4XTest"; + public static string ConnectionString2 = "server=.;uid=sa;pwd=sasa;database=SQLSUGAR4XTEST"; + public static string ConnectionString3 = "server=.;uid=sa;pwd=sasa;database=sqlsugar4xtest"; } } diff --git a/Src/Asp.Net/SqlServerTest/Demos/A_MasterSlave.cs b/Src/Asp.Net/SqlServerTest/Demos/A_MasterSlave.cs index 943ea2a62..b02745601 100644 --- a/Src/Asp.Net/SqlServerTest/Demos/A_MasterSlave.cs +++ b/Src/Asp.Net/SqlServerTest/Demos/A_MasterSlave.cs @@ -1,11 +1,43 @@ -using System; +using OrmTest.Demo; +using OrmTest.Models; +using SqlSugar; +using System; using System.Collections.Generic; using System.Linq; using System.Text; -namespace OrmTest.Demos +namespace OrmTest.Demo { - public class MasterSlave + public class MasterSlave : DemoBase { + + public static void Init() + { + var db = GetMasterSlaveInstance(); + for (int i = 0; i < 10; i++) + { + var list = db.Queryable().ToList(); // ConnectionString2 or ConnectionString3 + } + db.Insertable(new Student() { Name = "masterTest" }).ExecuteCommand();// Config.ConnectionString + } + + public static SqlSugarClient GetMasterSlaveInstance() + { + SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() + { + ConnectionString = Config.ConnectionString, + DbType = DbType.SqlServer, + IsAutoCloseConnection = true, + SlaveConnectionStrings = new List() { + new SlaveConnectionConfig() { HitRate=10, ConnectionString=Config.ConnectionString2 }, + new SlaveConnectionConfig() { HitRate=30, ConnectionString=Config.ConnectionString3 } + } + }); + db.Aop.OnLogExecuting = (sql, pars) => + { + Console.WriteLine(db.Ado.Connection.ConnectionString); + }; + return db; + } } } diff --git a/Src/Asp.Net/SqlServerTest/Program.cs b/Src/Asp.Net/SqlServerTest/Program.cs index 0eaa709c3..60b961997 100644 --- a/Src/Asp.Net/SqlServerTest/Program.cs +++ b/Src/Asp.Net/SqlServerTest/Program.cs @@ -46,6 +46,7 @@ namespace OrmTest OrmTest.Demo.ComplexModel.Init(); OrmTest.Demo.CodeFirst.Init(); OrmTest.Demo.Aop.Init(); + OrmTest.Demo.MasterSlave.Init(); } } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs index 7c051139a..0d1fac5cc 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs @@ -75,6 +75,16 @@ namespace SqlSugar { this.Connection.Close(); } + if (this.IsMasterSlaveSeparation) + { + foreach (var slaveConnection in this.SlaveConnections) + { + if (slaveConnection != null && slaveConnection.State == ConnectionState.Open) + { + slaveConnection.Close(); + } + } + } } public virtual void Dispose() { @@ -92,6 +102,17 @@ namespace SqlSugar this.Connection.Dispose(); } this.Connection = null; + + if (this.IsMasterSlaveSeparation) + { + foreach (var slaveConnection in this.SlaveConnections) + { + if (slaveConnection != null && slaveConnection.State == ConnectionState.Open) + { + slaveConnection.Dispose(); + } + } + } } public virtual void CheckConnection() { @@ -670,7 +691,7 @@ namespace SqlSugar } private void SetConnectionStart(string sql) { - if (this.IsMasterSlaveSeparation&&IsRead(sql)) + if (this.IsMasterSlaveSeparation && IsRead(sql)) { if (this.MasterConnection == null) { @@ -681,19 +702,29 @@ namespace SqlSugar var currentSaveConnection = saves[currentIndex]; this.Connection = null; this.Context.CurrentConnectionConfig.ConnectionString = currentSaveConnection.ConnectionString; - var connection = this.SlaveConnections.FirstOrDefault(it => it.ToString() == currentSaveConnection.ConnectionString); - if (connection == null) + this.Connection = this.Connection; + if (this.SlaveConnections.IsNullOrEmpty() || !this.SlaveConnections.Any(it => EqualsConnectionString(it.ConnectionString, this.Connection.ConnectionString))) { + if (this.SlaveConnections == null) this.SlaveConnections = new List(); this.SlaveConnections.Add(this.Connection); } } } + + private bool EqualsConnectionString(string connectionString1, string connectionString2) + { + var connectionString1Array = connectionString1.Split(';'); + var connectionString2Array = connectionString2.Split(';'); + var result = connectionString1Array.Except(connectionString2Array); + return result.Count() == 0; + } + private void SetConnectionEnd() { if (this.IsMasterSlaveSeparation) { this.Connection = this.MasterConnection; - this.Context.CurrentConnectionConfig.ConnectionString = this.MasterConnection.ToString(); + this.Context.CurrentConnectionConfig.ConnectionString = this.MasterConnection.ConnectionString; } } diff --git a/Src/Asp.Net/SqlSugar/Utilities/UtilRandom.cs b/Src/Asp.Net/SqlSugar/Utilities/UtilRandom.cs index 916fb47e8..5a5111996 100644 --- a/Src/Asp.Net/SqlSugar/Utilities/UtilRandom.cs +++ b/Src/Asp.Net/SqlSugar/Utilities/UtilRandom.cs @@ -13,7 +13,7 @@ namespace SqlSugar int maxValue = 0; foreach (var item in pars) { - maxValue = +item.Value; + maxValue += item.Value; } var num = Random.Next(1, maxValue); var result = 0;