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

@ -4,7 +4,7 @@ using System.Collections.Generic;
public interface IFreeSql<TMark> : IFreeSql { } public interface IFreeSql<TMark> : IFreeSql { }
public interface IFreeSql { public interface IFreeSql : IDisposable {
/// <summary> /// <summary>
/// 插入数据 /// 插入数据
/// </summary> /// </summary>

View File

@ -11,7 +11,7 @@ using System.Text;
using System.Reflection; using System.Reflection;
namespace FreeSql.Internal.CommonProvider { 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 void ReturnConnection(ObjectPool<DbConnection> pool, Object<DbConnection> conn, Exception ex);
protected abstract DbCommand CreateCommand(); protected abstract DbCommand CreateCommand();

View File

@ -110,13 +110,6 @@ namespace FreeSql.Internal.CommonProvider {
public void CommitTransaction() => CommitTransaction(true); public void CommitTransaction() => CommitTransaction(true);
public void RollbackTransaction() => CommitTransaction(false); 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) { public void Transaction(Action handler) {
Transaction(handler, TimeSpan.FromSeconds(60)); Transaction(handler, TimeSpan.FromSeconds(60));
} }
@ -130,5 +123,35 @@ namespace FreeSql.Internal.CommonProvider {
throw ex; 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; using System.Threading.Tasks;
namespace FreeSql.Internal.CommonProvider { namespace FreeSql.Internal.CommonProvider {
class CacheProvider : ICache { class CacheProvider : ICache, IDisposable {
public IDistributedCache Cache { get; private set; } public IDistributedCache Cache { get; private set; }
private bool CacheSupportMultiRemove = false; 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<object, string> Serialize { get; set; }
public Func<string, Type, object> Deserialize { 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 (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 && //骆峰命名 if (trytb.ColumnsByCs.TryGetValue($"{pnv.Name}{findtbrefPkCsName}", out var trycol) == false && //骆峰命名
trytb.ColumnsByCs.TryGetValue($"{pnv.Name}_{findtbrefPkCsName}", out 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 &&
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; if (isLazy) throw nvref.Exception;
continue; continue;
} }
if (trycol == null) { }
nvref.Exception = new Exception($"导航属性 {trytbTypeName}.{pnv.Name} 没有找到对应的字段,如:{pnv.Name}{findtbrefPkCsName}、{pnv.Name}_{findtbrefPkCsName}");
trytb.AddOrUpdateTableRef(pnv.Name, nvref); if (trycol == null) {
if (isLazy) throw nvref.Exception; nvref.Exception = new Exception($"导航属性 {trytbTypeName}.{pnv.Name} 没有找到对应的字段,如:{pnv.Name}{findtbrefPkCsName}、{pnv.Name}_{findtbrefPkCsName}");
continue; trytb.AddOrUpdateTableRef(pnv.Name, nvref);
} if (isLazy) throw nvref.Exception;
continue;
} }
nvref.Columns.Add(trycol); nvref.Columns.Add(trycol);

View File

@ -57,5 +57,15 @@ namespace FreeSql.MySql {
public void Transaction(Action handler) => Ado.Transaction(handler); public void Transaction(Action handler) => Ado.Transaction(handler);
public void Transaction(Action handler, TimeSpan timeout) => Ado.Transaction(handler, timeout); public void Transaction(Action handler, TimeSpan timeout) => Ado.Transaction(handler, timeout);
~MySqlProvider() {
this.Dispose();
}
bool _isdisposed = false;
public void Dispose() {
if (_isdisposed) return;
(this.Ado as AdoProvider).Dispose();
(this.Cache as CacheProvider)?.Dispose();
}
} }
} }

View File

@ -48,5 +48,15 @@ namespace FreeSql.Oracle {
public void Transaction(Action handler) => Ado.Transaction(handler); public void Transaction(Action handler) => Ado.Transaction(handler);
public void Transaction(Action handler, TimeSpan timeout) => Ado.Transaction(handler, timeout); public void Transaction(Action handler, TimeSpan timeout) => Ado.Transaction(handler, timeout);
~OracleProvider() {
this.Dispose();
}
bool _isdisposed = false;
public void Dispose() {
if (_isdisposed) return;
(this.Ado as AdoProvider).Dispose();
(this.Cache as CacheProvider)?.Dispose();
}
} }
} }

View File

@ -84,5 +84,15 @@ namespace FreeSql.PostgreSQL {
public void Transaction(Action handler) => Ado.Transaction(handler); public void Transaction(Action handler) => Ado.Transaction(handler);
public void Transaction(Action handler, TimeSpan timeout) => Ado.Transaction(handler, timeout); public void Transaction(Action handler, TimeSpan timeout) => Ado.Transaction(handler, timeout);
~PostgreSQLProvider() {
this.Dispose();
}
bool _isdisposed = false;
public void Dispose() {
if (_isdisposed) return;
(this.Ado as AdoProvider).Dispose();
(this.Cache as CacheProvider)?.Dispose();
}
} }
} }

View File

@ -55,5 +55,15 @@ namespace FreeSql.SqlServer {
public void Transaction(Action handler) => Ado.Transaction(handler); public void Transaction(Action handler) => Ado.Transaction(handler);
public void Transaction(Action handler, TimeSpan timeout) => Ado.Transaction(handler, timeout); public void Transaction(Action handler, TimeSpan timeout) => Ado.Transaction(handler, timeout);
~SqlServerProvider() {
this.Dispose();
}
bool _isdisposed = false;
public void Dispose() {
if (_isdisposed) return;
(this.Ado as AdoProvider).Dispose();
(this.Cache as CacheProvider)?.Dispose();
}
} }
} }

View File

@ -47,5 +47,15 @@ namespace FreeSql.Sqlite {
public void Transaction(Action handler) => Ado.Transaction(handler); public void Transaction(Action handler) => Ado.Transaction(handler);
public void Transaction(Action handler, TimeSpan timeout) => Ado.Transaction(handler, timeout); public void Transaction(Action handler, TimeSpan timeout) => Ado.Transaction(handler, timeout);
~SqliteProvider() {
this.Dispose();
}
bool _isdisposed = false;
public void Dispose() {
if (_isdisposed) return;
(this.Ado as AdoProvider).Dispose();
(this.Cache as CacheProvider)?.Dispose();
}
} }
} }