From b2ad000e07e1c87d3561b5aa94fdc88c779872f0 Mon Sep 17 00:00:00 2001 From: dengjunjie <dengjunjie@hnkhzn.com> Date: 星期二, 18 二月 2025 22:34:54 +0800 Subject: [PATCH] 1 --- 项目代码/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseRepository/UnitOfWorks/UnitOfWorkManage.cs | 194 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 194 insertions(+), 0 deletions(-) diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseRepository/UnitOfWorks/UnitOfWorkManage.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseRepository/UnitOfWorks/UnitOfWorkManage.cs" new file mode 100644 index 0000000..81607a6 --- /dev/null +++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseRepository/UnitOfWorks/UnitOfWorkManage.cs" @@ -0,0 +1,194 @@ +锘縰sing Microsoft.Extensions.Logging; +using SqlSugar; +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using WIDESEA_Core.Helper; + +namespace WIDESEA_Core.BaseRepository +{ + public class UnitOfWorkManage : IUnitOfWorkManage + { + private readonly ILogger<UnitOfWorkManage> _logger; + private readonly ISqlSugarClient _sqlSugarClient; + + private int _tranCount { get; set; } + public int TranCount => _tranCount; + public readonly ConcurrentStack<string> TranStack = new(); + + public UnitOfWorkManage(ISqlSugarClient sqlSugarClient, ILogger<UnitOfWorkManage> logger) + { + _sqlSugarClient = sqlSugarClient; + _logger = logger; + _tranCount = 0; + } + + /// <summary> + /// 鑾峰彇DB锛屼繚璇佸敮涓�鎬� + /// </summary> + /// <returns></returns> + public SqlSugarClient GetDbClient() + { + // 蹇呴』瑕乤s锛屽悗杈逛細鐢ㄥ埌鍒囨崲鏁版嵁搴撴搷浣� + return _sqlSugarClient as SqlSugarClient; + } + + + public UnitOfWork CreateUnitOfWork() + { + UnitOfWork uow = new UnitOfWork(); + uow.Logger = _logger; + uow.Db = _sqlSugarClient; + uow.Tenant = (ITenant)_sqlSugarClient; + uow.IsTran = true; + + uow.Db.Open(); + uow.Tenant.BeginTran(); + + _logger.LogDebug("UnitOfWork Begin"); + return uow; + } + + public void BeginTran() + { + lock (this) + { + _tranCount++; + GetDbClient().BeginTran(); + } + } + + public void BeginTran(MethodInfo method) + { + lock (this) + { + GetDbClient().BeginTran(); + TranStack.Push(method.GetFullName()); + _tranCount = TranStack.Count; + } + } + + public WebResponseContent BeginTran(Func<WebResponseContent> func) + { + lock (this) + { + BeginTran(); + try + { + WebResponseContent content = func(); + if (content.Status) + { + CommitTran(); + } + else + { + RollbackTran(); + } + return content; + } + catch(Exception ex) + { + RollbackTran(); + return WebResponseContent.Instance.Error(ex.Message); + } + } + } + + public void CommitTran() + { + lock (this) + { + _tranCount--; + if (_tranCount == 0) + { + try + { + GetDbClient().CommitTran(); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + GetDbClient().RollbackTran(); + } + } + } + } + + public void CommitTran(MethodInfo method) + { + lock (this) + { + string result = ""; + while (!TranStack.IsEmpty && !TranStack.TryPeek(out result)) + { + Thread.Sleep(1); + } + + + if (result == method.GetFullName()) + { + try + { + GetDbClient().CommitTran(); + + _logger.LogDebug($"Commit Transaction"); + Console.WriteLine($"Commit Transaction"); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + GetDbClient().RollbackTran(); + _logger.LogDebug($"Commit Error , Rollback Transaction"); + } + finally + { + while (!TranStack.TryPop(out _)) + { + Thread.Sleep(1); + } + + _tranCount = TranStack.Count; + } + } + } + } + + public void RollbackTran() + { + lock (this) + { + _tranCount--; + GetDbClient().RollbackTran(); + } + } + + public void RollbackTran(MethodInfo method) + { + lock (this) + { + string result = ""; + while (!TranStack.IsEmpty && !TranStack.TryPeek(out result)) + { + Thread.Sleep(1); + } + + if (result == method.GetFullName()) + { + GetDbClient().RollbackTran(); + _logger.LogDebug($"Rollback Transaction"); + Console.WriteLine($"Rollback Transaction"); + while (!TranStack.TryPop(out _)) + { + Thread.Sleep(1); + } + + _tranCount = TranStack.Count; + } + } + } + } +} -- Gitblit v1.9.3