加急修复 UnitOfWork 实现的 bug

This commit is contained in:
28810
2019-03-14 16:45:13 +08:00
parent 2ab59201b0
commit 89ff4ca44d
5 changed files with 28 additions and 18 deletions

View File

@ -18,35 +18,45 @@ namespace FreeSql {
_fsql = fsql;
}
DbTransaction BeginTransaction() {
void ReturnObject() {
_fsql.Ado.MasterPool.Return(_conn);
_tran = null;
_conn = null;
}
internal DbTransaction GetOrBeginTransaction() {
_isCommitOrRoolback = false;
if (_tran != null) return _tran;
if (_conn != null) _fsql.Ado.MasterPool.Return(_conn);
_conn = _fsql.Ado.MasterPool.Get();
try {
_tran = _conn.Value.BeginTransaction();
} catch {
_fsql.Ado.MasterPool.Return(_conn);
_conn = null;
ReturnObject();
throw;
}
return _tran;
}
public void Commit() {
_isCommitOrRoolback = true;
if (_conn != null) {
if (_tran != null) {
try {
_tran.Commit();
_isCommitOrRoolback = true;
} finally {
_fsql.Ado.MasterPool.Return(_conn);
ReturnObject();
}
}
}
public void Rollback() {
_isCommitOrRoolback = true;
if (_conn != null) {
if (_tran != null) {
try {
_tran.Rollback();
_isCommitOrRoolback = true;
} finally {
_fsql.Ado.MasterPool.Return(_conn);
ReturnObject();
}
}
}
@ -58,12 +68,12 @@ namespace FreeSql {
public DefaultRepository<TEntity, TKey> GetRepository<TEntity, TKey>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class {
var repos = new DefaultRepository<TEntity, TKey>(_fsql, filter);
repos._tran = BeginTransaction();
repos._unitOfWork = this;
return repos;
}
public GuidRepository<TEntity> GetGuidRepository<TEntity>(Expression<Func<TEntity, bool>> filter = null, Func<string, string> asTable = null) where TEntity : class {
var repos = new GuidRepository<TEntity>(_fsql, filter, asTable);
repos._tran = BeginTransaction();
repos._unitOfWork = this;
return repos;
}
}