IFreeSql 增加 IDisposable

This commit is contained in:
28810
2019-04-29 18:22:03 +08:00
parent 163fe89bd4
commit f5a292ef45
10 changed files with 101 additions and 17 deletions

View File

@ -11,7 +11,7 @@ using System.Text;
using System.Reflection;
namespace FreeSql.Internal.CommonProvider {
abstract partial class AdoProvider : IAdo {
abstract partial class AdoProvider : IAdo, IDisposable {
protected abstract void ReturnConnection(ObjectPool<DbConnection> pool, Object<DbConnection> conn, Exception ex);
protected abstract DbCommand CreateCommand();

View File

@ -110,13 +110,6 @@ namespace FreeSql.Internal.CommonProvider {
public void CommitTransaction() => CommitTransaction(true);
public void RollbackTransaction() => CommitTransaction(false);
public void Dispose() {
Transaction2[] trans = null;
lock (_trans_lock)
trans = _trans.Values.ToArray();
foreach (Transaction2 tran in trans) CommitTransaction(false, tran);
}
public void Transaction(Action handler) {
Transaction(handler, TimeSpan.FromSeconds(60));
}
@ -130,5 +123,35 @@ namespace FreeSql.Internal.CommonProvider {
throw ex;
}
}
~AdoProvider() {
this.Dispose();
}
bool _isdisposed = false;
public void Dispose() {
if (_isdisposed) return;
try {
Transaction2[] trans = null;
lock (_trans_lock)
trans = _trans.Values.ToArray();
foreach (Transaction2 tran in trans) CommitTransaction(false, tran);
} catch { }
ObjectPool<DbConnection>[] pools = null;
for (var a = 0; a < 10; a++) {
try {
pools = SlavePools.ToArray();
SlavePools.Clear();
break;
} catch {
}
}
if (pools != null) {
foreach (var pool in pools) {
try { pool.Dispose(); } catch { }
}
}
try { MasterPool.Dispose(); } catch { }
}
}
}

View File

@ -9,7 +9,7 @@ using System.Text;
using System.Threading.Tasks;
namespace FreeSql.Internal.CommonProvider {
class CacheProvider : ICache {
class CacheProvider : ICache, IDisposable {
public IDistributedCache Cache { get; private set; }
private bool CacheSupportMultiRemove = false;
@ -30,6 +30,16 @@ namespace FreeSql.Internal.CommonProvider {
}
}
~CacheProvider() {
this.Dispose();
}
bool _isdisposed = false;
public void Dispose() {
if (_isdisposed) return;
Cache = null;
}
public Func<object, string> Serialize { get; set; }
public Func<string, Type, object> Deserialize { get; set; }