- 优化 UnitOfWork 与 ForUpdate 事务开启逻辑;

This commit is contained in:
2881099 2022-12-08 15:49:18 +08:00
parent 09864eaa9f
commit 80cfa45f51
4 changed files with 17 additions and 4 deletions

View File

@ -487,6 +487,11 @@ namespace base_entity
BaseEntity.Initialization(fsql, () => _asyncUow.Value); BaseEntity.Initialization(fsql, () => _asyncUow.Value);
#endregion #endregion
using (var uow = fsql.CreateUnitOfWork())
{
uow.Orm.Select<User1>().ForUpdate().ToList();
}
var listaaaddd = new List<User1>(); var listaaaddd = new List<User1>();
for (int i = 0; i < 2; i++) for (int i = 0; i < 2; i++)
{ {

View File

@ -70,7 +70,10 @@ namespace FreeSql
{ {
var db = _resolveDbContext?.Invoke(); var db = _resolveDbContext?.Invoke();
db?.FlushCommand(); db?.FlushCommand();
var select = _originalFsql.Select<T1>().WithTransaction(_resolveUnitOfWork?.Invoke()?.GetOrBeginTransaction(false)); var uow = _resolveUnitOfWork?.Invoke();
var uowIsolationLevel = uow?.IsolationLevel ?? IsolationLevel.Unspecified;
var select = _originalFsql.Select<T1>().WithTransaction(uow?.GetOrBeginTransaction(uowIsolationLevel != IsolationLevel.Unspecified));
(select as Select0Provider)._resolveHookTransaction = () => uow?.GetOrBeginTransaction();
if (db?.Options.EnableGlobalFilter == false) select.DisableGlobalFilter(); if (db?.Options.EnableGlobalFilter == false) select.DisableGlobalFilter();
return select; return select;
} }

View File

@ -9,6 +9,7 @@ using System.Linq.Expressions;
using System.Reflection; using System.Reflection;
using System.Threading; using System.Threading;
using FreeSql.Internal.CommonProvider; using FreeSql.Internal.CommonProvider;
using System.Data;
namespace FreeSql namespace FreeSql
{ {
@ -34,7 +35,9 @@ namespace FreeSql
protected virtual ISelect<TEntity> OrmSelect(object dywhere) protected virtual ISelect<TEntity> OrmSelect(object dywhere)
{ {
DbContextFlushCommand(); //查询前先提交,否则会出脏读 DbContextFlushCommand(); //查询前先提交,否则会出脏读
var select = _db.OrmOriginal.Select<TEntity>().AsType(_entityType).WithTransaction(_uow?.GetOrBeginTransaction(false)).TrackToList(TrackToList).WhereDynamic(dywhere); var uowIsolationLevel = _uow?.IsolationLevel ?? IsolationLevel.Unspecified;
var select = _db.OrmOriginal.Select<TEntity>().AsType(_entityType).WithTransaction(_uow?.GetOrBeginTransaction(uowIsolationLevel != IsolationLevel.Unspecified)).TrackToList(TrackToList).WhereDynamic(dywhere);
(select as Select0Provider)._resolveHookTransaction = () => _uow?.GetOrBeginTransaction();
if (_db.Options.EnableGlobalFilter == false) select.DisableGlobalFilter(); if (_db.Options.EnableGlobalFilter == false) select.DisableGlobalFilter();
return select; return select;
} }

View File

@ -47,6 +47,7 @@ namespace FreeSql.Internal.CommonProvider
public Func<bool> _cancel; public Func<bool> _cancel;
public bool _is_AsTreeCte; public bool _is_AsTreeCte;
public BaseDiyMemberExpression _diymemexpWithTempQuery; public BaseDiyMemberExpression _diymemexpWithTempQuery;
public Func<DbTransaction> _resolveHookTransaction;
public bool IsDefaultSqlContent => _distinct == false && _is_AsTreeCte == false && _tables.Count == 1 && _where.Length == 0 && _join.Length == 0 && public bool IsDefaultSqlContent => _distinct == false && _is_AsTreeCte == false && _tables.Count == 1 && _where.Length == 0 && _join.Length == 0 &&
string.IsNullOrWhiteSpace(_orderby) && string.IsNullOrWhiteSpace(_groupby) && string.IsNullOrWhiteSpace(_tosqlAppendContent) && string.IsNullOrWhiteSpace(_orderby) && string.IsNullOrWhiteSpace(_groupby) && string.IsNullOrWhiteSpace(_tosqlAppendContent) &&
@ -1187,8 +1188,9 @@ namespace FreeSql.Internal.CommonProvider
} }
public TSelect ForUpdate(bool noawait = false) public TSelect ForUpdate(bool noawait = false)
{ {
if (_transaction == null && _orm.Ado.TransactionCurrentThread == null) if (_transaction == null && _orm.Ado.TransactionCurrentThread != null) this.WithTransaction(_orm.Ado.TransactionCurrentThread);
throw new Exception($"{CoreStrings.Begin_Transaction_Then_ForUpdate}"); if (_transaction == null && _resolveHookTransaction != null) this.WithTransaction(_resolveHookTransaction());
if (_transaction == null) throw new Exception($"{CoreStrings.Begin_Transaction_Then_ForUpdate}");
switch (_orm.Ado.DataType) switch (_orm.Ado.DataType)
{ {
case DataType.MySql: case DataType.MySql: