优化连接池

This commit is contained in:
李超 2026-04-20 11:16:59 +08:00
parent 8a61b56389
commit bd1ddfb0e7
4 changed files with 30 additions and 13 deletions

View File

@ -5,6 +5,7 @@ namespace BLL
using Quartz; using Quartz;
using RestSharp; using RestSharp;
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Globalization; using System.Globalization;
@ -22,13 +23,13 @@ namespace BLL
/// <summary> /// <summary>
/// 维护一个DB集合 /// 维护一个DB集合
/// </summary> /// </summary>
private static Dictionary<int, Model.SGGLDB> dataBaseLinkList = new System.Collections.Generic.Dictionary<int, Model.SGGLDB>(); private static ConcurrentDictionary<int, Model.SGGLDB> dataBaseLinkList = new ConcurrentDictionary<int, Model.SGGLDB>();
/// <summary> /// <summary>
/// 维护一个DB集合 /// 维护一个DB集合
/// </summary> /// </summary>
public static System.Collections.Generic.Dictionary<int, Model.SGGLDB> DBList public static ConcurrentDictionary<int, Model.SGGLDB> DBList
{ {
get get
{ {
@ -40,7 +41,7 @@ namespace BLL
get; get;
set; set;
} }
public static Dictionary<string, string> WebEditList = new Dictionary<string, string>(); public static ConcurrentDictionary<string, string> WebEditList = new ConcurrentDictionary<string, string>();
/// <summary> /// <summary>
/// 数据库连接字符串 /// 数据库连接字符串
@ -158,7 +159,7 @@ namespace BLL
// } // }
//} //}
private static object locker = new object(); //private static object locker = new object();
/// <summary> /// <summary>
/// 数据库上下文。 /// 数据库上下文。
@ -167,13 +168,13 @@ namespace BLL
{ {
get get
{ {
lock (locker) // lock (locker)
{ {
try try
{ {
if (!DBList.ContainsKey(System.Threading.Thread.CurrentThread.ManagedThreadId)) if (!DBList.ContainsKey(System.Threading.Thread.CurrentThread.ManagedThreadId))
{ {
DBList.Add(System.Threading.Thread.CurrentThread.ManagedThreadId, new SGGLDB(connString)); DBList.TryAdd(System.Threading.Thread.CurrentThread.ManagedThreadId, new SGGLDB(connString));
} }
// DBList[System.Threading.Thread.CurrentThread.ManagedThreadId].CommandTimeout = 1200; // DBList[System.Threading.Thread.CurrentThread.ManagedThreadId].CommandTimeout = 1200;
@ -188,7 +189,7 @@ namespace BLL
} }
else else
{ {
DBList.Add(System.Threading.Thread.CurrentThread.ManagedThreadId, db); DBList.TryAdd(System.Threading.Thread.CurrentThread.ManagedThreadId, db);
} }
return db; return db;
} }

View File

@ -235,7 +235,12 @@ namespace BLL
{ {
if (BLL.Funs.DBList.ContainsKey(System.Threading.Thread.CurrentThread.ManagedThreadId)) if (BLL.Funs.DBList.ContainsKey(System.Threading.Thread.CurrentThread.ManagedThreadId))
{ {
BLL.Funs.DBList.Remove(System.Threading.Thread.CurrentThread.ManagedThreadId); Model.SGGLDB db;
BLL.Funs.DBList.TryRemove(System.Threading.Thread.CurrentThread.ManagedThreadId,out db);
if (db != null)
{
db.Dispose();
}
} }
} }
} }

View File

@ -330,7 +330,12 @@ namespace FineUIPro.Web
{ {
if (BLL.Funs.DBList.ContainsKey(System.Threading.Thread.CurrentThread.ManagedThreadId)) if (BLL.Funs.DBList.ContainsKey(System.Threading.Thread.CurrentThread.ManagedThreadId))
{ {
BLL.Funs.DBList.Remove(System.Threading.Thread.CurrentThread.ManagedThreadId); Model.SGGLDB db ;
BLL.Funs.DBList.TryRemove(System.Threading.Thread.CurrentThread.ManagedThreadId,out db);
if (db != null)
{
db.Dispose();
}
} }
if (BLL.SQLHelper.GetConn().State == ConnectionState.Open) if (BLL.SQLHelper.GetConn().State == ConnectionState.Open)

View File

@ -138,13 +138,19 @@ namespace WebAPI.Filter
} }
if (!isOk) if (!isOk)
{ {
if (BLL.Funs.DBList.ContainsKey(System.Threading.Thread.CurrentThread.ManagedThreadId))
{
BLL.Funs.DBList.Remove(System.Threading.Thread.CurrentThread.ManagedThreadId);
}
base.OnActionExecuted(actionExecutedContext); base.OnActionExecuted(actionExecutedContext);
} }
if (BLL.Funs.DBList.ContainsKey(System.Threading.Thread.CurrentThread.ManagedThreadId))
{
Model.SGGLDB db;
BLL.Funs.DBList.TryRemove(System.Threading.Thread.CurrentThread.ManagedThreadId, out db);
if (db != null)
{
db.Dispose();
}
}
} }
/// <summary> /// <summary>