- 增加 UnitOfWork 静态属性 DebugBeingUsed,用于生产环境监视正在使用中的事务;

This commit is contained in:
28810
2019-11-21 21:55:04 +08:00
parent 04107d3d24
commit 8c5d5ddedc
9 changed files with 55 additions and 10 deletions

View File

@ -1,6 +1,7 @@
using SafeObjectPool;
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Data;
using System.Data.Common;
using System.Linq;
@ -14,10 +15,23 @@ namespace FreeSql
public static readonly AsyncLocal<IUnitOfWork> Current = new AsyncLocal<IUnitOfWork>();
#endif
static int _seed;
/// <summary>
/// 正在使用中的工作单元(调试)
/// </summary>
public static ConcurrentDictionary<string, UnitOfWork> DebugBeingUsed { get; } = new ConcurrentDictionary<string, UnitOfWork>();
protected IFreeSql _fsql;
protected Object<DbConnection> _conn;
protected DbTransaction _tran;
/// <summary>
/// 开启事务后有值,是 UnitOfWork 的唯一标识<para></para>
/// 格式yyyyMMdd_HHmmss_种子id<para></para>
/// 例如20191121_214504_1
/// </summary>
public string Id { get; private set; }
public UnitOfWork(IFreeSql fsql)
{
_fsql = fsql;
@ -28,6 +42,9 @@ namespace FreeSql
void ReturnObject()
{
if (string.IsNullOrEmpty(this.Id) == false && DebugBeingUsed.TryRemove(this.Id, out var old))
this.Id = null;
_fsql.Ado.MasterPool.Return(_conn);
_tran = null;
_conn = null;
@ -66,6 +83,9 @@ namespace FreeSql
_tran = IsolationLevel == null ?
_conn.Value.BeginTransaction() :
_conn.Value.BeginTransaction(IsolationLevel.Value);
this.Id = $"{DateTime.Now.ToString("yyyyMMdd_HHmmss")}_{Interlocked.Increment(ref _seed)}";
DebugBeingUsed.TryAdd(this.Id, this);
}
catch
{