Exemple #1
0
        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 BeginTransactionInterceptionContext();

            var mutableData = ((IDbMutableInterceptionContext <DbTransaction>)interceptionContext).MutableData;
            var transaction = new Mock <DbTransaction>().Object;

            mutableData.SetExecuted(transaction);
            mutableData.SetExceptionThrown(new Exception("Cheez Whiz"));

            interceptionContext = interceptionContext
                                  .WithDbContext(dbContext)
                                  .WithObjectContext(objectContext)
                                  .WithIsolationLevel(IsolationLevel.RepeatableRead)
                                  .AsAsync();

            interceptionContext = new BeginTransactionInterceptionContext(interceptionContext);

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

            Assert.Null(interceptionContext.Result);
            Assert.Null(interceptionContext.OriginalResult);
            Assert.Null(interceptionContext.Exception);
            Assert.Null(interceptionContext.OriginalException);
            Assert.False(interceptionContext.IsExecutionSuppressed);
        }
Exemple #2
0
 /// <summary>
 /// Sends <see cref="M:System.Data.Entity.Infrastructure.Interception.IDbConnectionInterceptor.BeginningTransaction(System.Data.Common.DbConnection,System.Data.Entity.Infrastructure.Interception.BeginTransactionInterceptionContext)" /> and
 /// <see cref="M:System.Data.Entity.Infrastructure.Interception.IDbConnectionInterceptor.BeganTransaction(System.Data.Common.DbConnection,System.Data.Entity.Infrastructure.Interception.BeginTransactionInterceptionContext)" /> 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.DbConnection.BeginTransaction(System.Data.IsolationLevel)" />.
 /// </summary>
 /// <remarks>
 /// Note that the result of executing the command 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="connection">The connection 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 DbTransaction BeginTransaction(
     DbConnection connection,
     BeginTransactionInterceptionContext interceptionContext)
 {
     Check.NotNull <DbConnection>(connection, nameof(connection));
     Check.NotNull <BeginTransactionInterceptionContext>(interceptionContext, nameof(interceptionContext));
     return(this.InternalDispatcher.Dispatch <DbConnection, BeginTransactionInterceptionContext, DbTransaction>(connection, (Func <DbConnection, BeginTransactionInterceptionContext, DbTransaction>)((t, c) => t.BeginTransaction(c.IsolationLevel)), new BeginTransactionInterceptionContext((DbInterceptionContext)interceptionContext), (Action <IDbConnectionInterceptor, DbConnection, BeginTransactionInterceptionContext>)((i, t, c) => i.BeginningTransaction(t, c)), (Action <IDbConnectionInterceptor, DbConnection, BeginTransactionInterceptionContext>)((i, t, c) => i.BeganTransaction(t, c))));
 }
Exemple #3
0
        /// <summary>
        /// Creates a new <see cref="T:System.Data.Entity.Infrastructure.Interception.BeginTransactionInterceptionContext" /> that contains all the contextual information in this
        /// interception context together with the given <see cref="P:System.Data.Entity.Infrastructure.Interception.BeginTransactionInterceptionContext.IsolationLevel" />.
        /// </summary>
        /// <param name="isolationLevel">The isolation level to associate.</param>
        /// <returns>A new interception context associated with the given isolation level.</returns>
        public BeginTransactionInterceptionContext WithIsolationLevel(
            IsolationLevel isolationLevel)
        {
            BeginTransactionInterceptionContext interceptionContext = this.TypedClone();

            interceptionContext._isolationLevel = isolationLevel;
            return(interceptionContext);
        }
Exemple #4
0
            public void BeganTransaction_logs_exceptions()
            {
                var writer = new StringWriter();
                var interceptionContext = new BeginTransactionInterceptionContext();

                interceptionContext.Exception = new Exception("Boo");
                new DatabaseLogFormatter(writer.Write).BeganTransaction(new Mock <DbConnection>().Object, interceptionContext);
                Assert.True(
                    _resourceVerifier.IsMatch("TransactionStartErrorLog", GetSingleLine(writer), new AnyValueParameter(), "Boo", ""));
            }
Exemple #5
0
        /// <summary>
        /// Creates a new <see cref="T:System.Data.Entity.Infrastructure.Interception.BeginTransactionInterceptionContext" /> by copying immutable state from the given
        /// interception context. Also see <see cref="M:System.Data.Entity.Infrastructure.Interception.BeginTransactionInterceptionContext.Clone" />
        /// </summary>
        /// <param name="copyFrom">The context from which to copy state.</param>
        public BeginTransactionInterceptionContext(DbInterceptionContext copyFrom)
            : base(copyFrom)
        {
            Check.NotNull <DbInterceptionContext>(copyFrom, nameof(copyFrom));
            BeginTransactionInterceptionContext interceptionContext = copyFrom as BeginTransactionInterceptionContext;

            if (interceptionContext == null)
            {
                return;
            }
            this._isolationLevel = interceptionContext._isolationLevel;
        }
Exemple #6
0
        /// <summary>
        /// Sends <see cref="IDbConnectionInterceptor.BeginningTransaction" /> and
        /// <see cref="IDbConnectionInterceptor.BeganTransaction" /> to any <see cref="IDbConnectionInterceptor" />
        /// registered on <see cref="DbInterception" /> before/after making a
        /// call to <see cref="DbConnection.BeginTransaction(IsolationLevel)" />.
        /// </summary>
        /// <remarks>
        /// Note that the result of executing the command 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="connection">The connection 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 DbTransaction BeginTransaction(
            DbConnection connection, BeginTransactionInterceptionContext interceptionContext)
        {
            Check.NotNull(connection, "connection");
            Check.NotNull(interceptionContext, "interceptionContext");

            return(InternalDispatcher.Dispatch(
                       connection,
                       (t, c) => t.BeginTransaction(c.IsolationLevel),
                       new BeginTransactionInterceptionContext(interceptionContext),
                       (i, t, c) => i.BeginningTransaction(t, c),
                       (i, t, c) => i.BeganTransaction(t, c)));
        }
