public void Cloning_the_interception_context_preserves_contextual_information_but_not_mutable_state()
            {
                var objectContext  = new ObjectContext();
                var dbContext      = DbContextMockHelper.CreateDbContext(objectContext);
                var mockConnection = new Mock <DbConnection>().Object;

                var interceptionContext = new DbTransactionInterceptionContext();

                var mutableData = ((IDbMutableInterceptionContext)interceptionContext).MutableData;

                mutableData.SetExceptionThrown(new Exception("Cheez Whiz"));
                mutableData.UserState = "Caerphilly";

                interceptionContext = interceptionContext
                                      .WithDbContext(dbContext)
                                      .WithObjectContext(objectContext)
                                      .WithConnection(mockConnection)
                                      .AsAsync();

                Assert.Equal(new[] { objectContext }, interceptionContext.ObjectContexts);
                Assert.Equal(new[] { dbContext }, interceptionContext.DbContexts);
                Assert.Same(mockConnection, interceptionContext.Connection);
                Assert.True(interceptionContext.IsAsync);

                Assert.Null(interceptionContext.Exception);
                Assert.Null(interceptionContext.OriginalException);
                Assert.False(interceptionContext.IsExecutionSuppressed);
                Assert.Null(interceptionContext.UserState);
            }
コード例 #2
0
            public void Initially_has_no_state(bool useObsoleteState)
            {
                var interceptionContext = new DbTransactionInterceptionContext <int>();

                Assert.Empty(interceptionContext.DbContexts);
                Assert.Null(interceptionContext.Exception);
                Assert.False(interceptionContext.IsAsync);
                Assert.False(interceptionContext.IsExecutionSuppressed);
                Assert.Empty(interceptionContext.ObjectContexts);
                Assert.Null(interceptionContext.OriginalException);
                Assert.Equal(0, interceptionContext.OriginalResult);
                Assert.Equal(0, interceptionContext.Result);
                Assert.Equal((TaskStatus)0, interceptionContext.TaskStatus);
                if (useObsoleteState)
                {
#pragma warning disable 618
                    Assert.Null(interceptionContext.UserState);
#pragma warning restore 618
                }
                else
                {
                    Assert.Null(interceptionContext.FindUserState("A"));
                    Assert.Null(interceptionContext.FindUserState("B"));
                }
            }
            public void Cloning_the_interception_context_preserves_contextual_information_but_not_mutable_state()
            {
                var objectContext = new ObjectContext();
                var dbContext     = DbContextMockHelper.CreateDbContext(objectContext);

                var interceptionContext = new DbTransactionInterceptionContext <int>();

                interceptionContext.Exception = new Exception("Cheez Whiz");
                interceptionContext.Result    = 23;
                interceptionContext.UserState = "Norwegian Jarlsberg";

                interceptionContext = interceptionContext
                                      .WithDbContext(dbContext)
                                      .WithObjectContext(objectContext)
                                      .AsAsync();

                Assert.Equal(new[] { objectContext }, interceptionContext.ObjectContexts);
                Assert.Equal(new[] { dbContext }, interceptionContext.DbContexts);
                Assert.True(interceptionContext.IsAsync);

                Assert.Equal(0, interceptionContext.Result);
                Assert.Equal(0, interceptionContext.OriginalResult);
                Assert.Null(interceptionContext.Exception);
                Assert.Null(interceptionContext.OriginalException);
                Assert.False(interceptionContext.IsExecutionSuppressed);
                Assert.Null(interceptionContext.UserState);
            }
            public void Cloning_the_interception_context_preserves_contextual_information_but_not_mutable_state()
            {
                var objectContext = new ObjectContext();
                var dbContext = DbContextMockHelper.CreateDbContext(objectContext);

                var interceptionContext = new DbTransactionInterceptionContext<int>();
                interceptionContext.Exception = new Exception("Cheez Whiz");
                interceptionContext.Result = 23;
                interceptionContext.UserState = "Norwegian Jarlsberg";

                interceptionContext = interceptionContext
                    .WithDbContext(dbContext)
                    .WithObjectContext(objectContext)
                    .AsAsync();

                Assert.Equal(new[] { objectContext }, interceptionContext.ObjectContexts);
                Assert.Equal(new[] { dbContext }, interceptionContext.DbContexts);
                Assert.True(interceptionContext.IsAsync);

                Assert.Equal(0, interceptionContext.Result);
                Assert.Equal(0, interceptionContext.OriginalResult);
                Assert.Null(interceptionContext.Exception);
                Assert.Null(interceptionContext.OriginalException);
                Assert.False(interceptionContext.IsExecutionSuppressed);
                Assert.Null(interceptionContext.UserState);
            }
