Browse Source

Thread safety

pull/12/MERGE
sunkaixuan 6 years ago
parent
commit
1dc072b1e9
  1. 10
      Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs
  2. 4
      Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarAccessory.cs
  3. 2
      Src/Asp.Net/SqlSugar/Entities/SugarTerant.cs
  4. 2
      Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs
  5. 25
      Src/Asp.Net/SqlSugar/SqlSugarClient.cs
  6. 2
      Src/Asp.Net/SqlSugar/Utilities/CallContext.cs

10
Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs

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

4
Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarAccessory.cs

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

2
Src/Asp.Net/SqlSugar/Entities/SugarTerant.cs

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

2
Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs

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

25
Src/Asp.Net/SqlSugar/SqlSugarClient.cs

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

2
Src/Asp.Net/SqlSugar/Utilities/CallContext.cs

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

Loading…
Cancel
Save