feat: 新增更新并发乐观锁配置与支持

- 在 DbConnOptions 新增 EnabledConcurrencyException(bool,默认 false) 配置项。
- 在 SqlSugarRepository 引入 IAbpLazyServiceProvider,通过 IOptions<DbConnOptions> 延迟获取配置。
- UpdateAsync 改为仅当 EnabledConcurrencyException 为 true 且实体实现 IHasConcurrencyStamp 时,使用 ExecuteCommandWithOptLockAsync 并捕获 VersionExceptions 抛出 AbpDbConcurrencyException;否则回退到原有的 UpdateAsync 实现。
- 清理/调整部分 using 引用,新增 Microsoft.Extensions.Options 与 Volo.Abp.DependencyInjection 引用。

注意:默认值为 false,需在配置中显式开启 EnabledConcurrencyException 才会启用乐观并发校验,开启后会改变之前对带版本实体自动使用乐观锁的行为。
This commit is contained in:
chenchun
2025-11-17 11:19:15 +08:00
parent 2ec7b5f4fd
commit 5eaffe2ec2
3 changed files with 27 additions and 17 deletions

View File

@@ -58,5 +58,10 @@ namespace Yi.Framework.SqlSugarCore.Abstractions
/// 是否启用SaaS多租户 /// 是否启用SaaS多租户
/// </summary> /// </summary>
public bool EnabledSaasMultiTenancy { get; set; } = false; public bool EnabledSaasMultiTenancy { get; set; } = false;
/// <summary>
/// 是否开启更新并发乐观锁
/// </summary>
public bool EnabledConcurrencyException { get;set; } = false;
} }
} }

View File

@@ -1,12 +1,9 @@
using System.Linq; using System.Linq.Expressions;
using System.Linq.Expressions; using Microsoft.Extensions.Options;
using System.Text;
using Microsoft.Extensions.Logging;
using Nito.AsyncEx; using Nito.AsyncEx;
using SqlSugar; using SqlSugar;
using Volo.Abp;
using Volo.Abp.Auditing;
using Volo.Abp.Data; using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories; using Volo.Abp.Domain.Repositories;
using Volo.Abp.Linq; using Volo.Abp.Linq;
@@ -23,6 +20,9 @@ namespace Yi.Framework.SqlSugarCore.Repositories
private readonly ISugarDbContextProvider<ISqlSugarDbContext> _dbContextProvider; private readonly ISugarDbContextProvider<ISqlSugarDbContext> _dbContextProvider;
public IAbpLazyServiceProvider LazyServiceProvider { get; set; }
protected DbConnOptions? Options => LazyServiceProvider?.LazyGetService<IOptions<DbConnOptions>>().Value;
/// <summary> /// <summary>
/// 异步查询执行器 /// 异步查询执行器
/// </summary> /// </summary>
@@ -380,20 +380,24 @@ namespace Yi.Framework.SqlSugarCore.Repositories
public virtual async Task<bool> UpdateAsync(TEntity updateObj) public virtual async Task<bool> UpdateAsync(TEntity updateObj)
{ {
if (typeof(TEntity).IsAssignableTo<IHasConcurrencyStamp>())//带版本号乐观锁更新 if (Options is not null && Options.EnabledConcurrencyException)
{ {
try if (typeof(TEntity).IsAssignableTo<IHasConcurrencyStamp>()) //带版本号乐观锁更新
{ {
int num = await (await GetDbSimpleClientAsync()) try
.Context.Updateable(updateObj).ExecuteCommandWithOptLockAsync(true); {
return num>0; int num = await (await GetDbSimpleClientAsync())
} .Context.Updateable(updateObj).ExecuteCommandWithOptLockAsync(true);
catch (VersionExceptions ex) return num > 0;
{ }
catch (VersionExceptions ex)
throw new AbpDbConcurrencyException($"{ex.Message}[更新失败ConcurrencyStamp不是最新版本],entityInfo{updateObj}", ex); {
throw new AbpDbConcurrencyException(
$"{ex.Message}[更新失败ConcurrencyStamp不是最新版本],entityInfo{updateObj}", ex);
}
} }
} }
return await (await GetDbSimpleClientAsync()).UpdateAsync(updateObj); return await (await GetDbSimpleClientAsync()).UpdateAsync(updateObj);
} }

View File

@@ -40,7 +40,8 @@
"EnabledDbSeed": true, "EnabledDbSeed": true,
"EnableUnderLine": false, // 启用驼峰转下划线 "EnableUnderLine": false, // 启用驼峰转下划线
//SAAS多租户 //SAAS多租户
"EnabledSaasMultiTenancy": true "EnabledSaasMultiTenancy": true,
"EnabledConcurrencyException": false
//读写分离地址 //读写分离地址
//"ReadUrl": [ //"ReadUrl": [
// "DataSource=[xxxx]", //Sqlite // "DataSource=[xxxx]", //Sqlite