diff --git a/Blog.Core.Api/Blog.Core.xml b/Blog.Core.Api/Blog.Core.xml index dfe9950..8beea22 100644 --- a/Blog.Core.Api/Blog.Core.xml +++ b/Blog.Core.Api/Blog.Core.xml @@ -1520,29 +1520,6 @@ - - - 自定义路由 /api/{version}/[controler]/[action] - - - - - 分组名称,是来实现接口 IApiDescriptionGroupNameProvider - - - - - 自定义路由构造函数,继承基类路由 - - - - - - 自定义版本+路由构造函数,继承基类路由 - - - - Summary:全局路由权限公约 @@ -1584,5 +1561,28 @@ 全局路由前缀公约 + + + 自定义路由 /api/{version}/[controler]/[action] + + + + + 分组名称,是来实现接口 IApiDescriptionGroupNameProvider + + + + + 自定义路由构造函数,继承基类路由 + + + + + + 自定义版本+路由构造函数,继承基类路由 + + + + diff --git a/Blog.Core.Api/Controllers/DbFirst/DbFirstController.cs b/Blog.Core.Api/Controllers/DbFirst/DbFirstController.cs index 83b0beb..553dee7 100644 --- a/Blog.Core.Api/Controllers/DbFirst/DbFirstController.cs +++ b/Blog.Core.Api/Controllers/DbFirst/DbFirstController.cs @@ -43,12 +43,12 @@ namespace Blog.Core.Controllers BaseDBConfig.ValidConfig.ForEach(m => { - _sqlSugarClient.ChangeDatabase(m.ConfigId.ToLower()); - data.response += $"库{m.ConfigId}-Model层生成:{FrameSeed.CreateModels(_sqlSugarClient, m.ConfigId, isMuti)} || "; - data.response += $"库{m.ConfigId}-IRepositorys层生成:{FrameSeed.CreateIRepositorys(_sqlSugarClient, m.ConfigId, isMuti)} || "; - data.response += $"库{m.ConfigId}-IServices层生成:{FrameSeed.CreateIServices(_sqlSugarClient, m.ConfigId, isMuti)} || "; - data.response += $"库{m.ConfigId}-Repository层生成:{FrameSeed.CreateRepository(_sqlSugarClient, m.ConfigId, isMuti)} || "; - data.response += $"库{m.ConfigId}-Services层生成:{FrameSeed.CreateServices(_sqlSugarClient, m.ConfigId, isMuti)} || "; + _sqlSugarClient.ChangeDatabase(m.ConfigId.ToString().ToLower()); + data.response += $"库{m.ConfigId}-Model层生成:{FrameSeed.CreateModels(_sqlSugarClient, m.ConfigId.ToString(), isMuti)} || "; + data.response += $"库{m.ConfigId}-IRepositorys层生成:{FrameSeed.CreateIRepositorys(_sqlSugarClient, m.ConfigId.ToString(), isMuti)} || "; + data.response += $"库{m.ConfigId}-IServices层生成:{FrameSeed.CreateIServices(_sqlSugarClient, m.ConfigId.ToString(), isMuti)} || "; + data.response += $"库{m.ConfigId}-Repository层生成:{FrameSeed.CreateRepository(_sqlSugarClient, m.ConfigId.ToString(), isMuti)} || "; + data.response += $"库{m.ConfigId}-Services层生成:{FrameSeed.CreateServices(_sqlSugarClient, m.ConfigId.ToString(), isMuti)} || "; }); // 切回主库 diff --git a/Blog.Core.Api/Controllers/Test/SqlsugarTestController.cs b/Blog.Core.Api/Controllers/Test/SqlsugarTestController.cs new file mode 100644 index 0000000..774a9b1 --- /dev/null +++ b/Blog.Core.Api/Controllers/Test/SqlsugarTestController.cs @@ -0,0 +1,29 @@ +using Blog.Core.Common; +using Blog.Core.Controllers; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using SqlSugar; + +namespace Blog.Core.Api.Controllers.Test; + +[Route("api/[Controller]/[Action]")] +[AllowAnonymous] +public class SqlsugarTestController : BaseApiController +{ + private readonly SqlSugarScope _db; + + public SqlsugarTestController(SqlSugarScope db) + { + _db = db; + } + + [HttpGet] + public async Task Get() + { + Console.WriteLine(App.HttpContext.Request.Path); + Console.WriteLine(App.HttpContext.RequestServices.ToString()); + Console.WriteLine(App.User?.ID); + await Task.CompletedTask; + return Ok(); + } +} \ No newline at end of file diff --git a/Blog.Core.Api/appsettings.json b/Blog.Core.Api/appsettings.json index 673340b..410acc3 100644 --- a/Blog.Core.Api/appsettings.json +++ b/Blog.Core.Api/appsettings.json @@ -118,7 +118,7 @@ { "ConnId": "Main2", "DBType": 2, - "Enabled": true, + "Enabled": false, "Connection": "WMBlog3.db", //sqlite只写数据库名就行 "Slaves": [ { @@ -265,7 +265,7 @@ "Enabled": false }, "IpRateLimit": { - "Enabled": true + "Enabled": false }, "EncryptionResponse": { "Enabled": true, diff --git a/Blog.Core.Api/wwwroot/BlogCore.Data.json/TasksQz.tsv b/Blog.Core.Api/wwwroot/BlogCore.Data.json/TasksQz.tsv index 2fa2c64..5aaef10 100644 --- a/Blog.Core.Api/wwwroot/BlogCore.Data.json/TasksQz.tsv +++ b/Blog.Core.Api/wwwroot/BlogCore.Data.json/TasksQz.tsv @@ -3,7 +3,7 @@ "Name": "博客管理", "JobGroup": "博客测试组", "TriggerType": 1, - "Cron": "0 */5 * * * ?", + "Cron": "0 */1 * * * ?", "AssemblyName": "Blog.Core.Tasks", "ClassName": "Job_Blogs_Quartz", "Remark": "", diff --git a/Blog.Core.Extensions/ServiceExtensions/SqlsugarSetup.cs b/Blog.Core.Extensions/ServiceExtensions/SqlsugarSetup.cs index 0700a74..d6bfbb2 100644 --- a/Blog.Core.Extensions/ServiceExtensions/SqlsugarSetup.cs +++ b/Blog.Core.Extensions/ServiceExtensions/SqlsugarSetup.cs @@ -69,7 +69,7 @@ namespace Blog.Core.Extensions } else { - if (string.Equals(config.ConfigId, MainDb.CurrentDbConnId, + if (string.Equals(config.ConfigId.ToString(), MainDb.CurrentDbConnId, StringComparison.CurrentCultureIgnoreCase)) { BaseDBConfig.MainConfig = config; @@ -122,6 +122,7 @@ namespace Blog.Core.Extensions SqlSugarReuse.AutoChangeAvailableConnect(db); }); }); + services.AddTransient(s => s.GetService() as SqlSugarScope); } private static string GetWholeSql(SugarParameter[] paramArr, string sql) diff --git a/Blog.Core.IServices/IGuestbookServices.cs b/Blog.Core.IServices/IGuestbookServices.cs index 7fc72f6..cd8b228 100644 --- a/Blog.Core.IServices/IGuestbookServices.cs +++ b/Blog.Core.IServices/IGuestbookServices.cs @@ -15,5 +15,7 @@ namespace Blog.Core.IServices Task TestTranPropagationNoTran(); Task TestTranPropagationTran(); + Task TestTranPropagationTran2(); + Task TestTranPropagationTran3(); } } \ No newline at end of file diff --git a/Blog.Core.IServices/IPasswordLibServices.cs b/Blog.Core.IServices/IPasswordLibServices.cs index d7a0b31..67532fd 100644 --- a/Blog.Core.IServices/IPasswordLibServices.cs +++ b/Blog.Core.IServices/IPasswordLibServices.cs @@ -9,5 +9,6 @@ namespace Blog.Core.IServices Task TestTranPropagation2(); Task TestTranPropagationNoTranError(); Task TestTranPropagationTran2(); + Task TestTranPropagationTran3(); } } diff --git a/Blog.Core.Model/Blog.Core.Model.csproj b/Blog.Core.Model/Blog.Core.Model.csproj index 841a466..3a35feb 100644 --- a/Blog.Core.Model/Blog.Core.Model.csproj +++ b/Blog.Core.Model/Blog.Core.Model.csproj @@ -14,7 +14,7 @@ - + diff --git a/Blog.Core.Repository/UnitOfWorks/UnitOfWorkManage.cs b/Blog.Core.Repository/UnitOfWorks/UnitOfWorkManage.cs index fe190b3..cfaffd9 100644 --- a/Blog.Core.Repository/UnitOfWorks/UnitOfWorkManage.cs +++ b/Blog.Core.Repository/UnitOfWorks/UnitOfWorkManage.cs @@ -51,21 +51,25 @@ namespace Blog.Core.Repository.UnitOfWorks public void BeginTran() { + Console.WriteLine("Begin Transaction Before:" + GetDbClient().ContextID); lock (this) { _tranCount++; GetDbClient().BeginTran(); } + Console.WriteLine("Begin Transaction After:" + GetDbClient().ContextID); } public void BeginTran(MethodInfo method) { + Console.WriteLine("Begin Transaction Before:" + GetDbClient().ContextID); lock (this) { GetDbClient().BeginTran(); TranStack.Push(method.GetFullName()); _tranCount = TranStack.Count; } + Console.WriteLine("Begin Transaction After:" + GetDbClient().ContextID); } public void CommitTran() diff --git a/Blog.Core.Services/GuestbookServices.cs b/Blog.Core.Services/GuestbookServices.cs index daff1ce..c63dfdb 100644 --- a/Blog.Core.Services/GuestbookServices.cs +++ b/Blog.Core.Services/GuestbookServices.cs @@ -8,6 +8,7 @@ using System; using System.Threading.Tasks; using Blog.Core.Common.DB; using Blog.Core.Repository.UnitOfWorks; +using SqlSugar; namespace Blog.Core.Services { @@ -15,14 +16,17 @@ namespace Blog.Core.Services { private readonly IUnitOfWorkManage _unitOfWorkManage; private readonly IBaseRepository _passwordLibRepository; - private readonly IPasswordLibServices _passwordLibServices; + private readonly ISqlSugarClient _db; + private SqlSugarScope db => _db as SqlSugarScope; - public GuestbookServices(IUnitOfWorkManage unitOfWorkManage, IBaseRepository dal, IBaseRepository passwordLibRepository, IPasswordLibServices passwordLibServices) + public GuestbookServices(IUnitOfWorkManage unitOfWorkManage, IBaseRepository dal, + IBaseRepository passwordLibRepository, IPasswordLibServices passwordLibServices, ISqlSugarClient db) { _unitOfWorkManage = unitOfWorkManage; _passwordLibRepository = passwordLibRepository; _passwordLibServices = passwordLibServices; + _db = db; } public async Task> TestTranInRepository() @@ -193,8 +197,9 @@ namespace Blog.Core.Services public async Task TestTranPropagationTran() { var guestbooks = await base.Query(); + guestbooks = await base.Query(); Console.WriteLine($"first time : the count of guestbooks is :{guestbooks.Count}"); - + Console.WriteLine(base.Db.ContextID); var insertGuestbook = await base.Add(new Guestbook() { username = "bbb", @@ -207,5 +212,68 @@ namespace Blog.Core.Services return true; } + + [UseTran(Propagation = Propagation.Required)] + public async Task TestTranPropagationTran2() + { + await Db.Insertable(new Guestbook() + { + username = "bbb", + blogId = 1, + createdate = DateTime.Now, + isshow = true + }).ExecuteReturnSnowflakeIdAsync(); + + + await Db.Insertable(new PasswordLib() + { + PLID = SnowFlakeSingle.Instance.NextId(), + IsDeleted = false, + plAccountName = "aaa", + plCreateTime = DateTime.Now + }).ExecuteReturnSnowflakeIdAsync(); + + await _passwordLibServices.TestTranPropagationTran2(); + + Console.WriteLine("完成"); + } + + public async Task TestTranPropagationTran3() + { + try + { + Console.WriteLine("Begin Transaction Before:" + db.ContextID); + db.BeginTran(); + Console.WriteLine("Begin Transaction After:" + db.ContextID); + + await db.Insertable(new Guestbook() + { + username = "bbb", + blogId = 1, + createdate = DateTime.Now, + isshow = true + }).ExecuteReturnSnowflakeIdAsync(); + + + await db.Insertable(new PasswordLib() + { + PLID = SnowFlakeSingle.Instance.NextId(), + IsDeleted = false, + plAccountName = "aaa", + plCreateTime = DateTime.Now + }).ExecuteReturnSnowflakeIdAsync(); + + await _passwordLibServices.TestTranPropagationTran3(); + + db.CommitTran(); + Console.WriteLine("完成"); + } + catch (Exception e) + { + db.RollbackTran(); + throw; + } + + } } } \ No newline at end of file diff --git a/Blog.Core.Services/PasswordLibServices.cs b/Blog.Core.Services/PasswordLibServices.cs index de58a6b..6f5a468 100644 --- a/Blog.Core.Services/PasswordLibServices.cs +++ b/Blog.Core.Services/PasswordLibServices.cs @@ -5,17 +5,24 @@ using Blog.Core.Common.DB; using Blog.Core.IRepository.Base; using Blog.Core.IServices; using Blog.Core.Model.Models; +using Blog.Core.Repository.UnitOfWorks; using Blog.Core.Services.BASE; +using SqlSugar; namespace Blog.Core.Services { public partial class PasswordLibServices : BaseServices, IPasswordLibServices { IBaseRepository _dal; + private readonly IUnitOfWorkManage _unitOfWorkManage; + private readonly ISqlSugarClient _db; + private SqlSugarScope db => _db as SqlSugarScope; - public PasswordLibServices(IBaseRepository dal) + public PasswordLibServices(IBaseRepository dal, IUnitOfWorkManage unitOfWorkManage, ISqlSugarClient db) { this._dal = dal; + _unitOfWorkManage = unitOfWorkManage; + _db = db; base.BaseDal = dal; } @@ -24,6 +31,7 @@ namespace Blog.Core.Services { await _dal.Add(new PasswordLib() { + PLID = SnowFlakeSingle.Instance.NextId(), IsDeleted = false, plAccountName = "aaa", plCreateTime = DateTime.Now @@ -48,13 +56,33 @@ namespace Blog.Core.Services [UseTran(Propagation = Propagation.Nested)] public async Task TestTranPropagationTran2() { - await _dal.Add(new PasswordLib() + await db.Insertable(new PasswordLib() { + PLID = SnowFlakeSingle.Instance.NextId(), IsDeleted = false, plAccountName = "aaa", plCreateTime = DateTime.Now - }); + }).ExecuteReturnSnowflakeIdAsync(); + throw new Exception("123"); + return true; + } + + public async Task TestTranPropagationTran3() + { + Console.WriteLine("Begin Transaction Before:" + db.ContextID); + db.BeginTran(); + Console.WriteLine("Begin Transaction After:" + db.ContextID); + Console.WriteLine(""); + await db.Insertable(new PasswordLib() + { + PLID = SnowFlakeSingle.Instance.NextId(), + IsDeleted = false, + plAccountName = "aaa", + plCreateTime = DateTime.Now + }).ExecuteReturnSnowflakeIdAsync(); + + throw new Exception("123"); return true; } } diff --git a/Blog.Core.Tasks/QuartzNet/Jobs/Job_Blogs_Quartz.cs b/Blog.Core.Tasks/QuartzNet/Jobs/Job_Blogs_Quartz.cs index f116fe0..6ca3178 100644 --- a/Blog.Core.Tasks/QuartzNet/Jobs/Job_Blogs_Quartz.cs +++ b/Blog.Core.Tasks/QuartzNet/Jobs/Job_Blogs_Quartz.cs @@ -2,25 +2,90 @@ using Quartz; using System; using System.Threading.Tasks; +using Blog.Core.Model.Models; +using Blog.Core.Repository.UnitOfWorks; +using SqlSugar; /// /// 这里要注意下,命名空间和程序集是一样的,不然反射不到 /// namespace Blog.Core.Tasks { + [DisallowConcurrentExecution] public class Job_Blogs_Quartz : JobBase, IJob { private readonly IBlogArticleServices _blogArticleServices; + private readonly IGuestbookServices _guestbookServices; + private readonly IUnitOfWorkManage _uowm; + private readonly ISqlSugarClient _db; + private SqlSugarScope db => _db as SqlSugarScope; - public Job_Blogs_Quartz(IBlogArticleServices blogArticleServices, ITasksQzServices tasksQzServices, ITasksLogServices tasksLogServices) + public Job_Blogs_Quartz(IBlogArticleServices blogArticleServices, ITasksQzServices tasksQzServices, ITasksLogServices tasksLogServices, + IGuestbookServices guestbookServices, IUnitOfWorkManage uowm, ISqlSugarClient db) : base(tasksQzServices, tasksLogServices) { _blogArticleServices = blogArticleServices; + _guestbookServices = guestbookServices; + _uowm = uowm; + this._db = db; } + + /// + /// 直接写就没有锁库 上下文ContextID一样 + /// + /// + public async Task Execute2(IJobExecutionContext context) + { + try + { + db.BeginTran(); + Console.WriteLine(_uowm.GetDbClient().ContextID); + await db.Insertable(new Guestbook() + { + username = "bbb", + blogId = 1, + createdate = DateTime.Now, + isshow = true + }).ExecuteReturnSnowflakeIdAsync(); + await db.Insertable(new PasswordLib() + { + PLID = SnowFlakeSingle.Instance.NextId(), + IsDeleted = false, + plAccountName = "aaa", + plCreateTime = DateTime.Now + }).ExecuteReturnSnowflakeIdAsync(); + + db.BeginTran(); + Console.WriteLine(db.ContextID); + await db.Insertable(new PasswordLib() + { + PLID = SnowFlakeSingle.Instance.NextId(), + IsDeleted = false, + plAccountName = "aaa", + plCreateTime = DateTime.Now + }).ExecuteReturnSnowflakeIdAsync(); + + db.CommitTran(); + + Console.WriteLine(db.ContextID); + db.CommitTran(); + Console.WriteLine("完成"); + } + catch (Exception e) + { + db.RollbackTran(); + } + } + + /// + /// 但是调用其他类方法 上下文ContextID就不一样 + /// + /// public async Task Execute(IJobExecutionContext context) { var executeLog = await ExecuteJob(context, async () => await Run(context)); } + public async Task Run(IJobExecutionContext context) { System.Console.WriteLine($"Job_Blogs_Quartz 执行 {DateTime.Now.ToShortTimeString()}"); @@ -28,6 +93,8 @@ namespace Blog.Core.Tasks // 也可以通过数据库配置,获取传递过来的参数 JobDataMap data = context.JobDetail.JobDataMap; //int jobId = data.GetInt("JobParam"); + + await _guestbookServices.TestTranPropagationTran2(); } } -} +} \ No newline at end of file