mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2026-02-27 16:50:33 +08:00
Support Shard Same Thread
This commit is contained in:
87
Src/Asp.Net/SqlServerTest/Demos/B_SharedConnection.cs
Normal file
87
Src/Asp.Net/SqlServerTest/Demos/B_SharedConnection.cs
Normal file
@@ -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<School>().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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -47,6 +47,7 @@ namespace OrmTest
|
|||||||
OrmTest.Demo.CodeFirst.Init();
|
OrmTest.Demo.CodeFirst.Init();
|
||||||
OrmTest.Demo.Aop.Init();
|
OrmTest.Demo.Aop.Init();
|
||||||
OrmTest.Demo.MasterSlave.Init();
|
OrmTest.Demo.MasterSlave.Init();
|
||||||
|
OrmTest.Demo.SharedConnection.Init();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,6 +55,7 @@
|
|||||||
<Compile Include="Demos\5_DbFirst.cs" />
|
<Compile Include="Demos\5_DbFirst.cs" />
|
||||||
<Compile Include="Demos\4_Delete.cs" />
|
<Compile Include="Demos\4_Delete.cs" />
|
||||||
<Compile Include="Demos\9_Aop.cs" />
|
<Compile Include="Demos\9_Aop.cs" />
|
||||||
|
<Compile Include="Demos\B_SharedConnection.cs" />
|
||||||
<Compile Include="Demos\DemoBase.cs" />
|
<Compile Include="Demos\DemoBase.cs" />
|
||||||
<Compile Include="Demos\3_Insert.cs" />
|
<Compile Include="Demos\3_Insert.cs" />
|
||||||
<Compile Include="Demos\1_Query.cs" />
|
<Compile Include="Demos\1_Query.cs" />
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ namespace SqlSugar
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
///If true, there is only one connection instance in the same thread within the same connection string
|
///If true, there is only one connection instance in the same thread within the same connection string
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool OneThreadOnlyOneDbConnectionInstance { get; set; }
|
public bool IsShardSameThread { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Configure External Services replace default services,For example, Redis storage
|
/// Configure External Services replace default services,For example, Redis storage
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -10,7 +10,38 @@ namespace SqlSugar
|
|||||||
public partial class SqlSugarAccessory
|
public partial class SqlSugarAccessory
|
||||||
{
|
{
|
||||||
#region Properties
|
#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<SqlSugarClient>();
|
||||||
|
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 ConnectionConfig CurrentConnectionConfig { get; set; }
|
||||||
public Dictionary<string, object> TempItems { get; set; }
|
public Dictionary<string, object> TempItems { get; set; }
|
||||||
public bool IsSystemTablesConfig { get { return this.CurrentConnectionConfig.InitKeyType == InitKeyType.SystemTable; } }
|
public bool IsSystemTablesConfig { get { return this.CurrentConnectionConfig.InitKeyType == InitKeyType.SystemTable; } }
|
||||||
@@ -22,6 +53,7 @@ namespace SqlSugar
|
|||||||
|
|
||||||
#region Fields
|
#region Fields
|
||||||
protected ISqlBuilder _SqlBuilder;
|
protected ISqlBuilder _SqlBuilder;
|
||||||
|
public SqlSugarClient _Context { get; set; }
|
||||||
protected EntityMaintenance _EntityProvider;
|
protected EntityMaintenance _EntityProvider;
|
||||||
protected IAdo _Ado;
|
protected IAdo _Ado;
|
||||||
protected ILambdaExpressions _LambdaExpressions;
|
protected ILambdaExpressions _LambdaExpressions;
|
||||||
@@ -287,7 +319,7 @@ namespace SqlSugar
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
isJoinType = false;
|
isJoinType = false;
|
||||||
joinValue += joinValue==null?item:(","+item);
|
joinValue += joinValue == null ? item : ("," + item);
|
||||||
}
|
}
|
||||||
if (isLast)
|
if (isLast)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
base.Context = this;
|
base.Context = this;
|
||||||
base.CurrentConnectionConfig = config;
|
base.CurrentConnectionConfig = config;
|
||||||
|
base.ContextID = Guid.NewGuid();
|
||||||
Check.ArgumentNullException(config, "config is null");
|
Check.ArgumentNullException(config, "config is null");
|
||||||
switch (config.DbType)
|
switch (config.DbType)
|
||||||
{
|
{
|
||||||
@@ -50,14 +51,14 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (_Ado == null)
|
if (base.Context._Ado == null)
|
||||||
{
|
{
|
||||||
var reval = InstanceFactory.GetAdo(base.CurrentConnectionConfig);
|
var reval = InstanceFactory.GetAdo(base.Context.CurrentConnectionConfig);
|
||||||
_Ado = reval;
|
base.Context._Ado = reval;
|
||||||
reval.Context = base.Context;
|
reval.Context = base.Context;
|
||||||
return reval;
|
return reval;
|
||||||
}
|
}
|
||||||
return _Ado;
|
return base.Context._Ado;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
@@ -77,14 +78,14 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (base._RewritableMethods == null)
|
if (base.Context._RewritableMethods == null)
|
||||||
{
|
{
|
||||||
base._RewritableMethods = new ContextMethods();
|
base.Context._RewritableMethods = new ContextMethods();
|
||||||
base._RewritableMethods.Context = base.Context;
|
base.Context._RewritableMethods.Context = base.Context;
|
||||||
}
|
}
|
||||||
return _RewritableMethods;
|
return base.Context._RewritableMethods;
|
||||||
}
|
}
|
||||||
set { base._RewritableMethods = value; }
|
set { base.Context._RewritableMethods = value; }
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -504,13 +505,13 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (base._DbMaintenance == null)
|
if (base.Context._DbMaintenance == null)
|
||||||
{
|
{
|
||||||
IDbMaintenance maintenance = InstanceFactory.GetDbMaintenance(base.Context.CurrentConnectionConfig);
|
IDbMaintenance maintenance = InstanceFactory.GetDbMaintenance(base.Context.CurrentConnectionConfig);
|
||||||
base._DbMaintenance = maintenance;
|
base.Context._DbMaintenance = maintenance;
|
||||||
maintenance.Context = base.Context;
|
maintenance.Context = base.Context;
|
||||||
}
|
}
|
||||||
return base._DbMaintenance;
|
return base.Context._DbMaintenance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
@@ -526,14 +527,14 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (base._EntityProvider == null)
|
if (base.Context._EntityProvider == null)
|
||||||
{
|
{
|
||||||
base._EntityProvider = new EntityMaintenance();
|
base.Context._EntityProvider = new EntityMaintenance();
|
||||||
base._EntityProvider.Context = base.Context;
|
base.Context._EntityProvider.Context = base.Context;
|
||||||
}
|
}
|
||||||
return _EntityProvider;
|
return base.Context._EntityProvider;
|
||||||
}
|
}
|
||||||
set { base._EntityProvider = value; }
|
set { base.Context._EntityProvider = value; }
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -542,14 +543,14 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (base._QueryFilterProvider == null)
|
if (base.Context._QueryFilterProvider == null)
|
||||||
{
|
{
|
||||||
base._QueryFilterProvider = new QueryFilterProvider();
|
base.Context._QueryFilterProvider = new QueryFilterProvider();
|
||||||
base._QueryFilterProvider.Context = base.Context;
|
base.Context._QueryFilterProvider.Context = base.Context;
|
||||||
}
|
}
|
||||||
return _QueryFilterProvider;
|
return base.Context._QueryFilterProvider;
|
||||||
}
|
}
|
||||||
set { base._QueryFilterProvider = value; }
|
set { base.Context._QueryFilterProvider = value; }
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -558,9 +559,9 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (_SimpleClient == null)
|
if (base.Context._SimpleClient == null)
|
||||||
_SimpleClient = new SimpleClient(base.Context);
|
base.Context._SimpleClient = new SimpleClient(base.Context);
|
||||||
return _SimpleClient;
|
return base.Context._SimpleClient;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
using System.Data.Common;
|
using System.Data.Common;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@@ -9,6 +10,6 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
internal class CallContext
|
internal class CallContext
|
||||||
{
|
{
|
||||||
public static ThreadLocal<List<DbConnection>> ContextList = new ThreadLocal<List<DbConnection>>();
|
public static ThreadLocal<List<SqlSugarClient>> ContextList = new ThreadLocal<List<SqlSugarClient>>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user