Thread safety

This commit is contained in:
sunkaixuan
2019-05-20 00:12:47 +08:00
parent 52c00cf8a9
commit 1dc072b1e9
6 changed files with 23 additions and 22 deletions

View File

@@ -60,12 +60,12 @@ namespace SqlSugar
public virtual CommandType CommandType { get; set; } public virtual CommandType CommandType { get; set; }
public virtual bool IsEnableLogEvent { get; set; } public virtual bool IsEnableLogEvent { get; set; }
public virtual bool IsClearParameters { get; set; } public virtual bool IsClearParameters { get; set; }
public virtual Action<string, SugarParameter[]> LogEventStarting => this.Context.CurrentConnectionConfig.AopEvents.OnLogExecuting; public virtual Action<string, SugarParameter[]> LogEventStarting => this.Context.CurrentConnectionConfig.AopEvents?.OnLogExecuting;
public virtual Action<string, SugarParameter[]> LogEventCompleted => this.Context.CurrentConnectionConfig.AopEvents.OnLogExecuted; public virtual Action<string, SugarParameter[]> LogEventCompleted => this.Context.CurrentConnectionConfig.AopEvents?.OnLogExecuted;
public virtual Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> ProcessingEventStartingSQL => this.Context.CurrentConnectionConfig.AopEvents.OnExecutingChangeSql; public virtual Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> ProcessingEventStartingSQL => this.Context.CurrentConnectionConfig.AopEvents?.OnExecutingChangeSql;
protected virtual Func<string, string> FormatSql { get; set; } protected virtual Func<string, string> FormatSql { get; set; }
public virtual Action<SqlSugarException> ErrorEvent => this.Context.CurrentConnectionConfig.AopEvents.OnError; public virtual Action<SqlSugarException> ErrorEvent => this.Context.CurrentConnectionConfig.AopEvents?.OnError;
public virtual Action<DiffLogModel> DiffLogEvent => this.Context.CurrentConnectionConfig.AopEvents.OnDiffLogEvent; public virtual Action<DiffLogModel> DiffLogEvent => this.Context.CurrentConnectionConfig.AopEvents?.OnDiffLogEvent;
public virtual List<IDbConnection> SlaveConnections { get; set; } public virtual List<IDbConnection> SlaveConnections { get; set; }
public virtual IDbConnection MasterConnection { get; set; } public virtual IDbConnection MasterConnection { get; set; }
#endregion #endregion

View File