コード例 #5
0
 public override void Committed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext)
 {
     if (interceptionContext.Exception != null
         && (interceptionContext.Connection != null && MatchesParentContext(interceptionContext.Connection, interceptionContext)))
     {
         interceptionContext.Exception = new CommitFailedException(Strings.CommitFailed, interceptionContext.Exception);
     }
 }
コード例 #6
0
        /// <summary>
        /// Creates a new <see cref="T:System.Data.Entity.Infrastructure.Interception.DbTransactionInterceptionContext" /> that contains all the contextual information in this
        /// interception context with the addition of the given <see cref="T:System.Data.Common.DbConnection" />.
        /// </summary>
        /// <param name="connection">The connection on which the transaction was started.</param>
        /// <returns>A new interception context that also contains the connection on which the transaction was started.</returns>
        public DbTransactionInterceptionContext WithConnection(
            DbConnection connection)
        {
            Check.NotNull <DbConnection>(connection, nameof(connection));
            DbTransactionInterceptionContext interceptionContext = this.TypedClone();

            interceptionContext._connection = connection;
            return(interceptionContext);
        }
コード例 #7
0
        public void Committed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext)
        {
            var entitySets = RemoveAffectedEntitySets(transaction);

            if (entitySets != null)
            {
                _cache.InvalidateSets(entitySets.Distinct());
            }
        }
コード例 #8
0
            public void RolledBack_logs_exceptions()
            {
                var writer = new StringWriter();
                var interceptionContext = new DbTransactionInterceptionContext();

                interceptionContext.Exception = new Exception("Boo");
                new DatabaseLogFormatter(writer.Write).RolledBack(new Mock <DbTransaction>().Object, interceptionContext);
                Assert.True(
                    _resourceVerifier.IsMatch("TransactionRollbackErrorLog", GetSingleLine(writer), new AnyValueParameter(), "Boo", ""));
            }
コード例 #9
0
        /// <summary>
        /// Creates a new <see cref="T:System.Data.Entity.Infrastructure.Interception.DbTransactionInterceptionContext" /> by copying immutable state from the given
        /// interception context. Also see <see cref="M:System.Data.Entity.Infrastructure.Interception.DbTransactionInterceptionContext.Clone" />
        /// </summary>
        /// <param name="copyFrom">The context from which to copy state.</param>
        public DbTransactionInterceptionContext(DbInterceptionContext copyFrom)
            : base(copyFrom)
        {
            DbTransactionInterceptionContext interceptionContext = copyFrom as DbTransactionInterceptionContext;

            if (interceptionContext != null)
            {
                this._connection = interceptionContext.Connection;
            }
            Check.NotNull <DbInterceptionContext>(copyFrom, nameof(copyFrom));
        }
コード例 #10
0
 /// <summary>
 /// This method is called before <see cref="M:System.Data.Common.DbTransaction.Dispose" /> is invoked.
 /// The default implementation of this method filters by <see cref="T:System.Data.Entity.DbContext" /> set into
 /// <see cref="P:System.Data.Entity.Infrastructure.Interception.DatabaseLogFormatter.Context" />, if any, and then logs the event.
 /// </summary>
 /// <param name="transaction">The transaction being disposed.</param>
 /// <param name="interceptionContext">Contextual information associated with the call.</param>
 public virtual void Disposing(
     DbTransaction transaction,
     DbTransactionInterceptionContext interceptionContext)
 {
     Check.NotNull <DbTransaction>(transaction, nameof(transaction));
     Check.NotNull <DbTransactionInterceptionContext>(interceptionContext, nameof(interceptionContext));
     if (this.Context != null && !interceptionContext.DbContexts.Contains <DbContext>(this.Context, new Func <DbContext, DbContext, bool>(object.ReferenceEquals)) || transaction.Connection == null)
     {
         return;
     }
     this.Write(Strings.TransactionDisposedLog((object)DateTimeOffset.Now, (object)Environment.NewLine));
 }
コード例 #11
0
        /// <summary>
        /// This method is called before <see cref="DbTransaction.Dispose()" /> is invoked.
        /// The default implementation of this method filters by <see cref="DbContext" /> set into
        /// <see cref="Context" />, if any, and then logs the event.
        /// </summary>
        /// <param name="transaction">The transaction being disposed.</param>
        /// <param name="interceptionContext">Contextual information associated with the call.</param>
        public virtual void Disposing(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext)
        {
            Check.NotNull(transaction, "transaction");
            Check.NotNull(interceptionContext, "interceptionContext");

            if ((Context == null ||
                 interceptionContext.DbContexts.Contains(Context, ReferenceEquals)) &&
                transaction.Connection != null)
            {
                Write(Strings.TransactionDisposedLog(DateTimeOffset.Now, Environment.NewLine));
            }
        }
