防止同连接字符串被IFreeSql使用多次,发生连接池溢出bug(ado.net连接池原理,减少解释成本)

This commit is contained in:
28810
2019-02-15 13:53:10 +08:00
parent 488a6edd4d
commit 84449e57f3
5 changed files with 20 additions and 10 deletions

View File

@ -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();