コード例 #1
0
        public static void Rollback(DataBase dataBase)
        {
            if (getTopTransacted(dataBase) == null)
            {
                throw new InvalidOperationException("Cannot roll back when no transacted scope is present");
            }
            var tx = getTx(dataBase);

            if (tx != null)
            {
                Nrdo.FlushCache(); // The cache contains stuff that might be blown away by the rollback, so remove it.
                tx.Rollback();
                ScopeInfo.Current.GetDbScopeInfo(dataBase).tx = null;
            }
        }
コード例 #2
0
ファイル: DBTableObject.cs プロジェクト: sab39/nrdo
        public virtual void Delete()
        {
            // If it's not in the database in the first place,
            // deleting is a no-op.
            if (IsNew)
            {
                return;
            }

            // Store the ModificationCount
            var modCount = DataModification.Count;

            // Store a pre-deletion copy. This will be used in the modification events so that the Id can be captured by code that isn't in this table.
            var preDelete = this.FieldwiseClone();

            var stopwatch = Stopwatch.StartNew();
            var succeeded = false;

            // Make the database change. If an exception is thrown, FullFlush then rethrow it.
            try
            {
                // Delete from the database.
                NrdoTransactedScope.MaybeBeginTransaction(DataBase);
                using (var scope = new NrdoScope(DataBase))
                {
                    scope.ExecuteSql(DeleteStatement, setPkeyOnCmd);
                }
                isNew     = true;
                succeeded = true;
            }
            catch
            {
                DataModification.RaiseFullFlush();
                throw;
            }
            finally
            {
                stopwatch.Stop();
                Nrdo.DebugLog(() => Nrdo.DebugArgs(stopwatch, "db-delete", typeof(T).FullName, "Delete", null));
                if (!succeeded)
                {
                    deletionCache.Value.HitInfo.updateStats(stats => stats.WithFailure(stopwatch.Elapsed), stats => stats.WithFailure(stopwatch.Elapsed));
                }
                else
                {
                    deletionCache.Value.HitInfo.updateStats(stats => stats.WithModification(stopwatch.Elapsed), stats => stats.WithSingleCacheNonHit(stopwatch.Elapsed, false));
                }
            }

            // Lock again. If the modification count is not equal to what it was, FullFlush. Otherwise raise
            // the relevant event.
            lock (Nrdo.LockObj)
            {
                if (DataModification.Count != modCount)
                {
                    DataModification.RaiseFullFlush();
                }
                else
                {
                    DataModification.RaiseDelete(preDelete);
                }
            }
        }
コード例 #3
0
ファイル: DBTableObject.cs プロジェクト: sab39/nrdo
        public virtual void UpdateThis()
        {
            // If we can skip it, skip it
            if (!IsNew && IsUnchanged)
            {
                Nrdo.DebugLog(() => Nrdo.DebugArgs("db-update-skipped", typeof(T).FullName, "Update", null));
                return;
            }

            // Store the ModificationCount
            var modCount = DataModification.Count;

            // Make the database change. If an exception is thrown, FullFlush then rethrow it.
            var  wasNew    = IsNew;
            var  stopwatch = Stopwatch.StartNew();
            bool succeeded = false;

            try
            {
                // Insert or update in the database.
                string cmdText = IsNew ? InsertStatement : UpdateStatement;
                if (cmdText != null)
                {
                    NrdoTransactedScope.MaybeBeginTransaction(DataBase);
                    using (var scope = new NrdoScope(DataBase))
                    {
                        scope.ExecuteSql(cmdText, cmd =>
                        {
                            setDataOnCmd(cmd);
                            setPkeyOnCmd(cmd);
                        });
                        if (IsNew)
                        {
                            getPkeyFromSeq(scope);
                        }
                        isNew     = false;
                        succeeded = true;
                    }
                }
            }
            catch
            {
                DataModification.RaiseFullFlush();
                throw;
            }
            finally
            {
                stopwatch.Stop();
                Nrdo.DebugLog(() => Nrdo.DebugArgs(stopwatch, wasNew ? "db-insert" : "db-update", typeof(T).FullName, "Update", null));

                var cache = wasNew ? insertionCache.Value : updateCache.Value;
                if (!succeeded)
                {
                    cache.HitInfo.updateStats(stats => stats.WithFailure(stopwatch.Elapsed), stats => stats.WithFailure(stopwatch.Elapsed));
                }
                else
                {
                    cache.HitInfo.updateStats(stats => stats.WithModification(stopwatch.Elapsed), stats => stats.WithSingleCacheNonHit(stopwatch.Elapsed, false));
                }
            }

            // Lock again. If the modification count is not equal to what it was, FullFlush. Otherwise raise
            // the relevant event.
            lock (Nrdo.LockObj)
            {
                if (DataModification.Count != modCount)
                {
                    DataModification.RaiseFullFlush();
                }
                else if (wasNew)
                {
                    DataModification.RaiseInsert((T)this);
                }
                else
                {
                    DataModification.RaiseUpdate((T)this);
                }
            }
        }
コード例 #4
0
ファイル: DBObject.cs プロジェクト: sab39/nrdo
 protected static void log(Stopwatch stopwatch, string eventType, Where <T> where)
 {
     Nrdo.DebugLog(() => Nrdo.DebugArgs(stopwatch, eventType, typeof(T).FullName, where.GetMethodName.Substring(where.GetMethodName.LastIndexOf('.') + 1), where.GetParameters));
 }