Exemple #7
0
        /// <summary>
        /// Sends <see cref="IDbConnectionInterceptor.BeginningTransaction" /> and
        /// <see cref="IDbConnectionInterceptor.BeganTransaction" /> to any <see cref="IDbConnectionInterceptor" />
        /// registered on <see cref="DbInterception" /> before/after making a
        /// call to <see cref="DbConnection.BeginTransaction(IsolationLevel)" />.
        /// </summary>
        /// <remarks>
        /// Note that the result of executing the command 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="dbConnection">The connection 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 DbTransaction BeginTransaction(
            DbConnection dbConnection, BeginTransactionInterceptionContext interceptionContext)
        {
            Check.NotNull(dbConnection, "dbConnection");
            Check.NotNull(interceptionContext, "interceptionContext");

            var clonedInterceptionContext = new BeginTransactionInterceptionContext(interceptionContext);

            return(InternalDispatcher.Dispatch(
                       () => dbConnection.BeginTransaction(clonedInterceptionContext.IsolationLevel),
                       clonedInterceptionContext,
                       i => i.BeginningTransaction(dbConnection, clonedInterceptionContext),
                       i => i.BeganTransaction(dbConnection, clonedInterceptionContext)));
        }
Exemple #8
0
        public void Initially_has_no_state()
        {
            var interceptionContext = new BeginTransactionInterceptionContext();

            Assert.Empty(interceptionContext.DbContexts);
            Assert.Null(interceptionContext.Exception);
            Assert.False(interceptionContext.IsAsync);
            Assert.False(interceptionContext.IsExecutionSuppressed);
            Assert.Equal(IsolationLevel.Unspecified, interceptionContext.IsolationLevel);
            Assert.Empty(interceptionContext.ObjectContexts);
            Assert.Null(interceptionContext.OriginalException);
            Assert.Null(interceptionContext.OriginalResult);
            Assert.Null(interceptionContext.Result);
            Assert.Equal((TaskStatus)0, interceptionContext.TaskStatus);
        }
        public void BeginTransaction_executes_operation_and_dispatches_to_interceptors()
        {
            var mockConnection     = new Mock <DbConnection>();
            var mockInterceptor    = new Mock <IDbConnectionInterceptor>();
            var dispatcher         = new DbConnectionDispatcher();
            var internalDispatcher = dispatcher.InternalDispatcher;

            internalDispatcher.Add(mockInterceptor.Object);

            var interceptionContext = new BeginTransactionInterceptionContext().WithIsolationLevel(IsolationLevel.Serializable);

            dispatcher.BeginTransaction(mockConnection.Object, interceptionContext);

            mockConnection.Protected().Verify <DbTransaction>("BeginDbTransaction", Times.Once(), IsolationLevel.Serializable);
            mockInterceptor.Verify(m => m.BeginningTransaction(mockConnection.Object, It.IsAny <BeginTransactionInterceptionContext>()), Times.Once());
            mockInterceptor.Verify(m => m.BeganTransaction(mockConnection.Object, It.IsAny <BeginTransactionInterceptionContext>()), Times.Once());
        }
        /// <summary>
        /// Called after <see cref="DbConnection.BeginTransaction(Data.IsolationLevel)" /> 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="connection">The connection that began the transaction.</param>
        /// <param name="interceptionContext">Contextual information associated with the call.</param>
        public virtual void BeganTransaction(DbConnection connection, BeginTransactionInterceptionContext interceptionContext)
        {
            Check.NotNull(connection, "connection");
            Check.NotNull(interceptionContext, "interceptionContext");

            if (Context == null ||
                interceptionContext.DbContexts.Contains(Context, ReferenceEquals))
            {
                if (interceptionContext.Exception != null)
                {
                    Write(Strings.TransactionStartErrorLog(DateTimeOffset.Now, interceptionContext.Exception.Message, Environment.NewLine));
                }
                else
                {
                    Write(Strings.TransactionStartedLog(DateTimeOffset.Now, Environment.NewLine));
                }
            }
        }
Exemple #11
0
 /// <summary>
 /// Called after <see cref="M:System.Data.Common.DbConnection.BeginTransaction(System.Data.IsolationLevel)" /> 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="connection">The connection that began the transaction.</param>
 /// <param name="interceptionContext">Contextual information associated with the call.</param>
 public virtual void BeganTransaction(
     DbConnection connection,
     BeginTransactionInterceptionContext interceptionContext)
 {
     Check.NotNull <DbConnection>(connection, nameof(connection));
     Check.NotNull <BeginTransactionInterceptionContext>(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.TransactionStartErrorLog((object)DateTimeOffset.Now, (object)interceptionContext.Exception.Message, (object)Environment.NewLine));
     }
     else
     {
         this.Write(Strings.TransactionStartedLog((object)DateTimeOffset.Now, (object)Environment.NewLine));
     }
 }
 /// <summary>
 /// Does not write to log unless overridden.
 /// </summary>
 /// <param name="connection">The connection beginning the transaction.</param>
 /// <param name="interceptionContext">Contextual information associated with the call.</param>
 public virtual void BeginningTransaction(DbConnection connection, BeginTransactionInterceptionContext interceptionContext)
 {
 }
 public abstract void BeganTransaction(DbConnection connection, BeginTransactionInterceptionContext interceptionContext);