public void BeginTransaction(IsolationLevel?isolationLevel) { if (TransactionCurrentThread != null) { return; } int tid = Thread.CurrentThread.ManagedThreadId; Transaction2 tran = null; Object <DbConnection> conn = null; var before = new Aop.TraceBeforeEventArgs("ThreadTransaction", isolationLevel); _util?._orm?.Aop.TraceBeforeHandler?.Invoke(this, before); try { conn = MasterPool.Get(); tran = new Transaction2(conn, isolationLevel == null ? conn.Value.BeginTransaction() : conn.Value.BeginTransaction(isolationLevel.Value), TimeSpan.FromSeconds(60)); tran.AopBefore = before; } catch (Exception ex) { Trace.WriteLine($"数据库出错(开启事务){ex.Message} \r\n{ex.StackTrace}"); MasterPool.Return(conn); var after = new Aop.TraceAfterEventArgs(before, "", ex); _util?._orm?.Aop.TraceAfterHandler?.Invoke(this, after); throw ex; } if (_trans.ContainsKey(tid)) { CommitTransaction(); } _trans.TryAdd(tid, tran); }
public void Rollback() { var isRollbacked = false; try { if (_tran != null) { _tran.Rollback(); isRollbacked = true; _fsql?.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(_tranBefore, "回滚", null)); } } catch (Exception ex) { if (isRollbacked == false) { _fsql?.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(_tranBefore, "回滚失败", ex)); } throw ex; } finally { ReturnObject(); _tranBefore = null; } }
public int ExecuteAffrows() { var affrows = 0; var ss = SplitSourceByIdentityValueIsNull(_source); try { if (_transaction == null) { var threadTransaction = _orm.Ado.TransactionCurrentThread; if (threadTransaction != null) { this.WithTransaction(threadTransaction); } } if (_transaction != null || _orm.Ado.MasterPool == null) { _source = ss.Item1; _SplitSourceByIdentityValueIsNullFlag = 1; affrows += this.RawExecuteAffrows(); _source = ss.Item2; _SplitSourceByIdentityValueIsNullFlag = 2; affrows += this.RawExecuteAffrows(); } else { using (var conn = _orm.Ado.MasterPool.Get()) { _transaction = conn.Value.BeginTransaction(); var transBefore = new Aop.TraceBeforeEventArgs("BeginTransaction", null); _orm.Aop.TraceBeforeHandler?.Invoke(this, transBefore); try { _source = ss.Item1; _SplitSourceByIdentityValueIsNullFlag = 1; affrows += this.RawExecuteAffrows(); _source = ss.Item2; _SplitSourceByIdentityValueIsNullFlag = 2; affrows += this.RawExecuteAffrows(); _transaction.Commit(); _orm.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(transBefore, "提交", null)); } catch (Exception ex) { _transaction.Rollback(); _orm.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(transBefore, "回滚", ex)); throw ex; } _transaction = null; } } } finally { _SplitSourceByIdentityValueIsNullFlag = 0; ClearData(); } return(affrows); }
public void Commit() { var isCommited = false; try { if (_tran != null) { _tran.Commit(); isCommited = true; _fsql?.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(_tranBefore, "提交", null)); if (EntityChangeReport != null && EntityChangeReport.OnChange != null && EntityChangeReport.Report.Any() == true) { EntityChangeReport.OnChange.Invoke(EntityChangeReport.Report); } } } catch (Exception ex) { if (isCommited == false) { _fsql?.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(_tranBefore, "提交失败", ex)); } throw ex; } finally { ReturnObject(); _tranBefore = null; } }
public void Rollback() { var isRollbacked = false; try { if (_tran != null) { if (_tran.Connection != null) { _tran.Rollback(); } isRollbacked = true; _fsql?.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(_tranBefore, "回滚", null)); } } catch (Exception ex) { if (isRollbacked == false) { _fsql?.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(_tranBefore, "回滚失败", ex)); } #pragma warning disable CA2200 // 再次引发以保留堆栈详细信息 throw ex; #pragma warning restore CA2200 // 再次引发以保留堆栈详细信息 } finally { ReturnObject(); _tranBefore = null; } }
public UnitOfWork(IFreeSql fsql) { _fsql = fsql; if (_fsql == null) { throw new ArgumentNullException(nameof(fsql)); } _uowBefore = new Aop.TraceBeforeEventArgs("UnitOfWork", null); _fsql.Aop.TraceBeforeHandler?.Invoke(this, _uowBefore); }
async protected override Task <long> SplitExecuteIdentityAsync(int valuesLimit, int parameterLimit, CancellationToken cancellationToken = default) { var ss = SplitSource(valuesLimit, parameterLimit); long ret = 0; if (ss.Any() == false) { ClearData(); return(ret); } if (ss.Length == 1) { _batchProgress?.Invoke(new BatchProgressStatus <T1>(_source, 1, 1)); ret = await this.RawExecuteIdentityAsync(cancellationToken); ClearData(); return(ret); } var before = new Aop.TraceBeforeEventArgs("SplitExecuteIdentityAsync", null); _orm.Aop.TraceBeforeHandler?.Invoke(this, before); Exception exception = null; try { for (var a = 0; a < ss.Length; a++) { _source = ss[a]; _batchProgress?.Invoke(new BatchProgressStatus <T1>(_source, a + 1, ss.Length)); if (a < ss.Length - 1) { await this.RawExecuteAffrowsAsync(cancellationToken); } else { ret = await this.RawExecuteIdentityAsync(cancellationToken); } } } catch (Exception ex) { exception = ex; throw; } finally { var after = new Aop.TraceAfterEventArgs(before, null, exception); _orm.Aop.TraceAfterHandler?.Invoke(this, after); } ClearData(); return(ret); }
public UnitOfWork(IFreeSql fsql) { _fsql = fsql; if (_fsql == null) { throw new ArgumentNullException(nameof(fsql)); } _uowBefore = new Aop.TraceBeforeEventArgs("UnitOfWork", null); _fsql?.Aop.TraceBeforeHandler?.Invoke(this, _uowBefore); #if netcoreapp Current.Value = this; #endif }
public DbTransaction GetOrBeginTransaction(bool isCreate = true) { if (_tran != null) { return(_tran); } if (isCreate == false) { return(null); } if (!Enable) { return(null); } if (_conn != null) { _fsql.Ado.MasterPool.Return(_conn); } _tranBefore = new Aop.TraceBeforeEventArgs("BeginTransaction", IsolationLevel); _fsql?.Aop.TraceBeforeHandler?.Invoke(this, _tranBefore); try { _conn = _fsql.Ado.MasterPool.Get(); try { _tran = IsolationLevel == null? _conn.Value.BeginTransaction() : _conn.Value.BeginTransaction(IsolationLevel.Value); this.Id = $"{DateTime.Now.ToString("yyyyMMdd_HHmmss")}_{Interlocked.Increment(ref _seed)}"; DebugBeingUsed.TryAdd(this.Id, this); } catch { ReturnObject(); throw; } } catch (Exception ex) { _fsql?.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(_tranBefore, "失败", ex)); #pragma warning disable CA2200 // 再次引发以保留堆栈详细信息 throw ex; #pragma warning restore CA2200 // 再次引发以保留堆栈详细信息 } return(_tran); }
protected override int SplitExecuteAffrows(int valuesLimit, int parameterLimit) { var ss = SplitSource(valuesLimit, parameterLimit); var ret = 0; if (ss.Any() == false) { ClearData(); return(ret); } if (ss.Length == 1) { _batchProgress?.Invoke(new BatchProgressStatus <T1>(_source, 1, 1)); ret = this.RawExecuteAffrows(); ClearData(); return(ret); } var before = new Aop.TraceBeforeEventArgs("SplitExecuteAffrows", null); _orm.Aop.TraceBeforeHandler?.Invoke(this, before); Exception exception = null; try { for (var a = 0; a < ss.Length; a++) { _source = ss[a]; _batchProgress?.Invoke(new BatchProgressStatus <T1>(_source, a + 1, ss.Length)); ret += this.RawExecuteAffrows(); } } catch (Exception ex) { exception = ex; throw; } finally { var after = new Aop.TraceAfterEventArgs(before, null, exception); _orm.Aop.TraceAfterHandler?.Invoke(this, after); } ClearData(); return(ret); }
async protected override Task <List <T1> > SplitExecuteUpdatedAsync(int valuesLimit, int parameterLimit, CancellationToken cancellationToken = default) { var ss = SplitSource(valuesLimit, parameterLimit); var ret = new List <T1>(); if (ss.Length <= 1) { if (_source?.Any() == true) { _batchProgress?.Invoke(new BatchProgressStatus <T1>(_source, 1, 1)); } ret = await this.RawExecuteUpdatedAsync(cancellationToken); ClearData(); return(ret); } var before = new Aop.TraceBeforeEventArgs("SplitExecuteUpdatedAsync", null); _orm.Aop.TraceBeforeHandler?.Invoke(this, before); Exception exception = null; try { for (var a = 0; a < ss.Length; a++) { _source = ss[a]; _batchProgress?.Invoke(new BatchProgressStatus <T1>(_source, a + 1, ss.Length)); ret.AddRange(await this.RawExecuteUpdatedAsync(cancellationToken)); } } catch (Exception ex) { exception = ex; throw; } finally { var after = new Aop.TraceAfterEventArgs(before, null, exception); _orm.Aop.TraceAfterHandler?.Invoke(this, after); } ClearData(); return(ret); }
public void Commit() { var isCommited = false; try { if (_tran != null) { if (_tran.Connection != null) { _tran.Commit(); } isCommited = true; _fsql?.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(_tranBefore, "提交", null)); if (EntityChangeReport != null && EntityChangeReport.OnChange != null && EntityChangeReport.Report.Any() == true) { EntityChangeReport.OnChange.Invoke(EntityChangeReport.Report); } } } catch (Exception ex) { if (isCommited == false) { _fsql?.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(_tranBefore, "提交失败", ex)); } #pragma warning disable CA2200 // 再次引发以保留堆栈详细信息 throw ex; #pragma warning restore CA2200 // 再次引发以保留堆栈详细信息 } finally { ReturnObject(); _tranBefore = null; } }
protected int SplitExecuteAffrows(int valuesLimit, int parameterLimit) { var ss = SplitSource(valuesLimit, parameterLimit); var ret = 0; if (ss.Length <= 1) { if (_source?.Any() == true) { _batchProgress?.Invoke(new BatchProgressStatus <T1>(_source, 1, 1)); } ret = this.RawExecuteAffrows(); ClearData(); return(ret); } if (_transaction == null) { var threadTransaction = _orm.Ado.TransactionCurrentThread; if (threadTransaction != null) { this.WithTransaction(threadTransaction); } } var before = new Aop.TraceBeforeEventArgs("SplitExecuteAffrows", null); _orm.Aop.TraceBeforeHandler?.Invoke(this, before); Exception exception = null; try { if (_transaction != null || _batchAutoTransaction == false) { for (var a = 0; a < ss.Length; a++) { _source = ss[a]; _batchProgress?.Invoke(new BatchProgressStatus <T1>(_source, a + 1, ss.Length)); ret += this.RawExecuteAffrows(); } } else { if (_orm.Ado.MasterPool == null) { throw new Exception("Ado.MasterPool 值为 null,该操作无法自启用事务,请显式传递【事务对象】解决"); } using (var conn = _orm.Ado.MasterPool.Get()) { _transaction = conn.Value.BeginTransaction(); var transBefore = new Aop.TraceBeforeEventArgs("BeginTransaction", null); _orm.Aop.TraceBeforeHandler?.Invoke(this, transBefore); try { for (var a = 0; a < ss.Length; a++) { _source = ss[a]; _batchProgress?.Invoke(new BatchProgressStatus <T1>(_source, a + 1, ss.Length)); ret += this.RawExecuteAffrows(); } _transaction.Commit(); _orm.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(transBefore, "提交", null)); } catch (Exception ex) { _transaction.Rollback(); _orm.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(transBefore, "回滚", ex)); throw; } _transaction = null; } } } catch (Exception ex) { exception = ex; throw; } finally { var after = new Aop.TraceAfterEventArgs(before, null, exception); _orm.Aop.TraceAfterHandler?.Invoke(this, after); } ClearData(); return(ret); }
async protected virtual Task <int> SplitExecuteAffrowsAsync(int valuesLimit, int parameterLimit, CancellationToken cancellationToken = default) { var ss = SplitSource(valuesLimit, parameterLimit); var ret = 0; if (ss.Length <= 1) { if (_source?.Any() == true) { _batchProgress?.Invoke(new BatchProgressStatus <T1>(_source, 1, 1)); } ret = await this.RawExecuteAffrowsAsync(cancellationToken); ClearData(); return(ret); } if (_transaction == null) { var threadTransaction = _orm.Ado.TransactionCurrentThread; if (threadTransaction != null) { this.WithTransaction(threadTransaction); } } var before = new Aop.TraceBeforeEventArgs("SplitExecuteAffrowsAsync", null); _orm.Aop.TraceBeforeHandler?.Invoke(this, before); Exception exception = null; try { if (_transaction != null || _batchAutoTransaction == false) { for (var a = 0; a < ss.Length; a++) { _source = ss[a]; _batchProgress?.Invoke(new BatchProgressStatus <T1>(_source, a + 1, ss.Length)); ret += await this.RawExecuteAffrowsAsync(cancellationToken); } } else { if (_orm.Ado.MasterPool == null) { throw new Exception(CoreStrings.MasterPool_IsNull_UseTransaction); } using (var conn = await _orm.Ado.MasterPool.GetAsync()) { _transaction = conn.Value.BeginTransaction(); var transBefore = new Aop.TraceBeforeEventArgs("BeginTransaction", null); _orm.Aop.TraceBeforeHandler?.Invoke(this, transBefore); try { for (var a = 0; a < ss.Length; a++) { _source = ss[a]; _batchProgress?.Invoke(new BatchProgressStatus <T1>(_source, a + 1, ss.Length)); ret += await this.RawExecuteAffrowsAsync(cancellationToken); } _transaction.Commit(); _orm.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(transBefore, CoreStrings.Commit, null)); } catch (Exception ex) { _transaction.Rollback(); _orm.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(transBefore, CoreStrings.RollBack, ex)); throw; } _transaction = null; } } } catch (Exception ex) { exception = ex; throw; } finally { var after = new Aop.TraceAfterEventArgs(before, null, exception); _orm.Aop.TraceAfterHandler?.Invoke(this, after); } ClearData(); return(ret); }
async protected Task <List <T1> > SplitExecuteUpdatedAsync(int valuesLimit, int parameterLimit) { var ss = SplitSource(valuesLimit, parameterLimit); var ret = new List <T1>(); if (ss.Length <= 1) { if (_source?.Any() == true) { _batchProgress?.Invoke(new BatchProgressStatus <T1>(_source, 1, 1)); } ret = await this.RawExecuteUpdatedAsync(); ClearData(); return(ret); } if (_transaction == null) { this.WithTransaction(_orm.Ado.TransactionCurrentThread); } var before = new Aop.TraceBeforeEventArgs("SplitExecuteUpdatedAsync", null); _orm.Aop.TraceBeforeHandler?.Invoke(this, before); Exception exception = null; try { if (_transaction != null || _batchAutoTransaction == false) { for (var a = 0; a < ss.Length; a++) { _source = ss[a]; _batchProgress?.Invoke(new BatchProgressStatus <T1>(_source, a + 1, ss.Length)); ret.AddRange(await this.RawExecuteUpdatedAsync()); } } else { using (var conn = await _orm.Ado.MasterPool.GetAsync()) { _transaction = conn.Value.BeginTransaction(); var transBefore = new Aop.TraceBeforeEventArgs("BeginTransaction", null); _orm.Aop.TraceBeforeHandler?.Invoke(this, transBefore); try { for (var a = 0; a < ss.Length; a++) { _source = ss[a]; _batchProgress?.Invoke(new BatchProgressStatus <T1>(_source, a + 1, ss.Length)); ret.AddRange(await this.RawExecuteUpdatedAsync()); } _transaction.Commit(); _orm.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(transBefore, "提交", null)); } catch (Exception ex) { _transaction.Rollback(); _orm.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(transBefore, "回滚", ex)); throw ex; } _transaction = null; } } } catch (Exception ex) { exception = ex; throw ex; } finally { var after = new Aop.TraceAfterEventArgs(before, null, exception); _orm.Aop.TraceAfterHandler?.Invoke(this, after); } ClearData(); return(ret); }
protected List <T1> SplitExecuteInserted(int valuesLimit, int parameterLimit) { var ss = SplitSource(valuesLimit, parameterLimit); var ret = new List <T1>(); if (ss.Any() == false) { ClearData(); return(ret); } if (ss.Length == 1) { ret = this.RawExecuteInserted(); ClearData(); return(ret); } if (_transaction == null) { this.WithTransaction(_orm.Ado.TransactionCurrentThread); } var before = new Aop.TraceBeforeEventArgs("SplitExecuteInserted", null); _orm.Aop.TraceBeforeHandler?.Invoke(this, before); Exception exception = null; try { if (_transaction != null || _batchAutoTransaction == false) { for (var a = 0; a < ss.Length; a++) { _source = ss[a]; ret.AddRange(this.RawExecuteInserted()); } } else { using (var conn = _orm.Ado.MasterPool.Get()) { _transaction = conn.Value.BeginTransaction(); var transBefore = new Aop.TraceBeforeEventArgs("BeginTransaction", null); _orm.Aop.TraceBeforeHandler?.Invoke(this, transBefore); try { for (var a = 0; a < ss.Length; a++) { _source = ss[a]; ret.AddRange(this.RawExecuteInserted()); } _transaction.Commit(); _orm.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(transBefore, "提交", null)); } catch (Exception ex) { _transaction.Rollback(); _orm.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(transBefore, "回滚", ex)); throw ex; } _transaction = null; } } } catch (Exception ex) { exception = ex; throw ex; } finally { var after = new Aop.TraceAfterEventArgs(before, null, exception); _orm.Aop.TraceAfterHandler?.Invoke(this, after); } ClearData(); return(ret); }
async public Task <int> ExecuteAffrowsAsync(CancellationToken cancellationToken = default) { var affrows = 0; var ss = SplitSourceByIdentityValueIsNull(_source); try { if (_transaction == null) { var threadTransaction = _orm.Ado.TransactionCurrentThread; if (threadTransaction != null) { this.WithTransaction(threadTransaction); } } if (_transaction != null || _orm.Ado.MasterPool == null) { _SplitSourceByIdentityValueIsNullFlag = 1; foreach (var tmpsource in ss.Item1) { _source = tmpsource; affrows += await this.RawExecuteAffrowsAsync(cancellationToken); } _SplitSourceByIdentityValueIsNullFlag = 2; foreach (var tmpsource in ss.Item2) { _source = tmpsource; affrows += await this.RawExecuteAffrowsAsync(cancellationToken); } } else { using (var conn = await _orm.Ado.MasterPool.GetAsync()) { _transaction = conn.Value.BeginTransaction(); var transBefore = new Aop.TraceBeforeEventArgs("BeginTransaction", null); _orm.Aop.TraceBeforeHandler?.Invoke(this, transBefore); try { _SplitSourceByIdentityValueIsNullFlag = 1; foreach (var tmpsource in ss.Item1) { _source = tmpsource; affrows += await this.RawExecuteAffrowsAsync(cancellationToken); } _SplitSourceByIdentityValueIsNullFlag = 2; foreach (var tmpsource in ss.Item2) { _source = tmpsource; affrows += await this.RawExecuteAffrowsAsync(cancellationToken); } _transaction.Commit(); _orm.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(transBefore, CoreStrings.Commit, null)); } catch (Exception ex) { _transaction.Rollback(); _orm.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(transBefore, CoreStrings.RollBack, ex)); throw; } _transaction = null; } } } finally { _SplitSourceByIdentityValueIsNullFlag = 0; ClearData(); } return(affrows); }