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);
        }
Пример #2
0
 public void Commit()
 {
     if (_disposed)
     {
         throw new ObjectDisposedException("Cannot commit a disposed scope.");
     }
     _currentTransaction.Commit(this);
     _currentTransaction = null;
 }
Пример #3
0
 public UnitOfWorkScope(IUnitOfWorkFactory factory, IsolationLevel isolationLevel, UoWScopeOptions scopeOptions)
 {
     _useCompatibleEnabled = scopeOptions.Contains(UoWScopeOptions.UseCompatible);
     _disposed             = false;
     _factory            = factory;
     _transactionOptions = scopeOptions;
     _autoComplete       = (scopeOptions & UoWScopeOptions.AutoComplete) == UoWScopeOptions.AutoComplete;
     _currentTransaction = UnitOfWorkScopeTransaction.GetTransactionForScope(factory, this, isolationLevel, scopeOptions);
     RegisterScope(this);
 }
Пример #4
0
        private void Dispose(bool disposing)
        {
            if (!disposing)
            {
                return;
            }
            if (_disposed)
            {
                return;
            }

            if (_currentTransaction != null)
            {
                if (_autoComplete)
                {
                    #region Try commit
                    try
                    {
                        _currentTransaction.Commit(this);
                    }
                    catch
                    {
                        _currentTransaction.Rollback(this);
                        _currentTransaction = null;
                        UnRegisterScope(this);
                        _disposed = true;
                        throw;
                    }
                    #endregion
                }
                else
                {
                    _currentTransaction.Rollback(this);
                }

                _currentTransaction = null;
            }

            UnRegisterScope(this);

            _disposed = true;
        }