fix: 修复优化uow问题

main
NoahLan 8 months ago
parent 8fe06ee495
commit 9cac129b15

@ -2,12 +2,10 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using NPin.Framework.SqlSugarCore.Abstractions;
using SqlSugar;
using Volo.Abp.Data;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Threading;
using Volo.Abp.Uow;
using Check = Volo.Abp.Check;
namespace NPin.Framework.SqlSugarCore.Uow;
@ -15,7 +13,6 @@ public class UnitOfWorkSqlSugarDbContextProvider<TDbContext> : ISugarDbContextPr
where TDbContext : ISqlSugarDbContext
{
private readonly ISqlSugarDbConnectionCreator _dbConnectionCreator;
private readonly string MasterTenantDbDefaultName = DbConnOptions.MasterTenantName;
private ILogger<UnitOfWorkSqlSugarDbContextProvider<TDbContext>> Logger { get; set; }
private IServiceProvider ServiceProvider { get; set; }
@ -56,10 +53,9 @@ public class UnitOfWorkSqlSugarDbContextProvider<TDbContext> : ISugarDbContextPr
ContextInstance.Current ??= (TDbContext)ServiceProvider.GetRequiredService<ISqlSugarDbContext>();
var dbContext = (TDbContext)ContextInstance.Current;
var output = DatabaseChange(dbContext, connectionStringName, connectionString);
// 提高体验取消uow的强制性
// 若不启用uow创建新db不开启事务
return output;
return dbContext;
}
// 尝试当前工作单元获取db
@ -93,57 +89,6 @@ public class UnitOfWorkSqlSugarDbContextProvider<TDbContext> : ISugarDbContextPr
return await ConnectionStringResolver.ResolveAsync(connectionStringName);
}
protected virtual TDbContext DatabaseChange(TDbContext dbContext, string configId, string connectionString)
{
// 没有检测到使用多租户功能,默认使用默认库即可
if (string.IsNullOrWhiteSpace(connectionString))
{
connectionString = dbContext.Options.Url;
configId = DbConnOptions.DefaultTenantName;
}
var dbOption = dbContext.Options;
var db = dbContext.SqlSugarClient.AsTenant();
// 主库的Db切换当操作的是租户表的时候
if (CurrentTenant.Name == MasterTenantDbDefaultName)
{
//直接切换
configId = MasterTenantDbDefaultName;
var conStrOrNull = dbOption.GetDefaultMasterSaasMultiTenancy();
Check.NotNull(conStrOrNull, "租户主库未找到");
connectionString = conStrOrNull.Url;
}
// 租户Db的动态切换
// 二级缓存
var changed = false;
if (!db.IsAnyConnection(configId))
{
var config = _dbConnectionCreator.Build(options =>
{
options.DbType = dbOption.DbType!.Value;
options.ConfigId = configId; //设置库的唯一标识
options.IsAutoCloseConnection = true;
options.ConnectionString = connectionString;
});
//添加一个db到当前上下文 (Add部分不写上下文不会共享)
db.AddConnection(config);
changed = true;
}
var currentDb = db.GetConnection(configId) as ISqlSugarClient;
//设置Aop
if (changed)
{
_dbConnectionCreator.SetDbAop(currentDb);
}
dbContext.SetSqlSugarClient(currentDb);
return dbContext;
}
protected virtual async Task<TDbContext> CreateDbContextAsync(IUnitOfWork unitOfWork, string connectionStringName,
string connectionString)
{
@ -152,10 +97,7 @@ public class UnitOfWorkSqlSugarDbContextProvider<TDbContext> : ISugarDbContextPr
using (SqlSugarDbContextCreationContext.Use(creationContext))
{
var dbContext = await CreateDbContextAsync(unitOfWork);
//获取到DB之后对多租户多库进行处理
var changedDbContext = DatabaseChange(dbContext, connectionStringName, connectionString);
return changedDbContext;
return dbContext;
}
}
@ -169,7 +111,7 @@ public class UnitOfWorkSqlSugarDbContextProvider<TDbContext> : ISugarDbContextPr
protected virtual async Task<TDbContext> CreateDbContextWithTransactionAsync(IUnitOfWork unitOfWork)
{
//事务key
var transactionApiKey = $"SqlsugarCore_{SqlSugarDbContextCreationContext.Current.ConnectionString}";
var transactionApiKey = $"SqlSugarCore_{SqlSugarDbContextCreationContext.Current.ConnectionString}";
//尝试查找事务
var activeTransaction = unitOfWork.FindTransactionApi(transactionApiKey) as SqlSugarTransactionApi;
@ -184,12 +126,10 @@ public class UnitOfWorkSqlSugarDbContextProvider<TDbContext> : ISugarDbContextPr
);
unitOfWork.AddTransactionApi(transactionApiKey, transaction);
await dbContext.SqlSugarClient.Ado.BeginTranAsync();
// await dbContext.SqlSugarClient.Ado.BeginTranAsync();
return dbContext;
}
else
{
return (TDbContext)activeTransaction.GetDbContext();
}
return (TDbContext)activeTransaction.GetDbContext();
}
}
Loading…
Cancel
Save