コード例 #12
0
        public void Committed_does_not_wrap_exception_if_unknown_connection()
        {
            var context = Core.Objects.MockHelper.CreateMockObjectContext<object>();
            var handler = new DefaultTransactionHandler();
            handler.Initialize(context);
            var mockTransaction = new Mock<DbTransaction>().Object;

            var interceptionContext = new DbTransactionInterceptionContext().WithConnection(new Mock<DbConnection>().Object);
            interceptionContext.Exception = new Exception();
            handler.Committed(mockTransaction, interceptionContext);

            Assert.IsNotType<CommitFailedException>(interceptionContext.Exception);
        }
        /// <summary>
        /// Sends <see cref="IDbTransactionInterceptor.Committing" /> and
        /// <see cref="IDbTransactionInterceptor.Committed" /> to any <see cref="IDbConnectionInterceptor" />
        /// registered on <see cref="DbInterception" /> before/after making a
        /// call to <see cref="DbTransaction.Commit" />.
        /// </summary>
        /// <param name="transaction">The transaction on which the operation will be executed.</param>
        /// <param name="interceptionContext">Optional information about the context of the call being made.</param>
        public virtual void Commit(DbTransaction transaction, DbInterceptionContext interceptionContext)
        {
            Check.NotNull(transaction, "transaction");
            Check.NotNull(interceptionContext, "interceptionContext");

            var clonedInterceptionContext = new DbTransactionInterceptionContext(interceptionContext);

            InternalDispatcher.Dispatch(
                transaction.Commit,
                clonedInterceptionContext,
                i => i.Committing(transaction, clonedInterceptionContext),
                i => i.Committed(transaction, clonedInterceptionContext));
        }
        /// <summary>
        /// Sends <see cref="IDbTransactionInterceptor.IsolationLevelGetting" /> and
        /// <see cref="IDbTransactionInterceptor.IsolationLevelGot" /> to any <see cref="IDbTransactionInterceptor" />
        /// registered on <see cref="DbInterception" /> before/after
        /// getting <see cref="DbTransaction.IsolationLevel" />.
        /// </summary>
        /// <remarks>
        /// Note that the value of the property is returned by this method. The result is not available
        /// in the interception context passed into this method since the interception context is cloned before
        /// being passed to interceptors.
        /// </remarks>
        /// <param name="transaction">The transaction on which the operation will be executed.</param>
        /// <param name="interceptionContext">Optional information about the context of the call being made.</param>
        /// <returns>The result of the operation, which may have been modified by interceptors.</returns>
        public virtual IsolationLevel GetIsolationLevel(DbTransaction transaction, DbInterceptionContext interceptionContext)
        {
            Check.NotNull(transaction, "transaction");
            Check.NotNull(interceptionContext, "interceptionContext");

            var clonedInterceptionContext = new DbTransactionInterceptionContext <IsolationLevel>(interceptionContext);

            return(InternalDispatcher.Dispatch(
                       () => transaction.IsolationLevel,
                       clonedInterceptionContext,
                       i => i.IsolationLevelGetting(transaction, clonedInterceptionContext),
                       i => i.IsolationLevelGot(transaction, clonedInterceptionContext)));
        }
        /// <summary>
        /// Sends <see cref="IDbTransactionInterceptor.ConnectionGetting" /> and
        /// <see cref="IDbTransactionInterceptor.ConnectionGot" /> to any <see cref="IDbTransactionInterceptor" />
        /// registered on <see cref="DbInterception" /> before/after
        /// getting <see cref="DbTransaction.Connection" />.
        /// </summary>
        /// <remarks>
        /// Note that the value of the property is returned by this method. The result is not available
        /// in the interception context passed into this method since the interception context is cloned before
        /// being passed to interceptors.
        /// </remarks>
        /// <param name="transaction">The transaction on which the operation will be executed.</param>
        /// <param name="interceptionContext">Optional information about the context of the call being made.</param>
        /// <returns>The result of the operation, which may have been modified by interceptors.</returns>
        public virtual DbConnection GetConnection(DbTransaction transaction, DbInterceptionContext interceptionContext)
        {
            Check.NotNull(transaction, "transaction");
            Check.NotNull(interceptionContext, "interceptionContext");

            var clonedInterceptionContext = new DbTransactionInterceptionContext <DbConnection>(interceptionContext);

            return(InternalDispatcher.Dispatch(
                       () => transaction.Connection,
                       clonedInterceptionContext,
                       i => i.ConnectionGetting(transaction, clonedInterceptionContext),
                       i => i.ConnectionGot(transaction, clonedInterceptionContext)));
        }
        /// <summary>
        /// Sends <see cref="IDbTransactionInterceptor.RollingBack" /> and
        /// <see cref="IDbTransactionInterceptor.RolledBack" /> to any <see cref="IDbConnectionInterceptor" />
        /// registered on <see cref="DbInterception" /> before/after making a
        /// call to <see cref="DbTransaction.Rollback" />.
        /// </summary>
        /// <param name="transaction">The transaction on which the operation will be executed.</param>
        /// <param name="interceptionContext">Optional information about the context of the call being made.</param>
        public virtual void Rollback(DbTransaction transaction, DbInterceptionContext interceptionContext)
        {
            Check.NotNull(transaction, "transaction");
            Check.NotNull(interceptionContext, "interceptionContext");

            var clonedInterceptionContext = new DbTransactionInterceptionContext(interceptionContext);

            InternalDispatcher.Dispatch(
                transaction.Rollback,
                clonedInterceptionContext,
                i => i.RollingBack(transaction, clonedInterceptionContext),
                i => i.RolledBack(transaction, clonedInterceptionContext));
        }