@@ -10,7 +10,7 @@ namespace SqlSugar
public partial class SqlSugarProvider public partial class SqlSugarProvider
{ {
#region Properties #region Properties
public ISqlSugarClient Context public SqlSugarProvider Context
{ {
get get
{ {
@@ -39,7 +39,7 @@ namespace SqlSugar
public Dictionary<string, object> _TempItems; public Dictionary<string, object> _TempItems;
public QueueList _Queues; public QueueList _Queues;
protected ISqlBuilder _SqlBuilder; protected ISqlBuilder _SqlBuilder;
protected ISqlSugarClient _Context { get; set; } protected SqlSugarProvider _Context { get; set; }
protected EntityMaintenance _EntityProvider; protected EntityMaintenance _EntityProvider;
protected IAdo _Ado; protected IAdo _Ado;
protected ILambdaExpressions _LambdaExpressions; protected ILambdaExpressions _LambdaExpressions;

View File

@@ -7,7 +7,7 @@ namespace SqlSugar
{ {
public class SugarTenant public class SugarTenant
{ {
public ISqlSugarClient Context { get; set; } public SqlSugarProvider Context { get; set; }
public ConnectionConfig ConnectionConfig { get; set; } public ConnectionConfig ConnectionConfig { get; set; }
} }
} }

View File

@@ -23,7 +23,7 @@ namespace SqlSugar
IAdo Ado { get; } IAdo Ado { get; }
AopProvider Aop { get; } AopProvider Aop { get; }
ICodeFirst CodeFirst { get; } ICodeFirst CodeFirst { get; }
ISqlSugarClient Context { get; set; } SqlSugarProvider Context { get; set; }
IDbFirst DbFirst { get; } IDbFirst DbFirst { get; }
IDbMaintenance DbMaintenance { get; } IDbMaintenance DbMaintenance { get; }

View File

@@ -12,7 +12,7 @@ namespace SqlSugar
public partial class SqlSugarClient : ISqlSugarClient, ITenant public partial class SqlSugarClient : ISqlSugarClient, ITenant
{ {
#region Gobal Property #region Gobal Property
private ISqlSugarClient _Context = null; private SqlSugarProvider _Context = null;
private string _ThreadId; private string _ThreadId;
private ConnectionConfig _CurrentConnectionConfig; private ConnectionConfig _CurrentConnectionConfig;
private List<SugarTenant> _AllClients; private List<SugarTenant> _AllClients;
@@ -45,7 +45,7 @@ namespace SqlSugar
#endregion #endregion
#region Global variable #region Global variable
public ISqlSugarClient Context { get => GetContext(); set => _Context = value; } public SqlSugarProvider Context { get => GetContext(); set => _Context = value; }
public bool IsSystemTablesConfig => this.Context.IsSystemTablesConfig; public bool IsSystemTablesConfig => this.Context.IsSystemTablesConfig;
public ConnectionConfig CurrentConnectionConfig { get => _CurrentConnectionConfig; set => _CurrentConnectionConfig = value; } public ConnectionConfig CurrentConnectionConfig { get => _CurrentConnectionConfig; set => _CurrentConnectionConfig = value; }
public Guid ContextID { get => this.Context.ContextID; set => this.Context.ContextID = value; } public Guid ContextID { get => this.Context.ContextID; set => this.Context.ContextID = value; }
@@ -669,20 +669,20 @@ namespace SqlSugar
#endregion #endregion
#region Helper #region Helper
private ISqlSugarClient GetContext() private SqlSugarProvider GetContext()
{ {
if (CurrentConnectionConfig.IsShardSameThread) if (CurrentConnectionConfig.IsShardSameThread)
{ {
ISqlSugarClient result = _Context; SqlSugarProvider result = _Context;
if (CallContext.ContextList.Value.IsNullOrEmpty()) if (CallContext.ContextList.Value.IsNullOrEmpty())
{ {
CallContext.ContextList.Value = new List<ISqlSugarClient>(); CallContext.ContextList.Value = new List<SqlSugarProvider>();
CallContext.ContextList.Value.Add(_Context); CallContext.ContextList.Value.Add(_Context);
} }
else else
{ {
ISqlSugarClient cacheContext = GetCallContext(); SqlSugarProvider cacheContext = GetCallContext();
if (cacheContext != null) if (cacheContext != null)
{ {
result = cacheContext; result = cacheContext;
@@ -707,7 +707,7 @@ namespace SqlSugar
{ {
if (CallContext.ContextList.Value == null) if (CallContext.ContextList.Value == null)
{ {
CallContext.ContextList.Value = new List<ISqlSugarClient>(); CallContext.ContextList.Value = new List<SqlSugarProvider>();
} }
if (CallContext.ContextList.Value.IsNullOrEmpty() || GetCallContext() == null) if (CallContext.ContextList.Value.IsNullOrEmpty() || GetCallContext() == null)
{ {
@@ -722,9 +722,9 @@ namespace SqlSugar
} }
} }
private SqlSugarClient CopyClient() private SqlSugarProvider CopyClient()
{ {
var result = new SqlSugarClient(this.CurrentConnectionConfig); var result = new SqlSugarProvider(this.CurrentConnectionConfig);
result.MappingColumns = _MappingColumns; result.MappingColumns = _MappingColumns;
result.MappingTables = _MappingTables; result.MappingTables = _MappingTables;
result.IgnoreColumns = _IgnoreColumns; result.IgnoreColumns = _IgnoreColumns;
@@ -733,12 +733,13 @@ namespace SqlSugar
return result; return result;
} }
private ISqlSugarClient GetCallContext() private SqlSugarProvider GetCallContext()
{ {
return CallContext.ContextList.Value.FirstOrDefault(it => return CallContext.ContextList.Value.FirstOrDefault(it =>
it.CurrentConnectionConfig.DbType == _Context.CurrentConnectionConfig.DbType&& it.CurrentConnectionConfig.DbType == _Context.CurrentConnectionConfig.DbType&&
it.CurrentConnectionConfig.ConnectionString == _Context.CurrentConnectionConfig.ConnectionString&& it.CurrentConnectionConfig.ConnectionString == _Context.CurrentConnectionConfig.ConnectionString&&
it.CurrentConnectionConfig.InitKeyType==_Context.CurrentConnectionConfig.InitKeyType it.CurrentConnectionConfig.InitKeyType==_Context.CurrentConnectionConfig.InitKeyType&&
it.CurrentConnectionConfig.IsAutoCloseConnection == _Context.CurrentConnectionConfig.IsAutoCloseConnection
); );
} }
@@ -789,7 +790,7 @@ namespace SqlSugar
{ {
if (Tenant.Context == null) if (Tenant.Context == null)
{ {
Tenant.Context = new SqlSugarClient(Tenant.ConnectionConfig); Tenant.Context = new SqlSugarProvider(Tenant.ConnectionConfig);
} }
_Context = Tenant.Context; _Context = Tenant.Context;
this.CurrentConnectionConfig = Tenant.ConnectionConfig; this.CurrentConnectionConfig = Tenant.ConnectionConfig;

View File

@@ -10,6 +10,6 @@ namespace SqlSugar
{ {
internal class CallContext internal class CallContext
{ {
public static ThreadLocal<List<ISqlSugarClient>> ContextList = new ThreadLocal<List<ISqlSugarClient>>(); public static ThreadLocal<List<SqlSugarProvider>> ContextList = new ThreadLocal<List<SqlSugarProvider>>();
} }
} }