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:
@@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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>
|
||||||
@@ -379,6 +379,8 @@ namespace Yi.Framework.SqlSugarCore.Repositories
|
|||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task<bool> UpdateAsync(TEntity updateObj)
|
public virtual async Task<bool> UpdateAsync(TEntity updateObj)
|
||||||
|
{
|
||||||
|
if (Options is not null && Options.EnabledConcurrencyException)
|
||||||
{
|
{
|
||||||
if (typeof(TEntity).IsAssignableTo<IHasConcurrencyStamp>()) //带版本号乐观锁更新
|
if (typeof(TEntity).IsAssignableTo<IHasConcurrencyStamp>()) //带版本号乐观锁更新
|
||||||
{
|
{
|
||||||
@@ -390,10 +392,12 @@ namespace Yi.Framework.SqlSugarCore.Repositories
|
|||||||
}
|
}
|
||||||
catch (VersionExceptions ex)
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user