コード例 #17
0
        /// <summary>
        /// Sends <see cref="M:System.Data.Entity.Infrastructure.Interception.IDbTransactionInterceptor.Disposing(System.Data.Common.DbTransaction,System.Data.Entity.Infrastructure.Interception.DbTransactionInterceptionContext)" /> and
        /// <see cref="M:System.Data.Entity.Infrastructure.Interception.IDbTransactionInterceptor.Disposed(System.Data.Common.DbTransaction,System.Data.Entity.Infrastructure.Interception.DbTransactionInterceptionContext)" /> to any <see cref="T:System.Data.Entity.Infrastructure.Interception.IDbConnectionInterceptor" />
        /// registered on <see cref="T:System.Data.Entity.Infrastructure.Interception.DbInterception" /> before/after making a
        /// call to <see cref="M:System.Data.Common.DbTransaction.Dispose" />.
        /// </summary>
        /// <param name="transaction">The transaction on which the operation will be executed.</param>
        /// <param name="interceptionContext">Optional information about the context of the call being made.</param>
        public virtual void Dispose(
            DbTransaction transaction,
            DbInterceptionContext interceptionContext)
        {
            Check.NotNull <DbTransaction>(transaction, nameof(transaction));
            Check.NotNull <DbInterceptionContext>(interceptionContext, nameof(interceptionContext));
            DbTransactionInterceptionContext interceptionContext1 = new DbTransactionInterceptionContext(interceptionContext);

            if (transaction.Connection != null)
            {
                interceptionContext1 = interceptionContext1.WithConnection(transaction.Connection);
            }
            this.InternalDispatcher.Dispatch <DbTransaction, DbTransactionInterceptionContext>(transaction, (Action <DbTransaction, DbTransactionInterceptionContext>)((t, c) => t.Dispose()), interceptionContext1, (Action <IDbTransactionInterceptor, DbTransaction, DbTransactionInterceptionContext>)((i, t, c) => i.Disposing(t, c)), (Action <IDbTransactionInterceptor, DbTransaction, DbTransactionInterceptionContext>)((i, t, c) => i.Disposed(t, c)));
        }
            public void Initially_has_no_state()
            {
                var interceptionContext = new DbTransactionInterceptionContext();

                Assert.Empty(interceptionContext.DbContexts);
                Assert.Null(interceptionContext.Exception);
                Assert.Null(interceptionContext.Connection);
                Assert.False(interceptionContext.IsAsync);
                Assert.False(interceptionContext.IsExecutionSuppressed);
                Assert.Empty(interceptionContext.ObjectContexts);
                Assert.Null(interceptionContext.OriginalException);
                Assert.Equal((TaskStatus)0, interceptionContext.TaskStatus);
                Assert.Null(interceptionContext.UserState);
            }
