多航户下使用 GetConnectionScope ,new StackTrace(true) 会导致 CPU开销大吗 返回

SqlSugar 沟通中
4 334
该叫什么 . 发布于2026/6/22
悬赏:0 飞吻

偶尔会有高CPU占用,通过分析  dotnet-dump , 发现问题在  System.Diagnostics.StackTrace.GetStackFramesInternal(System.Diagnostics.StackFrameHelper, Int32, Boolean, System.Exception)

经分析后的完整链接:

XXX.PublishSuccessAsync → BaseRepository.SaveQueuesAsync → SqlSugarScopeProvider.SaveQueuesAsync → get_ScopedContext () → GetContext () → new StackTrace (true) → StackTrace.GetStackFramesInternal ,

经过查看源代码,发现 :

        public SqlSugarScopeProvider(SqlSugarProvider conn)
        {
            this.conn = conn;            this.initThreadMainId = GetCurrentThreadId();            var key = GetKey();            this.initkey = key;            this.GetContext(true);        }
GetKey、GetContext 都会调用 new StackTrace(true) ,这个会带来性能影响吗?

调用
GetConnection 、 GetConnectionScope 的 本质区别在哪里呢


热忱回答4

  • SaveQueuesAsync  这个方法不用了

    0 回复
  • https://www.donet5.com/Home/Doc?typeId=1200

    0 回复
  • . . VIP0
    2026/6/22

    contextID 这些确认是按照链接 (2)SqlSugarScope  部分处理;


        protected override ISqlSugarClient GetDbClient(IUnitOfWork unitOfWork)
        {
            var db = unitOfWork.GetDbClient();        var tenantContext = _serviceProvider.GetService<ITenantContext>();        var iTenant = db.AsTenant();        if (!iTenant.IsAnyConnection(tenantContext.TenantCode))
            {
                var tenantConnectionConfigService = _serviceProvider.GetService<ITenantConnectionConfigService>();            var connConfig = tenantConnectionConfigService.GetConnectionConfig(tenantContext.TenantCode);            iTenant.AddConnection(connConfig);        }
    
            return iTenant.GetConnectionScope(tenantContext.TenantCode);    }

    其中链接地址是通过租户代码获取 ,可以直接替换为   return iTenant.GetConnection(tenantContext.TenantCode); 吗


    0 回复
  • fate sta fate sta VIP0
    2026/6/23

    你也可以用getconnection这样不进这个方法

    0 回复