From 650d59aaffe90f5341a9dc92311ae2a43b883657 Mon Sep 17 00:00:00 2001 From: OpenClaw Agent Date: Wed, 25 Mar 2026 02:47:00 +0000 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=BB=9F=E4=B8=80=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20SqlSugarClient=EF=BC=88=E5=AE=98=E6=96=B9=E6=8E=A8?= =?UTF-8?q?=E8=8D=90=E9=AB=98=E6=80=A7=E8=83=BD=E6=A8=A1=E5=BC=8F=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 官方建议: - SqlSugarClient 超高性能模式 - 每次 new 创建实例 - IsAutoCloseConnection=true 自动关闭连接 - 适合 DI Scoped 注入、后台任务、Task.Run 场景 修改: 1. DI 注入改为 Scoped(每次 HTTP 请求一个实例) 2. 移除 SqlSugarScope,统一使用 SqlSugarClient 3. 后台任务创建新实例(已实现) 4. MySQL 连接池复用底层 TCP 连接 优点: - 性能更好 - 代码更简单 - 符合官方最佳实践 --- AssetManager.Data/DatabaseExtensions.cs | 8 ++--- AssetManager.Data/SqlSugarConfig.cs | 41 ++++--------------------- 2 files changed, 10 insertions(+), 39 deletions(-) diff --git a/AssetManager.Data/DatabaseExtensions.cs b/AssetManager.Data/DatabaseExtensions.cs index c8af1a3..fe2003b 100755 --- a/AssetManager.Data/DatabaseExtensions.cs +++ b/AssetManager.Data/DatabaseExtensions.cs @@ -8,11 +8,11 @@ public static class DatabaseExtensions { public static IServiceCollection AddDatabase(this IServiceCollection services) { - // SqlSugarScope 是线程安全的,内部使用 AsyncLocal 隔离上下文 - // 使用 Singleton 注册,整个应用共享一个实例 - services.AddSingleton(s => + // SqlSugarClient 超高性能模式:每次请求创建新实例 + // Scoped 确保:同一 HTTP 请求内复用,不同请求隔离 + services.AddScoped(s => { - return SqlSugarConfig.GetSqlSugarScope(); + return SqlSugarConfig.GetSqlSugarClient(); }); services.AddScoped(); diff --git a/AssetManager.Data/SqlSugarConfig.cs b/AssetManager.Data/SqlSugarConfig.cs index 2a54873..71bd345 100755 --- a/AssetManager.Data/SqlSugarConfig.cs +++ b/AssetManager.Data/SqlSugarConfig.cs @@ -16,7 +16,9 @@ public static class SqlSugarConfig } /// - /// 获取 SqlSugarClient 实例(用于后台任务,每次创建新实例) + /// 获取 SqlSugarClient 实例(超高性能模式) + /// 每次 new 创建实例,IsAutoCloseConnection=true 自动关闭连接 + /// 适合:DI Scoped 注入、后台任务、Task.Run 场景 /// public static ISqlSugarClient GetSqlSugarClient() { @@ -27,44 +29,13 @@ public static class SqlSugarConfig var connectionString = GetConnectionString(); - // 使用 SqlSugarClient(非 Scope),每次创建独立实例 - // 适合后台任务、Task.Run 场景 + // SqlSugarClient 超高性能模式 + // 每次创建新实例,IsAutoCloseConnection=true 确保连接自动关闭 return new SqlSugarClient(new ConnectionConfig() { ConnectionString = connectionString, DbType = DbType.MySql, - IsAutoCloseConnection = true, - InitKeyType = InitKeyType.Attribute, - ConfigureExternalServices = new ConfigureExternalServices - { - EntityService = (property, column) => - { - if (property.PropertyType == typeof(DateTime)) - { - column.DataType = "datetime(3)"; - } - } - } - }); - } - - /// - /// 获取 SqlSugarScope 实例(用于 DI 注入,Singleton/Scoped) - /// - public static ISqlSugarClient GetSqlSugarScope() - { - if (_configuration == null) - { - throw new InvalidOperationException("SqlSugarConfig has not been initialized. Call Initialize() first."); - } - - var connectionString = GetConnectionString(); - - return new SqlSugarScope(new ConnectionConfig() - { - ConnectionString = connectionString, - DbType = DbType.MySql, - IsAutoCloseConnection = true, + IsAutoCloseConnection = true, // 必须为 true InitKeyType = InitKeyType.Attribute, ConfigureExternalServices = new ConfigureExternalServices {