コード例 #19
0
            public void Cloning_the_interception_context_preserves_contextual_information_but_not_mutable_state(bool useObsoleteState)
            {
                var objectContext  = new ObjectContext();
                var dbContext      = DbContextMockHelper.CreateDbContext(objectContext);
                var mockConnection = new Mock <DbConnection>().Object;

                var interceptionContext = new DbTransactionInterceptionContext();

                var mutableData = ((IDbMutableInterceptionContext)interceptionContext).MutableData;

                mutableData.SetExceptionThrown(new Exception("Cheez Whiz"));
                if (useObsoleteState)
                {
#pragma warning disable 618
                    interceptionContext.UserState = "Cheddar";
#pragma warning restore 618
                }
                else
                {
                    interceptionContext.SetUserState("A", "AState");
                    interceptionContext.SetUserState("B", "BState");
                }

                interceptionContext = interceptionContext
                                      .WithDbContext(dbContext)
                                      .WithObjectContext(objectContext)
                                      .WithConnection(mockConnection)
                                      .AsAsync();

                Assert.Equal(new[] { objectContext }, interceptionContext.ObjectContexts);
                Assert.Equal(new[] { dbContext }, interceptionContext.DbContexts);
                Assert.Same(mockConnection, interceptionContext.Connection);
                Assert.True(interceptionContext.IsAsync);

                Assert.Null(interceptionContext.Exception);
                Assert.Null(interceptionContext.OriginalException);
                Assert.False(interceptionContext.IsExecutionSuppressed);
                if (useObsoleteState)
                {
#pragma warning disable 618
                    Assert.Null(interceptionContext.UserState);
#pragma warning restore 618
                }
                else
                {
                    Assert.Null(interceptionContext.FindUserState("A"));
                    Assert.Null(interceptionContext.FindUserState("B"));
                }
            }
コード例 #20
0
            public void Initially_has_no_state()
            {
                var interceptionContext = new DbTransactionInterceptionContext<int>();

                Assert.Empty(interceptionContext.DbContexts);
                Assert.Null(interceptionContext.Exception);
                Assert.False(interceptionContext.IsAsync);
                Assert.False(interceptionContext.IsExecutionSuppressed);
                Assert.Empty(interceptionContext.ObjectContexts);
                Assert.Null(interceptionContext.OriginalException);
                Assert.Equal(0, interceptionContext.OriginalResult);
                Assert.Equal(0, interceptionContext.Result);
                Assert.Equal((TaskStatus)0, interceptionContext.TaskStatus);
                Assert.Null(interceptionContext.UserState);
            }
コード例 #21
0
        public void BeganTransaction_does_not_record_transaction_if_connection_does_not_match()
        {
            var context = Core.Objects.MockHelper.CreateMockObjectContext<object>();
            var handler = new DefaultTransactionHandler();
            handler.Initialize(context);
            var mockTransaction = new Mock<DbTransaction>().Object;

            var beginTransactionInterceptionContext = new BeginTransactionInterceptionContext();
            beginTransactionInterceptionContext.Result = mockTransaction;

            handler.BeganTransaction(new Mock<DbConnection>().Object, beginTransactionInterceptionContext);

            var interceptionContext = new DbTransactionInterceptionContext();
            interceptionContext.Exception = new Exception();
            handler.Committed(mockTransaction, interceptionContext);

            Assert.IsNotType<CommitFailedException>(interceptionContext.Exception);
        }
コード例 #22
0
        public void Commited_wraps_exceptions_for_known_transactions()
        {
            var context = Core.Objects.MockHelper.CreateMockObjectContext<object>();
            var handler = new DefaultTransactionHandler();
            handler.Initialize(context);
            var mockTransaction = new Mock<DbTransaction>().Object;

            var beginTransactionInterceptionContext = new BeginTransactionInterceptionContext();
            beginTransactionInterceptionContext.Result = mockTransaction;

            handler.BeganTransaction(handler.Connection, beginTransactionInterceptionContext);

            var interceptionContext = new DbTransactionInterceptionContext().WithConnection(handler.Connection);
            interceptionContext.Exception = new Exception();
            handler.Committed(mockTransaction, interceptionContext);

            Assert.IsType<CommitFailedException>(interceptionContext.Exception);
        }
コード例 #23
0
        /// <summary>
        /// This method is called after <see cref="DbTransaction.Commit" /> is invoked.
        /// The default implementation of this method filters by <see cref="DbContext" /> set into
        /// <see cref="Context" />, if any, and then logs the event.
        /// </summary>
        /// <param name="transaction">The transaction that was commited.</param>
        /// <param name="interceptionContext">Contextual information associated with the call.</param>
        public virtual void Committed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext)
        {
            Check.NotNull(transaction, "transaction");
            Check.NotNull(interceptionContext, "interceptionContext");

            if (Context == null ||
                interceptionContext.DbContexts.Contains(Context, ReferenceEquals))
            {
                if (interceptionContext.Exception != null)
                {
                    Write(Strings.TransactionCommitErrorLog(DateTimeOffset.Now, interceptionContext.Exception.Message, Environment.NewLine));
                }
                else
                {
                    Write(Strings.TransactionCommittedLog(DateTimeOffset.Now, Environment.NewLine));
                }
            }
        }
