From 39a8bbc0dc3e2ce0df95958edb9dc690ca82b4ee Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Fri, 27 Oct 2017 15:26:01 +0800 Subject: [PATCH] Support Shard Same Thread --- .../SqlServerTest/Demos/B_SharedConnection.cs | 87 +++++++++++++++++++ Src/Asp.Net/SqlServerTest/Program.cs | 1 + .../SqlServerTest/SqlServerTest.csproj | 1 + .../SqlSugar/Entities/ConnectionConfig.cs | 2 +- .../Infrastructure/SqlSugarAccessory.cs | 36 +++++++- Src/Asp.Net/SqlSugar/SqlSugarClient.cs | 51 +++++------ Src/Asp.Net/SqlSugar/Utilities/CallContext.cs | 3 +- 7 files changed, 152 insertions(+), 29 deletions(-) create mode 100644 Src/Asp.Net/SqlServerTest/Demos/B_SharedConnection.cs diff --git a/Src/Asp.Net/SqlServerTest/Demos/B_SharedConnection.cs b/Src/Asp.Net/SqlServerTest/Demos/B_SharedConnection.cs new file mode 100644 index 000000000..d774d05f2 --- /dev/null +++ b/Src/Asp.Net/SqlServerTest/Demos/B_SharedConnection.cs @@ -0,0 +1,87 @@ +using OrmTest.Models; +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace OrmTest.Demo +{ + public class SharedConnection : DemoBase + { + public static void Init() + { + StudentDal studentDal = new StudentDal(); + SchoolDal schoolDal = new SchoolDal(); + + try + { + studentDal.BeginTran(); + + Console.WriteLine("school Count:"+ schoolDal.GetSchoolCount());//0 + + studentDal.AddStudent(new Student() { Name = "StudentTest" }); + schoolDal.AddSchool(new School() { Name = "SchoolTest" });//1 + + Console.WriteLine("school Count:" + schoolDal.GetSchoolCount()); + + throw new Exception("error"); + } + catch (Exception ex) + { + studentDal.RollbackTran(); + Console.WriteLine("school Count:" + schoolDal.GetSchoolCount());//0 + } + } + + + } + public class StudentDal : BaseDao + { + public void AddStudent(Student sudent) + { + db.Insertable(sudent).ExecuteCommand(); + } + } + public class SchoolDal : BaseDao + { + public void AddSchool(School school) + { + db.Insertable(school).ExecuteCommand(); + } + public int GetSchoolCount() + { + return db.Queryable().Count(); + } + } + + public class BaseDao + { + + public SqlSugar.SqlSugarClient db { get { return GetInstance(); } } + public void BeginTran() + { + db.Ado.BeginTran(); + } + public void CommitTran() + { + db.Ado.CommitTran(); + } + public void RollbackTran() + { + db.Ado.RollbackTran(); + } + public SqlSugarClient GetInstance() + { + SqlSugarClient db = new SqlSugarClient( + new ConnectionConfig() { + ConnectionString = Config.ConnectionString, + DbType = DbType.SqlServer, + IsAutoCloseConnection = false, + IsShardSameThread= true /*Shard Same Thread*/ + }); + + return db; + } + } +} diff --git a/Src/Asp.Net/SqlServerTest/Program.cs b/Src/Asp.Net/SqlServerTest/Program.cs index 60b961997..ce73bbb5d 100644 --- a/Src/Asp.Net/SqlServerTest/Program.cs +++ b/Src/Asp.Net/SqlServerTest/Program.cs @@ -47,6 +47,7 @@ namespace OrmTest OrmTest.Demo.CodeFirst.Init(); OrmTest.Demo.Aop.Init(); OrmTest.Demo.MasterSlave.Init(); + OrmTest.Demo.SharedConnection.Init(); } } } diff --git a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj index 97fd5ea18..e9d39a3cc 100644 --- a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj +++ b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj @@ -55,6 +55,7 @@ + diff --git a/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs b/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs index 2413d3905..20b783ff7 100644 --- a/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs +++ b/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs @@ -27,7 +27,7 @@ namespace SqlSugar /// ///If true, there is only one connection instance in the same thread within the same connection string /// - public bool OneThreadOnlyOneDbConnectionInstance { get; set; } + public bool IsShardSameThread { get; set; } /// /// Configure External Services replace default services,For example, Redis storage /// diff --git a/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs b/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs index 75e71265c..49e6803fe 100644 --- a/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs +++ b/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs @@ -10,7 +10,38 @@ namespace SqlSugar public partial class SqlSugarAccessory { #region Properties - public SqlSugarClient Context { get; set; } + public SqlSugarClient Context + { + get + { + var result = _Context; ; + if (CurrentConnectionConfig.IsShardSameThread) + { + if (CallContext.ContextList.Value.IsNullOrEmpty()) + { + CallContext.ContextList.Value = new List(); + CallContext.ContextList.Value.Add(_Context); + } + else + { + var cacheContext = CallContext.ContextList.Value.FirstOrDefault(it => + it.CurrentConnectionConfig.ConnectionString == _Context.CurrentConnectionConfig.ConnectionString && + it.CurrentConnectionConfig.DbType == _Context.CurrentConnectionConfig.DbType && + it.CurrentConnectionConfig.IsAutoCloseConnection == _Context.CurrentConnectionConfig.IsAutoCloseConnection && + it.CurrentConnectionConfig.IsShardSameThread == _Context.CurrentConnectionConfig.IsShardSameThread); + if (cacheContext != null) + { + return cacheContext; + } + } + } + return result; + } + set + { + _Context = value; + } + } public ConnectionConfig CurrentConnectionConfig { get; set; } public Dictionary TempItems { get; set; } public bool IsSystemTablesConfig { get { return this.CurrentConnectionConfig.InitKeyType == InitKeyType.SystemTable; } } @@ -22,6 +53,7 @@ namespace SqlSugar #region Fields protected ISqlBuilder _SqlBuilder; + public SqlSugarClient _Context { get; set; } protected EntityMaintenance _EntityProvider; protected IAdo _Ado; protected ILambdaExpressions _LambdaExpressions; @@ -287,7 +319,7 @@ namespace SqlSugar else { isJoinType = false; - joinValue += joinValue==null?item:(","+item); + joinValue += joinValue == null ? item : ("," + item); } if (isLast) { diff --git a/Src/Asp.Net/SqlSugar/SqlSugarClient.cs b/Src/Asp.Net/SqlSugar/SqlSugarClient.cs index c5659f0af..d5fc40bd7 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugarClient.cs +++ b/Src/Asp.Net/SqlSugar/SqlSugarClient.cs @@ -22,6 +22,7 @@ namespace SqlSugar { base.Context = this; base.CurrentConnectionConfig = config; + base.ContextID = Guid.NewGuid(); Check.ArgumentNullException(config, "config is null"); switch (config.DbType) { @@ -50,14 +51,14 @@ namespace SqlSugar { get { - if (_Ado == null) + if (base.Context._Ado == null) { - var reval = InstanceFactory.GetAdo(base.CurrentConnectionConfig); - _Ado = reval; + var reval = InstanceFactory.GetAdo(base.Context.CurrentConnectionConfig); + base.Context._Ado = reval; reval.Context = base.Context; return reval; } - return _Ado; + return base.Context._Ado; } } #endregion @@ -77,14 +78,14 @@ namespace SqlSugar { get { - if (base._RewritableMethods == null) + if (base.Context._RewritableMethods == null) { - base._RewritableMethods = new ContextMethods(); - base._RewritableMethods.Context = base.Context; + base.Context._RewritableMethods = new ContextMethods(); + base.Context._RewritableMethods.Context = base.Context; } - return _RewritableMethods; + return base.Context._RewritableMethods; } - set { base._RewritableMethods = value; } + set { base.Context._RewritableMethods = value; } } #endregion @@ -504,13 +505,13 @@ namespace SqlSugar { get { - if (base._DbMaintenance == null) + if (base.Context._DbMaintenance == null) { IDbMaintenance maintenance = InstanceFactory.GetDbMaintenance(base.Context.CurrentConnectionConfig); - base._DbMaintenance = maintenance; + base.Context._DbMaintenance = maintenance; maintenance.Context = base.Context; } - return base._DbMaintenance; + return base.Context._DbMaintenance; } } #endregion @@ -526,14 +527,14 @@ namespace SqlSugar { get { - if (base._EntityProvider == null) + if (base.Context._EntityProvider == null) { - base._EntityProvider = new EntityMaintenance(); - base._EntityProvider.Context = base.Context; + base.Context._EntityProvider = new EntityMaintenance(); + base.Context._EntityProvider.Context = base.Context; } - return _EntityProvider; + return base.Context._EntityProvider; } - set { base._EntityProvider = value; } + set { base.Context._EntityProvider = value; } } #endregion @@ -542,14 +543,14 @@ namespace SqlSugar { get { - if (base._QueryFilterProvider == null) + if (base.Context._QueryFilterProvider == null) { - base._QueryFilterProvider = new QueryFilterProvider(); - base._QueryFilterProvider.Context = base.Context; + base.Context._QueryFilterProvider = new QueryFilterProvider(); + base.Context._QueryFilterProvider.Context = base.Context; } - return _QueryFilterProvider; + return base.Context._QueryFilterProvider; } - set { base._QueryFilterProvider = value; } + set { base.Context._QueryFilterProvider = value; } } #endregion @@ -558,9 +559,9 @@ namespace SqlSugar { get { - if (_SimpleClient == null) - _SimpleClient = new SimpleClient(base.Context); - return _SimpleClient; + if (base.Context._SimpleClient == null) + base.Context._SimpleClient = new SimpleClient(base.Context); + return base.Context._SimpleClient; } } #endregion diff --git a/Src/Asp.Net/SqlSugar/Utilities/CallContext.cs b/Src/Asp.Net/SqlSugar/Utilities/CallContext.cs index 458131c79..5e9cd3280 100644 --- a/Src/Asp.Net/SqlSugar/Utilities/CallContext.cs +++ b/Src/Asp.Net/SqlSugar/Utilities/CallContext.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Data; using System.Data.Common; using System.Linq; using System.Text; @@ -9,6 +10,6 @@ namespace SqlSugar { internal class CallContext { - public static ThreadLocal> ContextList = new ThreadLocal>(); + public static ThreadLocal> ContextList = new ThreadLocal>(); } }