mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 20:38:16 +08:00
防止同连接字符串被IFreeSql使用多次,发生连接池溢出bug(ado.net连接池原理,减少解释成本)
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
using SafeObjectPool;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
@ -49,6 +50,7 @@ namespace FreeSql.Sqlite {
|
||||
public int CheckAvailableInterval { get; set; } = 5;
|
||||
public string[] Attaches = new string[0];
|
||||
|
||||
static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
|
||||
private string _connectionString;
|
||||
public string ConnectionString {
|
||||
get => _connectionString;
|
||||
@ -57,7 +59,8 @@ namespace FreeSql.Sqlite {
|
||||
var poolsizePatern = @"Max\s*pool\s*size\s*=\s*(\d+)";
|
||||
Match m = Regex.Match(connStr, poolsizePatern, RegexOptions.IgnoreCase);
|
||||
if (m.Success == false || int.TryParse(m.Groups[1].Value, out var poolsize) == false || poolsize <= 0) poolsize = 100;
|
||||
PoolSize = poolsize + 1;
|
||||
var connStrIncr = dicConnStrIncr.AddOrUpdate(connStr, 1, (oldkey, oldval) => oldval + 1);
|
||||
PoolSize = poolsize + connStrIncr;
|
||||
_connectionString = m.Success ?
|
||||
Regex.Replace(connStr, poolsizePatern, $"Max pool size={PoolSize}", RegexOptions.IgnoreCase) :
|
||||
$"{connStr};Max pool size={PoolSize}";
|
||||
@ -74,7 +77,6 @@ namespace FreeSql.Sqlite {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public bool OnCheckAvailable(Object<DbConnection> obj) {
|
||||
if ((obj.Value as SQLiteConnection).Ping() == false) obj.Value.OpenAndAttach(Attaches);
|
||||
return (obj.Value as SQLiteConnection).Ping();
|
||||
|
Reference in New Issue
Block a user