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); }
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); }
private void CommitTransaction(bool isCommit, Transaction2 tran, Exception rollbackException, string remark = null) { if (tran == null || tran.Transaction == null || tran.Transaction.Connection == null) { return; } if (_trans.ContainsKey(tran.Conn.LastGetThreadId)) { lock (_trans_lock) if (_trans.ContainsKey(tran.Conn.LastGetThreadId)) { _trans.TryRemove(tran.Conn.LastGetThreadId, out var oldtran); } } Exception ex = null; if (string.IsNullOrEmpty(remark)) { remark = isCommit ? "提交" : "回滚"; } try { Trace.WriteLine($"线程{tran.Conn.LastGetThreadId}事务{remark}"); if (isCommit) { tran.Transaction.Commit(); } else { tran.Transaction.Rollback(); } } catch (Exception ex2) { ex = ex2; Trace.WriteLine($"数据库出错({remark}事务):{ex.Message} {ex.StackTrace}"); } finally { ReturnConnection(MasterPool, tran.Conn, ex); //MasterPool.Return(tran.Conn, ex); var after = new Aop.TraceAfterEventArgs(tran.AopBefore, remark, ex ?? rollbackException); _util?._orm?.Aop.TraceAfterHandler?.Invoke(this, after); } }
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); }
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); }
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 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 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); }