Exemplo n.º 1
0
        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 BeginTransaction(TimeSpan timeout)
        {
            if (TransactionCurrentThread != null)
            {
                return;
            }

            int                   tid  = Thread.CurrentThread.ManagedThreadId;
            Transaction2          tran = null;
            Object <DbConnection> conn = null;

            try {
                conn = MasterPool.Get();
                tran = new Transaction2(conn, conn.Value.BeginTransaction(), timeout);
            } catch (Exception ex) {
                _log.LogError($"数据库出错(开启事务){ex.Message} \r\n{ex.StackTrace}");
                MasterPool.Return(conn);
                throw ex;
            }
            if (_trans.ContainsKey(tid))
            {
                CommitTransaction();
            }

            lock (_trans_lock)
                _trans.Add(tid, tran);
        }