Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new transaction scope associated with the specified connection,
        /// creating the inner scope using the specified factory method.
        /// The scope can be nested inside another scope in which case the underlying
        /// db transaction is only committed once both the outer and inner transaction(s)
        /// have been committed. The returned ITransactionScope implements IDisposable
        /// and should be wrapped in a using statement.
        /// </summary>
        /// <param name="dbConnection">
        /// <para>
        /// The DbConnection instance to manage transactions for. Transaction scopes
        /// created by this instance only apply to a single DbConnection, so if you want
        /// the scope to span additional data access mechanism then they must share the
        /// same connection.
        /// </para>
        /// <para>
        /// You can use the ICofoundryDbConnectionManager to get a reference to the shared
        /// connection directly.
        /// </para>
        /// </param>
        /// <returns>ITransactionScope, which is IDisposable and must be disposed.</returns>
        public ITransactionScope Create(DbConnection dbConnection, Func <System.Transactions.TransactionScope> transactionScopeFactory)
        {
            if (dbConnection == null)
            {
                throw new ArgumentNullException(nameof(dbConnection));
            }
            if (transactionScopeFactory == null)
            {
                throw new ArgumentNullException(nameof(transactionScopeFactory));
            }

            ITransactionScope scope;
            var connectionHash = dbConnection.GetHashCode();
            var primaryScope   = _primaryTransactionScopes.GetOrDefault(connectionHash);

            if (primaryScope == null)
            {
                primaryScope = new PrimaryTransactionScope(this, transactionScopeFactory);
                _primaryTransactionScopes.Add(connectionHash, primaryScope);
                scope = primaryScope;
            }
            else
            {
                scope = new ChildTransactionScope(primaryScope, transactionScopeFactory);
            }

            return(scope);
        }
Ejemplo n.º 2
0
 public ChildTransactionScope(
     PrimaryTransactionScope primaryTransactionScope
     )
 {
     _primaryTransactionScope = primaryTransactionScope;
     _innerScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);
 }
Ejemplo n.º 3
0
 public ChildTransactionScope(
     PrimaryTransactionScope primaryTransactionScope,
     Func <System.Transactions.TransactionScope> transactionScopeFactory
     )
 {
     _primaryTransactionScope = primaryTransactionScope;
     _innerScope = transactionScopeFactory();
 }
Ejemplo n.º 4
0
        internal void DeregisterTransaction(PrimaryTransactionScope scope)
        {
            if (scope == null)
            {
                throw new ArgumentNullException(nameof(scope));
            }

            var scopeToRemoveKey = _primaryTransactionScopes
                                   .Where(s => s.Value == scope)
                                   .Select(s => (int?)s.Key)
                                   .SingleOrDefault();

            if (scopeToRemoveKey.HasValue)
            {
                _primaryTransactionScopes.Remove(scopeToRemoveKey.Value);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates a new transaction scope associated with the specified connection.
        /// The scope can be nested inside another scope in which case the underlying
        /// db transaction is only committed once both the outer and inner transaction(s)
        /// have been committed. The returned ITransactionScope implements IDisposable
        /// and should be wrapped in a using statement.
        /// </summary>
        /// <param name="dbConnection">
        /// <para>
        /// The DbConnection instance to manage transactions for. Transaction scopes
        /// created by this instance only apply to a single DbConnection, so if you want
        /// the scope to span additional data access mechanism then they must share the
        /// same connection.
        /// </para>
        /// <para>
        /// You can use the ICofoundryDbConnectionManager to get a reference to the shared
        /// connection directly.
        /// </para>
        /// </param>
        /// <returns>ITransactionScope, which is IDisposable and must be disposed.</returns>
        public ITransactionScope Create(DbConnection dbConnection)
        {
            ITransactionScope scope;
            var connectionHash = dbConnection.GetHashCode();
            var primaryScope   = _primaryTransactionScopes.GetOrDefault(connectionHash);

            if (primaryScope == null)
            {
                primaryScope = new PrimaryTransactionScope(this);
                _primaryTransactionScopes.Add(connectionHash, primaryScope);
                scope = primaryScope;
            }
            else
            {
                scope = new ChildTransactionScope(primaryScope);
            }

            return(scope);
        }