From 5eaffe2ec2bd74588338b71e73c68e097d5893d5 Mon Sep 17 00:00:00 2001 From: chenchun Date: Mon, 17 Nov 2025 11:19:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E5=B9=B6=E5=8F=91=E4=B9=90=E8=A7=82=E9=94=81=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E4=B8=8E=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 DbConnOptions 新增 EnabledConcurrencyException(bool,默认 false) 配置项。 - 在 SqlSugarRepository 引入 IAbpLazyServiceProvider,通过 IOptions 延迟获取配置。 - UpdateAsync 改为仅当 EnabledConcurrencyException 为 true 且实体实现 IHasConcurrencyStamp 时,使用 ExecuteCommandWithOptLockAsync 并捕获 VersionExceptions 抛出 AbpDbConcurrencyException;否则回退到原有的 UpdateAsync 实现。 - 清理/调整部分 using 引用,新增 Microsoft.Extensions.Options 与 Volo.Abp.DependencyInjection 引用。 注意:默认值为 false,需在配置中显式开启 EnabledConcurrencyException 才会启用乐观并发校验,开启后会改变之前对带版本实体自动使用乐观锁的行为。 --- .../DbConnOptions.cs | 5 +++ .../Repositories/SqlSugarRepository.cs | 36 ++++++++++--------- Yi.Abp.Net8/src/Yi.Abp.Web/appsettings.json | 3 +- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore.Abstractions/DbConnOptions.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore.Abstractions/DbConnOptions.cs index 6f48f14d..65c83f13 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore.Abstractions/DbConnOptions.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore.Abstractions/DbConnOptions.cs @@ -58,5 +58,10 @@ namespace Yi.Framework.SqlSugarCore.Abstractions /// 是否启用SaaS多租户 /// public bool EnabledSaasMultiTenancy { get; set; } = false; + + /// + /// 是否开启更新并发乐观锁 + /// + public bool EnabledConcurrencyException { get;set; } = false; } } \ No newline at end of file diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarRepository.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarRepository.cs index 9243e5ed..4b3b7023 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarRepository.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarRepository.cs @@ -1,12 +1,9 @@ -using System.Linq; -using System.Linq.Expressions; -using System.Text; -using Microsoft.Extensions.Logging; +using System.Linq.Expressions; +using Microsoft.Extensions.Options; using Nito.AsyncEx; using SqlSugar; -using Volo.Abp; -using Volo.Abp.Auditing; using Volo.Abp.Data; +using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Repositories; using Volo.Abp.Linq; @@ -23,6 +20,9 @@ namespace Yi.Framework.SqlSugarCore.Repositories private readonly ISugarDbContextProvider _dbContextProvider; + public IAbpLazyServiceProvider LazyServiceProvider { get; set; } + + protected DbConnOptions? Options => LazyServiceProvider?.LazyGetService>().Value; /// /// 异步查询执行器 /// @@ -380,20 +380,24 @@ namespace Yi.Framework.SqlSugarCore.Repositories public virtual async Task UpdateAsync(TEntity updateObj) { - if (typeof(TEntity).IsAssignableTo())//带版本号乐观锁更新 + if (Options is not null && Options.EnabledConcurrencyException) { - try + if (typeof(TEntity).IsAssignableTo()) //带版本号乐观锁更新 { - int num = await (await GetDbSimpleClientAsync()) - .Context.Updateable(updateObj).ExecuteCommandWithOptLockAsync(true); - return num>0; - } - catch (VersionExceptions ex) - { - - throw new AbpDbConcurrencyException($"{ex.Message}[更新失败:ConcurrencyStamp不是最新版本],entityInfo:{updateObj}", ex); + try + { + int num = await (await GetDbSimpleClientAsync()) + .Context.Updateable(updateObj).ExecuteCommandWithOptLockAsync(true); + return num > 0; + } + catch (VersionExceptions ex) + { + throw new AbpDbConcurrencyException( + $"{ex.Message}[更新失败:ConcurrencyStamp不是最新版本],entityInfo:{updateObj}", ex); + } } } + return await (await GetDbSimpleClientAsync()).UpdateAsync(updateObj); } diff --git a/Yi.Abp.Net8/src/Yi.Abp.Web/appsettings.json b/Yi.Abp.Net8/src/Yi.Abp.Web/appsettings.json index 1b1eee0e..ba5ec320 100644 --- a/Yi.Abp.Net8/src/Yi.Abp.Web/appsettings.json +++ b/Yi.Abp.Net8/src/Yi.Abp.Web/appsettings.json @@ -40,7 +40,8 @@ "EnabledDbSeed": true, "EnableUnderLine": false, // 启用驼峰转下划线 //SAAS多租户 - "EnabledSaasMultiTenancy": true + "EnabledSaasMultiTenancy": true, + "EnabledConcurrencyException": false //读写分离地址 //"ReadUrl": [ // "DataSource=[xxxx]", //Sqlite