| | |
| | | using 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 WIDESEAWCS_Core.Helper; |
| | | |
| | | namespace WIDESEAWCS_Core.BaseRepository |
| | |
| | | { |
| | | // 定义日志记录器 |
| | | private readonly ILogger<UnitOfWorkManage> _logger; |
| | | |
| | | // 定义SqlSugarClient |
| | | private readonly ISqlSugarClient _sqlSugarClient; |
| | | |
| | | // 定义事务计数器 |
| | | private int _tranCount { get; set; } |
| | | |
| | | // 定义事务计数器的只读属性 |
| | | public int TranCount => _tranCount; |
| | | |
| | | // 定义事务栈 |
| | | public readonly ConcurrentStack<string> TranStack = new(); |
| | | |
| | |
| | | return _sqlSugarClient as SqlSugarClient; |
| | | } |
| | | |
| | | |
| | | // 创建UnitOfWork |
| | | public UnitOfWork CreateUnitOfWork() |
| | | { |
| | |
| | | |
| | | uow.Db.Open(); |
| | | uow.Tenant.BeginTran(); |
| | | |
| | | |
| | | _logger.LogDebug("UnitOfWork Begin"); |
| | | return uow; |
| | | } |
| | |
| | | } |
| | | return content; |
| | | } |
| | | catch(Exception ex) |
| | | catch (Exception ex) |
| | | { |
| | | RollbackTran(); |
| | | return WebResponseContent.Instance.Error(ex.Message); |
| | |
| | | lock (this) |
| | | { |
| | | string result = ""; |
| | | var spinner = new SpinWait(); |
| | | while (!TranStack.IsEmpty && !TranStack.TryPeek(out result)) |
| | | { |
| | | Thread.Sleep(1); |
| | | spinner.SpinOnce(); |
| | | } |
| | | |
| | | |
| | | if (result == method.GetFullName()) |
| | | { |
| | |
| | | } |
| | | finally |
| | | { |
| | | var spinner2 = new SpinWait(); |
| | | while (!TranStack.TryPop(out _)) |
| | | { |
| | | Thread.Sleep(1); |
| | | spinner2.SpinOnce(); |
| | | } |
| | | |
| | | _tranCount = TranStack.Count; |
| | |
| | | lock (this) |
| | | { |
| | | string result = ""; |
| | | var spinner = new SpinWait(); |
| | | while (!TranStack.IsEmpty && !TranStack.TryPeek(out result)) |
| | | { |
| | | Thread.Sleep(1); |
| | | spinner.SpinOnce(); |
| | | } |
| | | |
| | | if (result == method.GetFullName()) |
| | |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |