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; }

View File

@ -542,7 +542,7 @@ namespace FreeSql.Internal {
if (findtbrefPkCsName.StartsWith(tbref.Type.Name, StringComparison.CurrentCultureIgnoreCase)) findtbrefPkCsName = findtbrefPkCsName.Substring(tbref.Type.Name.Length).TrimStart('_');
if (trytb.ColumnsByCs.TryGetValue($"{pnv.Name}{findtbrefPkCsName}", out var trycol) == false && //骆峰命名
trytb.ColumnsByCs.TryGetValue($"{pnv.Name}_{findtbrefPkCsName}", out trycol) == false && //下划线命名
tbref.Primarys.Length == 1 &&
//tbref.Primarys.Length == 1 &&
trytb.ColumnsByCs.TryGetValue($"{pnv.Name}_Id", out trycol) == false &&
trytb.ColumnsByCs.TryGetValue($"{pnv.Name}Id", out trycol) == false
) {
@ -569,12 +569,13 @@ namespace FreeSql.Internal {
if (isLazy) throw nvref.Exception;
continue;
}
if (trycol == null) {
nvref.Exception = new Exception($"导航属性 {trytbTypeName}.{pnv.Name} 没有找到对应的字段,如:{pnv.Name}{findtbrefPkCsName}、{pnv.Name}_{findtbrefPkCsName}");
trytb.AddOrUpdateTableRef(pnv.Name, nvref);
if (isLazy) throw nvref.Exception;
continue;
}
}
if (trycol == null) {
nvref.Exception = new Exception($"导航属性 {trytbTypeName}.{pnv.Name} 没有找到对应的字段,如:{pnv.Name}{findtbrefPkCsName}、{pnv.Name}_{findtbrefPkCsName}");
trytb.AddOrUpdateTableRef(pnv.Name, nvref);
if (isLazy) throw nvref.Exception;
continue;
}
nvref.Columns.Add(trycol);