コード例 #24
0
        /// <summary>
        /// Sends <see cref="IDbTransactionInterceptor.Disposing" /> and
        /// <see cref="IDbTransactionInterceptor.Disposed" /> to any <see cref="IDbConnectionInterceptor" />
        /// registered on <see cref="DbInterception" /> before/after making a
        /// call to <see cref="DbTransaction.Dispose()" />.
        /// </summary>
        /// <param name="transaction">The transaction on which the operation will be executed.</param>
        /// <param name="interceptionContext">Optional information about the context of the call being made.</param>
        public virtual void Dispose(DbTransaction transaction, DbInterceptionContext interceptionContext)
        {
            Check.NotNull(transaction, "transaction");
            Check.NotNull(interceptionContext, "interceptionContext");

            var clonedInterceptionContext = new DbTransactionInterceptionContext(interceptionContext);

            if (transaction.Connection != null)
            {
                clonedInterceptionContext = clonedInterceptionContext.WithConnection(transaction.Connection);
            }

            InternalDispatcher.Dispatch(
                transaction,
                (t, c) => t.Dispose(),
                clonedInterceptionContext,
                (i, t, c) => i.Disposing(t, c),
                (i, t, c) => i.Disposed(t, c));
        }
コード例 #25
0
 /// <summary>
 /// This method is called after <see cref="M:System.Data.Common.DbTransaction.Rollback" /> is invoked.
 /// The default implementation of this method filters by <see cref="T:System.Data.Entity.DbContext" /> set into
 /// <see cref="P:System.Data.Entity.Infrastructure.Interception.DatabaseLogFormatter.Context" />, if any, and then logs the event.
 /// </summary>
 /// <param name="transaction">The transaction that was rolled back.</param>
 /// <param name="interceptionContext">Contextual information associated with the call.</param>
 public virtual void RolledBack(
     DbTransaction transaction,
     DbTransactionInterceptionContext interceptionContext)
 {
     Check.NotNull <DbTransaction>(transaction, nameof(transaction));
     Check.NotNull <DbTransactionInterceptionContext>(interceptionContext, nameof(interceptionContext));
     if (this.Context != null && !interceptionContext.DbContexts.Contains <DbContext>(this.Context, new Func <DbContext, DbContext, bool>(object.ReferenceEquals)))
     {
         return;
     }
     if (interceptionContext.Exception != null)
     {
         this.Write(Strings.TransactionRollbackErrorLog((object)DateTimeOffset.Now, (object)interceptionContext.Exception.Message, (object)Environment.NewLine));
     }
     else
     {
         this.Write(Strings.TransactionRolledBackLog((object)DateTimeOffset.Now, (object)Environment.NewLine));
     }
 }
コード例 #26
0
 public void RollingBack(DbTransaction transaction,
                         DbTransactionInterceptionContext interceptionContext)
 {
 }
コード例 #27
0
 public void IsolationLevelGot(DbTransaction transaction,
                               DbTransactionInterceptionContext<IsolationLevel> interceptionContext)
 {
 }
コード例 #28
0
 public void Disposed(DbTransaction transaction,
                      DbTransactionInterceptionContext interceptionContext)
 {
 }
コード例 #29
0
 /// <summary>
 /// Stops tracking the transaction that was disposed.
 /// </summary>
 /// <param name="transaction">The transaction that was disposed.</param>
 /// <param name="interceptionContext">Contextual information associated with the call.</param>
 /// <seealso cref="IDbTransactionInterceptor.Disposed" />
 public override void Disposed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext)
 {
     RolledBack(transaction, interceptionContext);
 }
コード例 #30
0
 /// <summary>
 /// Does not write to log unless overridden.
 /// </summary>
 /// <param name="transaction">The transaction being commited.</param>
 /// <param name="interceptionContext">Contextual information associated with the call.</param>
 public virtual void Committing(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext)
 {
 }
コード例 #31
0
 /// <summary>
 /// Does not write to log unless overridden.
 /// </summary>
 /// <param name="transaction">The transaction.</param>
 /// <param name="interceptionContext">Contextual information associated with the call.</param>
 public virtual void ConnectionGot(DbTransaction transaction, DbTransactionInterceptionContext <DbConnection> interceptionContext)
 {
 }
コード例 #32
0
 public abstract void Committed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext);
コード例 #33
0
 /// <summary>
 /// This method is called after <see cref="DbTransaction.Dispose()" /> is invoked.
 /// The default implementation of this method filters by <see cref="DbContext" /> set into
 /// <see cref="Context" />, if any, and then logs the event.
 /// </summary>
 /// <param name="transaction">The transaction that was disposed.</param>
 /// <param name="interceptionContext">Contextual information associated with the call.</param>
 public virtual void Disposed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext)
 {
     RolledBack(transaction, interceptionContext);
 }
