This commit is contained in:
sunkaixuan 2019-01-25 22:40:19 +08:00
parent b3f7fc29d1
commit 18d0346e46
3 changed files with 33 additions and 34 deletions

View File

@ -20,30 +20,34 @@ namespace OrmTest.Demo
db.Insertable<Student>(new Student() { Name = "a" }).ExecuteCommandAsync();
db.Updateable<Student>(new Student() { Name = "a" }).ExecuteCommandAsync();
db.Deleteable<Student>(1111).ExecuteCommandAsync();
try
{
var task = new Task(() =>
var task = new Task(() =>
{
try
{
//is error
db.Queryable<Student>().ToList();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
try
{
//error
db.Queryable<Student>().ToList();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
});
}
task.Start();
task.Wait();
});
task.Start();
task.Wait();
}
finally
var task2 = new Task(() =>
{
//is ok
var db2 = GetInstance();
db2.CurrentConnectionConfig.Debugger = new SqlSugar.SugarDebugger() { EnableThreadSecurityValidation = true };
db2.Queryable<Student>().ToList();
});
task2.Start();
task2.Wait();
}
}

View File

@ -1,15 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;
using SqlSugar;
using OrmTest.Models;
using System.Data.SqlClient;
using OrmTest.PerformanceTesting;
using OrmTest.UnitTest;
using OrmTest.PerformanceTesting;
using System;
namespace OrmTest
{
@ -62,6 +53,9 @@ namespace OrmTest
Demo.Delete.Init();
Demo.InsertOrUpdate.Init();
Demo.Debugger.Init();
Console.WriteLine("all successfully.");
Console.ReadKey();
}
}
}

View File

@ -8,6 +8,7 @@ using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace SqlSugar
{
@ -678,15 +679,15 @@ namespace SqlSugar
if (this.Context.IsAsyncMethod==false&&this.Context.CurrentConnectionConfig.Debugger != null && this.Context.CurrentConnectionConfig.Debugger.EnableThreadSecurityValidation == true) {
var contextId =this.Context.ContextID.ToString();
var processId = Process.GetCurrentProcess().Id.ToString();
var processId = Thread.CurrentThread.ManagedThreadId.ToString();
var cache = new ReflectionInoCacheService();
if (!cache.ContainsKey<string>(processId))
if (!cache.ContainsKey<string>(contextId))
{
cache.Add(processId, contextId);
cache.Add(contextId, processId);
}
else {
var cacheValue = cache.Get<string>(processId);
if (contextId != cacheValue) {
var cacheValue = cache.Get<string>(contextId);
if (processId != cacheValue) {
throw new SqlSugarException(this.Context,ErrorMessage.GetThrowMessage("Detection of SqlSugarClient cross-threading usage,a thread needs a new one", "检测到声名的SqlSugarClient跨线程使用请检查是否静态、是否单例、或者IOC配置错误引起的保证一个线程new出一个对象 具本Sql:")+sql,parameters);
}
}