From 9cac129b1544637ad41f12c3183da383cb62695c Mon Sep 17 00:00:00 2001 From: NoahLan <6995syu@163.com> Date: Wed, 28 Feb 2024 10:28:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BC=98=E5=8C=96uow?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UnitOfWorkSqlSugarDbContextProvider.cs | 72 ++----------------- 1 file changed, 6 insertions(+), 66 deletions(-) diff --git a/framework/NPin.Framework.SqlSugarCore/Uow/UnitOfWorkSqlSugarDbContextProvider.cs b/framework/NPin.Framework.SqlSugarCore/Uow/UnitOfWorkSqlSugarDbContextProvider.cs index 675c8ca..e227fb3 100644 --- a/framework/NPin.Framework.SqlSugarCore/Uow/UnitOfWorkSqlSugarDbContextProvider.cs +++ b/framework/NPin.Framework.SqlSugarCore/Uow/UnitOfWorkSqlSugarDbContextProvider.cs @@ -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 : ISugarDbContextPr where TDbContext : ISqlSugarDbContext { private readonly ISqlSugarDbConnectionCreator _dbConnectionCreator; - private readonly string MasterTenantDbDefaultName = DbConnOptions.MasterTenantName; private ILogger> Logger { get; set; } private IServiceProvider ServiceProvider { get; set; } @@ -56,10 +53,9 @@ public class UnitOfWorkSqlSugarDbContextProvider : ISugarDbContextPr ContextInstance.Current ??= (TDbContext)ServiceProvider.GetRequiredService(); 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 : 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 CreateDbContextAsync(IUnitOfWork unitOfWork, string connectionStringName, string connectionString) { @@ -152,10 +97,7 @@ public class UnitOfWorkSqlSugarDbContextProvider : 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 : ISugarDbContextPr protected virtual async Task 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 : 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(); } } \ No newline at end of file