Exemplo n.º 1
0
        public static DateTime ReadDateTime(Stream stream)
        {
            //long v = ReadInt64(stream);
            //return DateTime.FromBinary(v);
            int b = stream.ReadByte();

            if (b == -1)
            {
                throw new EndOfStreamException();
            }

            DateTimeKind kind = (DateTimeKind)b;

            switch (kind)
            {
            case DateTimeKind.Utc:
                return(new DateTime(ReadInt64(stream), kind));

            //break;

            case DateTimeKind.Local:
                return(new DateTime(ReadInt64(stream), DateTimeKind.Utc).ToLocalTime());

            //break;

            case DateTimeKind.Unspecified:
                return(new DateTime(ReadInt64(stream), DateTimeKind.Unspecified));

            //break;

            default:
                throw NotSupportedValueException.New(kind);
            }
        }
Exemplo n.º 2
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);
                }
            }
        }
Exemplo n.º 3
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);
                }
            }
        }
Exemplo n.º 4
0
        public static DateTime?ReadNullableDateTime(Stream stream)
        {
            //if (ReadBoolean(stream))
            //    return ReadDateTime(stream);
            //else
            //    return null;

            var b = stream.ReadByte();

            if (b == -1)
            {
                throw new EndOfStreamException();
            }
            if (b == 0)
            {
                return(null);
            }
            else
            {
                DateTimeKind kind = (DateTimeKind)((byte)(b - 1));
                switch (kind)
                {
                case DateTimeKind.Utc:
                    return(new DateTime(ReadInt64(stream), kind));

                //break;

                case DateTimeKind.Local:
                    return(new DateTime(ReadInt64(stream), DateTimeKind.Utc).ToLocalTime());

                //break;

                case DateTimeKind.Unspecified:
                    return(new DateTime(ReadInt64(stream), DateTimeKind.Unspecified));

                //break;

                default:
                    throw NotSupportedValueException.New(kind);
                }
            }
        }
Exemplo n.º 5
0
        //#endif

        #region DateTime
        public static void Write(Stream stream, DateTime value)
        {
            //long v = value.ToBinary();
            //Write(stream, v);
            Write(stream, (byte)value.Kind);
            switch (value.Kind)
            {
            case DateTimeKind.Utc:
                Write(stream, value.Ticks);
                break;

            case DateTimeKind.Local:
                Write(stream, value.ToUniversalTime().Ticks);
                break;

            case DateTimeKind.Unspecified:
                Write(stream, value.Ticks);
                break;

            default:
                throw NotSupportedValueException.New(value.Kind);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Sets the change state to has changes.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">
        /// Thrown if <see cref="ChangeState"/> equals <see cref="UowChangeStateEnum.RollingBack"/> or <seealso cref="UowChangeStateEnum.Commiting"/>.
        /// </exception>
        /// <exception cref="NotSupportedValueException">
        /// Thrown if <see cref="ChangeState"/> is not supported.
        /// </exception>
        protected void SetChangeStateToHasChanges()
        {
            var cs = this.ChangeState;

            switch (cs)
            {
            case UowChangeStateEnum.NoChanges:
                this.ChangeState = UowChangeStateEnum.HasChanges;
                break;

            case UowChangeStateEnum.HasChanges:
                break;

            case UowChangeStateEnum.RollingBack:
                throw new InvalidOperationException("Cannot make changes during rollback!");

            case UowChangeStateEnum.Commiting:
                throw new InvalidOperationException("Cannot make changes during commit!");

            default:
                throw NotSupportedValueException.New <UowChangeStateEnum>(cs);
            }
        }
Exemplo n.º 7
0
        public static void Write(Stream stream, DateTime?value)
        {
            //if (value == null)
            //    Write(stream, false);
            //else
            //{
            //    Write(stream, true);
            //    Write(stream, value.Value);
            //}

            if (value == null)
            {
                Write(stream, (byte)0);
            }
            else
            {
                Write(stream, (byte)((byte)value.Value.Kind + (byte)1));
                switch (value.Value.Kind)
                {
                case DateTimeKind.Utc:
                    Write(stream, value.Value.Ticks);
                    break;

                case DateTimeKind.Local:
                    Write(stream, value.Value.ToUniversalTime().Ticks);
                    break;

                case DateTimeKind.Unspecified:
                    Write(stream, value.Value.Ticks);
                    break;

                default:
                    throw NotSupportedValueException.New(value.Value.Kind);
                }
            }
        }