Пример #1
0
        /// <summary>
        /// Rolls back all changes made since the beginning or the last call to Commit()
        /// </summary>
        /// <exception cref="InvalidUowChangeStateException">
        /// Cannot rollback during commit.
        /// or
        /// Rollback is already running.
        /// </exception>
        public virtual void Rollback()
        {
            // ha nincs semmi változás
            if (this.ChangeState == UowChangeStateEnum.NoChanges)
            {
                return;
            }

            if (!this.IsDisposing)
            {
                ThrowIfDisposingOrDisposed();
                if (!this.IsDisposing && !this.SupportsMultiThread)
                {
                    this.EnsureOwnerThread();
                }
            }

            lock (_commitOrRollbackLockObj)
            {
                // lekérjük, miután megszereztük a lock-ot
                var currentChangeState = this.ChangeState;
                switch (currentChangeState)
                {
                case UowChangeStateEnum.HasChanges:
                    var sw = Stopwatch.StartNew();
                    try
                    {
                        this.ChangeState = UowChangeStateEnum.RollingBack;
                        this.Logger.LogUowRollingBack(LogLevel.Debug, this.UowId);

                        DoRollback();

                        sw.Stop();
                        this.Logger.LogUowRollbackSuccess(LogLevel.Debug, this.UowId, sw.Elapsed);
                    }
                    catch (Exception ex)
                    {
                        sw.Stop();
                        this.Logger.LogUowRollbackFailed(LogLevel.Error, this.UowId, sw.Elapsed, ex);

                        this.ChangeState = UowChangeStateEnum.HasChanges;
                        throw;
                    }
                    this.ChangeState = UowChangeStateEnum.NoChanges;
                    break;

                case UowChangeStateEnum.NoChanges: return;

                case UowChangeStateEnum.Commiting: throw new InvalidUowChangeStateException(currentChangeState, "Cannot rollback during commit.");

                case UowChangeStateEnum.RollingBack: throw new InvalidUowChangeStateException(currentChangeState, "Rollback is already running.");
                //break;

                default:
                    //throw new NotSupportedException(string.Format(System.Globalization.CultureInfo.InvariantCulture,
                    //    "'{0}' is a not supported '{1}' value.", currentChangeState, typeof(UowChangeStateEnum).Name));
                    throw NotSupportedValueException.New <UowChangeStateEnum>(currentChangeState);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Commits all changes made since the beginning or the last call to Commit()
        /// </summary>
        /// <exception cref="InvalidUowChangeStateException">
        /// Commit is already running.
        /// or
        /// Cannot commit during rollback.
        /// </exception>
        public virtual void Commit()
        {
            // ha nincs semmi változás
            if (this.ChangeState == UowChangeStateEnum.NoChanges)
            {
                return;
            }
            ThrowIfDisposingOrDisposed();

            if (!this.SupportsMultiThread)
            {
                this.EnsureOwnerThread();
            }

            lock (_commitOrRollbackLockObj)
            {
                // lekérjük, miután megszereztük a lock-ot
                var currentChangeState = this.ChangeState;
                switch (currentChangeState)
                {
                case UowChangeStateEnum.HasChanges:
                    var sw = Stopwatch.StartNew();
                    try
                    {
                        this.ChangeState = UowChangeStateEnum.Commiting;
                        this.Logger.LogUowCommiting(LogLevel.Debug, this.UowId);

                        DoCommit();

                        sw.Stop();
                        this.Logger.LogUowCommitSuccess(LogLevel.Debug, this.UowId, sw.Elapsed);
                    }
                    catch (Exception ex)
                    {
                        sw.Stop();
                        this.Logger.LogUowCommitFailed(LogLevel.Error, this.UowId, sw.Elapsed, ex);

                        this.ChangeState = UowChangeStateEnum.HasChanges;

                        throw;
                    }
                    this.ChangeState = UowChangeStateEnum.NoChanges;
                    break;

                case UowChangeStateEnum.NoChanges: return;

                case UowChangeStateEnum.Commiting: throw new InvalidUowChangeStateException(currentChangeState, "Commit is already running.");

                case UowChangeStateEnum.RollingBack: throw new InvalidUowChangeStateException(currentChangeState, "Cannot commit during rollback.");

                default:
                    throw NotSupportedValueException.New <UowChangeStateEnum>(currentChangeState);
                }
            }
        }
Пример #3
0
 /// <summary>
 /// Logs the change of the ChangeState of the unit of work.
 /// </summary>
 /// <param name="logger">The logger.</param>
 /// <param name="level">The level.</param>
 /// <param name="uowId">The uow identifier.</param>
 /// <param name="from">The previous change state.</param>
 /// <param name="to">The new change state.</param>
 public static void LogUowChangeStateChanged(this ILogger logger, LogLevel level, int uowId, UowChangeStateEnum from, UowChangeStateEnum to)
 {
     logger?.Log(level, LogEvents.UowChangeStateChanged, "Change state of unit of work with {uow.Id} changed from {oldValue} to {newValue}.", uowId, from, to);
 }
 public UowChangeStateMismatchException(UowChangeStateEnum currentChangeState, UowChangeStateEnum requiredChangeState, Exception innerException)
     : base(currentChangeState, GetDefaultMessageOfUowChangeStateMismatchException(currentChangeState, requiredChangeState), innerException)
 {
     this.RequiredChangeState = requiredChangeState;
 }
 public UowChangeStateMismatchException(UowChangeStateEnum currentChangeState, UowChangeStateEnum requiredChangeState, string message, Exception innerException)
     : base(currentChangeState, message, innerException)
 {
     this.RequiredChangeState = requiredChangeState;
 }
 public static string GetDefaultMessageOfUowChangeStateMismatchException(UowChangeStateEnum currentChangeState, UowChangeStateEnum requiredChangeState)
 {
     return(string.Format(System.Globalization.CultureInfo.InvariantCulture, "Current change state '{0}' of the UnitOfWork does not equal the required value '{1}'.", currentChangeState, requiredChangeState));
 }
 public InvalidUowChangeStateException(UowChangeStateEnum currentChangeState, Exception innerException)
     : base(GetDefaultMessageOfInvalidUowChangeStateException(currentChangeState), innerException)
 {
     this.CurrentChangeState = currentChangeState;
 }
 public InvalidUowChangeStateException(UowChangeStateEnum currentChangeState, string message, Exception innerException)
     : base(message, innerException)
 {
     this.CurrentChangeState = currentChangeState;
 }
 public static string GetDefaultMessageOfInvalidUowChangeStateException(UowChangeStateEnum currentChangeState)
 {
     return(string.Format(System.Globalization.CultureInfo.InvariantCulture, "The method or operation is not valid when the UnitOfWork is in the '{0}' change state.", currentChangeState));
 }