コード例 #34
0
 public void RolledBack_logs_exceptions()
 {
     var writer = new StringWriter();
     var interceptionContext = new DbTransactionInterceptionContext();
     interceptionContext.Exception = new Exception("Boo");
     new DatabaseLogFormatter(writer.Write).RolledBack(new Mock<DbTransaction>().Object, interceptionContext);
     Assert.True(
         _resourceVerifier.IsMatch("TransactionRollbackErrorLog", GetSingleLine(writer), new AnyValueParameter(), "Boo", ""));
 }
コード例 #35
0
 public abstract void RolledBack(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext);
コード例 #36
0
 public void Committing(DbTransaction transaction,
                        DbTransactionInterceptionContext interceptionContext)
 {
 }
コード例 #37
0
        /// <summary>
        /// If there was an exception thrown checks the database for this transaction and rethrows it if not found.
        /// Otherwise marks the commit as succeeded and queues the transaction information to be deleted.
        /// </summary>
        /// <param name="transaction">The transaction that was commited.</param>
        /// <param name="interceptionContext">Contextual information associated with the call.</param>
        /// <seealso cref="IDbTransactionInterceptor.Committed" />
        public override void Committed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext)
        {
            TransactionRow transactionRow;
            if (TransactionContext == null
                || (interceptionContext.Connection != null && !MatchesParentContext(interceptionContext.Connection, interceptionContext))
                || !_transactions.TryGetValue(transaction, out transactionRow))
            {
                return;
            }

            _transactions.Remove(transaction);
            if (interceptionContext.Exception != null)
            {
                TransactionRow existingTransactionRow = null;
                try
                {
                    existingTransactionRow = TransactionContext.Transactions
                        .AsNoTracking()
                        .WithExecutionStrategy(new DefaultExecutionStrategy())
                        .SingleOrDefault(t => t.Id == transactionRow.Id);
                }
                catch (EntityCommandExecutionException)
                {
                    // Transaction table doesn't exist
                }

                if (existingTransactionRow != null)
                {
                    // The transaction id is still in the database, so the commit succeeded
                    interceptionContext.Exception = null;

                    PruneTransactionHistory(transactionRow);
                }
                else
                {
                    TransactionContext.Entry(transactionRow).State = EntityState.Detached;
                }
            }
            else
            {
                PruneTransactionHistory(transactionRow);
            }
        }
コード例 #38
0
        /// <summary>
        /// Sends <see cref="IDbTransactionInterceptor.Disposing" /> and
        /// <see cref="IDbTransactionInterceptor.Disposed" /> to any <see cref="IDbConnectionInterceptor" />
        /// registered on <see cref="DbInterception" /> before/after making a
        /// call to <see cref="DbTransaction.Dispose()" />.
        /// </summary>
        /// <param name="transaction">The transaction on which the operation will be executed.</param>
        /// <param name="interceptionContext">Optional information about the context of the call being made.</param>
        public virtual void Dispose(DbTransaction transaction, DbInterceptionContext interceptionContext)
        {
            Check.NotNull(transaction, "transaction");
            Check.NotNull(interceptionContext, "interceptionContext");

            var clonedInterceptionContext = new DbTransactionInterceptionContext(interceptionContext);

            if (transaction.Connection != null)
            {
                clonedInterceptionContext = clonedInterceptionContext.WithConnection(transaction.Connection);
            }

            InternalDispatcher.Dispatch(
                transaction,
                (t, c) => t.Dispose(),
                clonedInterceptionContext,
                (i, t, c) => i.Disposing(t, c),
                (i, t, c) => i.Disposed(t, c));
        }
コード例 #39
0
 public void RolledBack(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext)
 {
     RemoveAffectedEntitySets(transaction);
 }
コード例 #40
0
 public abstract void Disposed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext);
コード例 #41
0
 public abstract void RolledBack(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext);
コード例 #42
0
 public abstract void IsolationLevelGot(
     DbTransaction transaction, DbTransactionInterceptionContext <IsolationLevel> interceptionContext);
コード例 #43
0
 public abstract void ConnectionGot(
     DbTransaction transaction, DbTransactionInterceptionContext <DbConnection> interceptionContext);
コード例 #44
0
 /// <summary>
 /// Does not write to log unless overridden.
 /// </summary>
 /// <param name="transaction">The transaction.</param>
 /// <param name="interceptionContext">Contextual information associated with the call.</param>
 public virtual void IsolationLevelGot(
     DbTransaction transaction, DbTransactionInterceptionContext <IsolationLevel> interceptionContext)
 {
 }
コード例 #45
0
 /// <summary>
 /// Does not write to log unless overridden.
 /// </summary>
 /// <param name="transaction">The transaction being rolled back.</param>
 /// <param name="interceptionContext">Contextual information associated with the call.</param>
 public virtual void RollingBack(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext)
 {
 }
