public void Rollback(UnitOfWorkScope scope)
        {
            #region Verify

            if (_disposed)
            {
                throw new ObjectDisposedException("Cannot rollback a disposed transaction.");
            }

            if (_attachedScopes.Peek() != scope)
            {
                throw new InvalidOperationException("Rollback can only be called by the current UnitOfWorkScope instance.");
            }

            #endregion

            _attachedScopes.Pop();

            //если у текущей транзакции не осталось прикреплённых транзакционных областей - осовободим её
            //иначе не мешаем внешним областям делать откат/подтверждение при наличии опции UseCompatible
            if (_attachedScopes.Count == 0)
            {
                _runningTransaction.Rollback();
                _runningTransaction.Dispose();
                _unitOfWork.Dispose();
                CurrentTransactions.Remove(this);
            }
            else
            if (!scope.UseCompatibleEnabled)
            {
                _transactionRolledback = true;
            }
        }
        private void AttachScope(UnitOfWorkScope scope)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("Transaction is disposed.");
            }

            _attachedScopes.Push(scope);
        }
Example #3
0
        private void UnRegisterScope(UnitOfWorkScope scope)
        {
            if (RunningScopes.Peek() != scope)
            {
                throw new InvalidOperationException("Cannot un-register scope.");
            }

            RunningScopes.Pop();

            if (RunningScopes.Count > 0)
            {
                UnitOfWorkScope currentScope = RunningScopes.Peek();
                UnitOfWork.Current = currentScope.RelUnitOfWork;
            }
            else
            {
                UnitOfWork.Current = null;
            }
        }
        public void Commit(UnitOfWorkScope scope)
        {
            #region Verify

            if (_disposed)
            {
                throw new ObjectDisposedException("Transaction disposed.");
            }
            if (_transactionRolledback)
            {
                throw new InvalidOperationException("A child scope or current scope has already rolled back the transaction.");
            }

            if (_attachedScopes.Peek() != scope)
            {
                throw new InvalidOperationException("Commit can only be called by the current UnitOfWorkScope instance.");
            }

            #endregion

            var currentScope = _attachedScopes.Pop();
            if (_attachedScopes.Count != 0)
            {
                return;
            }

            try
            {
                _unitOfWork.Flush();
                _runningTransaction.Commit();
                _runningTransaction.Dispose();
                _unitOfWork.Dispose();
                CurrentTransactions.Remove(this);
            }
            catch (Exception)
            {
                _attachedScopes.Push(currentScope);
                throw;
            }
        }
        public static UnitOfWorkScopeTransaction GetTransactionForScope(IUnitOfWorkFactory factory, UnitOfWorkScope scope, IsolationLevel isolationLevel, UoWScopeOptions options)
        {
            var useCompatibleTx = (options & UoWScopeOptions.UseCompatible) == UoWScopeOptions.UseCompatible;
            var createNewTx     = (options & UoWScopeOptions.CreateNew) == UoWScopeOptions.CreateNew;

            //вот нельзя одновременно создавать новую транзакцию и использовать существующую
            if (useCompatibleTx && createNewTx)
            {
                throw new InvalidOperationException("Несовместимые опции запуска транзакции");
            }

            if (options == UoWScopeOptions.UseCompatible)
            {
                var transaction = (from t in CurrentTransactions where t.IsolationLevel == isolationLevel select t).FirstOrDefault();
                if (transaction != null)
                {
                    transaction.AttachScope(scope);
                    return(transaction);
                }
            }

            var newTransaction = new UnitOfWorkScopeTransaction(factory, isolationLevel);

            newTransaction.AttachScope(scope);
            CurrentTransactions.AddFirst(newTransaction);
            return(newTransaction);
        }
 public static UnitOfWorkScopeTransaction GetTransactionForScope(IUnitOfWorkFactory factory, UnitOfWorkScope scope, IsolationLevel isolationLevel)
 {
     return(GetTransactionForScope(factory, scope, isolationLevel, UoWScopeOptions.UseCompatible));
 }
Example #7
0
 private void RegisterScope(UnitOfWorkScope scope)
 {
     UnitOfWork.Current = scope.RelUnitOfWork;
     RunningScopes.Push(scope);
 }