コード例 #46
0
 /** <inheritDoc /> */
 public void Disposing(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext)
 {
     // No-op
 }
コード例 #47
0
        /// <summary>
        /// Stops tracking the transaction that was rolled back.
        /// </summary>
        /// <param name="transaction">The transaction that was rolled back.</param>
        /// <param name="interceptionContext">Contextual information associated with the call.</param>
        /// <seealso cref="IDbTransactionInterceptor.RolledBack" />
        public override void RolledBack(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext)
        {
            TransactionRow transactionRow;
            if (TransactionContext == null
                || (interceptionContext.Connection != null && !MatchesParentContext(interceptionContext.Connection, interceptionContext))
                || !_transactions.TryGetValue(transaction, out transactionRow))
            {
                return;
            }

            _transactions.Remove(transaction);
            TransactionContext.Entry(transactionRow).State = EntityState.Detached;
        }
コード例 #48
0
 /// <summary>
 /// Does not write to log unless overridden.
 /// </summary>
 /// <param name="transaction">The transaction that was disposed.</param>
 /// <param name="interceptionContext">Contextual information associated with the call.</param>
 public virtual void Disposed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext)
 {
 }
コード例 #49
0
 /** <inheritDoc /> */
 public void Disposed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext)
 {
     HashSet<EntitySetBase> val;
     _entitySets.TryRemove(transaction, out val);
 }
コード例 #50
0
 public void RolledBack(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext)
 {
     // No-op
 }
コード例 #51
0
 /** <inheritDoc /> */
 public void ConnectionGetting(DbTransaction transaction, DbTransactionInterceptionContext<DbConnection> interceptionContext)
 {
     // No-op
 }
コード例 #52
0
 public void Disposing(DbTransaction transaction,
                       DbTransactionInterceptionContext interceptionContext)
 {
     var context = EFProfilerContextProvider.GetLoggedDbTransaction(transaction, interceptionContext);
     _profiler.TransactionDisposing(transaction, context);
 }
コード例 #53
0
 public void IsolationLevelGetting(DbTransaction transaction, DbTransactionInterceptionContext<IsolationLevel> interceptionContext)
 {
     // No-op
 }
コード例 #54
0
 public void RolledBack(DbTransaction transaction,
                        DbTransactionInterceptionContext interceptionContext)
 {
     var context = EFProfilerContextProvider.GetLoggedDbTransaction(transaction, interceptionContext);
     _profiler.TransactionRolledBack(transaction, context);
 }
コード例 #55
0
 /** <inheritDoc /> */
 public void Committed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext)
 {
     HashSet<EntitySetBase> entitySets;
     if (_entitySets.TryGetValue(transaction, out entitySets))
         _cache.InvalidateSets(entitySets);
 }
コード例 #56
0
 public void Committed(DbTransaction transaction,
                       DbTransactionInterceptionContext interceptionContext)
 {
     var context = EFProfilerContextProvider.GetLoggedDbTransaction(transaction, interceptionContext);
     _profiler.TransactionCommitted(transaction, context);
 }
コード例 #57
0
            public void Cloning_the_interception_context_preserves_contextual_information_but_not_mutable_state()
            {
                var objectContext = new ObjectContext();
                var dbContext = DbContextMockHelper.CreateDbContext(objectContext);
                var mockConnection = new Mock<DbConnection>().Object;

                var interceptionContext = new DbTransactionInterceptionContext();

                var mutableData = ((IDbMutableInterceptionContext)interceptionContext).MutableData;
                mutableData.SetExceptionThrown(new Exception("Cheez Whiz"));
                mutableData.UserState = "Caerphilly";

                interceptionContext = interceptionContext
                    .WithDbContext(dbContext)
                    .WithObjectContext(objectContext)
                    .WithConnection(mockConnection)
                    .AsAsync();

                Assert.Equal(new[] { objectContext }, interceptionContext.ObjectContexts);
                Assert.Equal(new[] { dbContext }, interceptionContext.DbContexts);
                Assert.Same(mockConnection, interceptionContext.Connection);
                Assert.True(interceptionContext.IsAsync);

                Assert.Null(interceptionContext.Exception);
                Assert.Null(interceptionContext.OriginalException);
                Assert.False(interceptionContext.IsExecutionSuppressed);
                Assert.Null(interceptionContext.UserState);
            }
コード例 #58
0
 public void ConnectionGot(DbTransaction transaction,
                           DbTransactionInterceptionContext<DbConnection> interceptionContext)
 {
 }
コード例 #59
0
 public abstract void Disposing(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext);