예제 #1
0
        public void SessionScopeFlushModeNever()
        {
            using (new SessionScope()) {
                Post.DeleteAll();
                Blog.DeleteAll();
            }

            using (var scope = new SessionScope(FlushAction.Never))
            {
                var blog = new Blog {
                    Author = "hammett", Name = "some name"
                };

                //This gets flushed automatically because of the identity field
                blog.Save();

                var blogs = Blog.FindAll().ToArray();
                Assert.AreEqual(1, blogs.Length);

                //This change won't be saved to the db
                blog.Author = "A New Author";
                blog.Save(false);

                //The change should not be in the db
                blogs = Blog.FindAllByProperty("Author", "A New Author").ToArray();
                Assert.AreEqual(0, blogs.Length);

                scope.Flush();

                //The change should now be in the db
                blogs = Blog.FindAllByProperty("Author", "A New Author").ToArray();
                Assert.AreEqual(1, blogs.Length);

                //This change will be save to the db
                blog.Name = "A New Name";
                blog.Save();

                //The change should now be in the db
                blogs = Blog.FindAllByProperty("Name", "A New Name").ToArray();
                Assert.AreEqual(1, blogs.Length);

                //This deletion should not get to the db
                blog.Delete(false);

                blogs = Blog.FindAll().ToArray();
                Assert.AreEqual(1, blogs.Length);

                scope.Flush();

                //The deletion should now be in the db
                blogs = Blog.FindAll().ToArray();
                Assert.AreEqual(0, blogs.Length);
            }
        }
예제 #2
0
 /// <summary>
 /// starts a new AR sessionscope
 /// </summary>
 private void BeginARSession()
 {
     try
     {
         now = DateTime.Now;
         string logMessage;
         //if there is one, flush it
         if (RoSession.Instance["nh.sessionscope"] != null)
         {
             SessionScope scope = RoSession.Instance["nh.sessionscope"] as SessionScope;
             if (scope != null)
             {
                 scope.Flush();
             }
             logMessage = " maintained opened"; //used on redirects
         }
         //create one if there is none
         else
         {
             RoSession.Instance["nh.sessionscope"] = new SessionScope(FlushAction.Auto);
             logMessage = " opened";
         }
         logMessage = "nh.sessionscope " + RoSession.Instance["nh.sessionscope"].GetHashCode() + logMessage;
         RoLog.Instance.WriteToLog("");
         Console.WriteLine("");
         RoLog.Instance.WriteToLog(logMessage);
         Console.WriteLine(logMessage);
     }
     catch (ActiveRecordException ex)
     {
         RoLog.Instance.WriteToLog("Problems initializing the session:" + ex.Message + "/r/n" + ex,
                                   TracedAttribute.ERROR);
     }
 }
예제 #3
0
        public void AnExceptionInvalidatesTheScopeAndPreventItsFlushing()
        {
            using (new SessionScope()) {
                Post.DeleteAll();
                Blog.DeleteAll();
            }

            Post post;

            // Prepare
            using (new SessionScope())
            {
                var blog = new Blog {
                    Author = "hammett", Name = "some name"
                };
                blog.Save();

                post = new Post(blog, "title", "contents", "castle");
                post.Save();
            }

            using (var session = new SessionScope())
            {
                Assert.IsFalse(session.HasSessionError);

                Assert.Throws <ActiveRecordException>(() => {
                    post = new Post(new Blog(100), "title", "contents", "castle");
                    post.Save();
                    session.Flush();
                });

                Assert.IsTrue(session.HasSessionError);
            }
        }
예제 #4
0
 public void UsingSessionScopeUsingExplicitFlush()
 {
     using (var scope = new SessionScope())
     {
         new SSAFEntity("example").Save();
         scope.Flush();
         Assert.AreEqual(1, SSAFEntity.FindAll().Count());
     }
     using (new SessionScope())
         Assert.AreEqual(1, SSAFEntity.FindAll().Count());
 }
예제 #5
0
        private void OnEndRequest(object sender, EventArgs e)
        {
            SessionScope sessionScope = (SessionScope)HttpContext.Current.Items[sessionKey];

            if (sessionScope != null)
            {
                sessionScope.Flush();//事务提交

                if (sessionScope.WantsToCreateTheSession == false)
                {
                    sessionScope.Dispose();
                }
            }
        }
예제 #6
0
        /// <summary>
        /// ends the current AR sessionscope, from vr session
        /// </summary>
        private void CloseARSession()
        {
            SessionScope scope = RoSession.Instance["nh.sessionscope"] as SessionScope;

            try
            {
                if (scope != null)
                {
                    string message = "nh.sessionscope " + RoSession.Instance["nh.sessionscope"].GetHashCode() +
                                     " closing, took " + (DateTime.Now.Subtract(now).Seconds) + " seconds.";
                    RoLog.Instance.WriteToLog(message);
                    Console.WriteLine(message);
                    RoLog.Instance.WriteToLog("");
                    Console.WriteLine("");
                    scope.Flush();
                    scope.Dispose();
                    RoSession.Instance["nh.sessionscope"] = null;
                }
            }
            catch (Exception ex)
            {
                RoLog.Instance.WriteToLog("Problems with the session:" + ex.Message + "/r/n" + ex, TracedAttribute.ERROR);
            }
        }
예제 #7
0
        /// <summary>
        /// Saves the Character to the DB instantly.
        /// Blocking call.
        /// See: <see cref="SaveLater()"/>.
        /// When calling this method directly, make sure to set m_saving = true
        /// </summary>
        internal protected bool SaveNow()
        {
            if (!m_record.CanSave)
            {
                return(false);
            }

            if (DebugUtil.Dumps)
            {
                var writer = DebugUtil.GetTextWriter(m_client.Account);
                writer.WriteLine("Saving {0}...", Name);
            }

            try
            {
                if (m_record == null)
                {
                    throw new InvalidOperationException("Cannot save Character while not in world.");
                }

                UpdatePlayedTime();

                // always make sure that the values saved to DB, will not be influenced by buffs etc
                m_record.Race       = Race;
                m_record.Class      = Class;
                m_record.Gender     = Gender;
                m_record.Skin       = Skin;
                m_record.Face       = Facial;
                m_record.HairStyle  = HairStyle;
                m_record.HairColor  = HairColor;
                m_record.FacialHair = FacialHair;
                m_record.Outfit     = Outfit;
                m_record.Name       = Name;
                m_record.Level      = Level;
                if (m_Map != null)
                {
                    // only save position information if we are in world
                    m_record.PositionX   = Position.X;
                    m_record.PositionY   = Position.Y;
                    m_record.PositionZ   = Position.Z;
                    m_record.Orientation = Orientation;
                    m_record.MapId       = m_Map.Id;
                    m_record.InstanceId  = m_Map.InstanceId;
                    m_record.Zone        = ZoneId;
                }
                m_record.DisplayId = DisplayId;
                m_record.BindX     = m_bindLocation.Position.X;
                m_record.BindY     = m_bindLocation.Position.Y;
                m_record.BindZ     = m_bindLocation.Position.Z;
                m_record.BindMap   = m_bindLocation.MapId;
                m_record.BindZone  = m_bindLocation.ZoneId;

                m_record.Health         = Health;
                m_record.BaseHealth     = BaseHealth;
                m_record.Power          = Power;
                m_record.BasePower      = BasePower;
                m_record.Money          = Money;
                m_record.WatchedFaction = WatchedFaction;
                m_record.BaseStrength   = GetBaseStatValue(StatType.Strength);
                m_record.BaseStamina    = GetBaseStatValue(StatType.Stamina);
                m_record.BaseSpirit     = GetBaseStatValue(StatType.Spirit);
                m_record.BaseIntellect  = GetBaseStatValue(StatType.Intellect);
                m_record.BaseAgility    = GetBaseStatValue(StatType.Agility);
                m_record.Xp             = Experience;
                m_record.RestXp         = RestXp;

                // Honor and Arena
                m_record.KillsTotal             = KillsTotal;
                m_record.HonorToday             = HonorToday;
                m_record.HonorYesterday         = HonorYesterday;
                m_record.LifetimeHonorableKills = LifetimeHonorableKills;
                m_record.HonorPoints            = HonorPoints;
                m_record.ArenaPoints            = ArenaPoints;


                // Finished quests
                if (m_questLog.FinishedQuests.Count > 0)
                {
                    m_record.FinishedQuests = new uint[m_questLog.FinishedQuests.Count];
                    m_questLog.FinishedQuests.CopyTo(m_record.FinishedQuests);
                }

                // Taxis
                if (LatestTaxiPathNode != null && LatestTaxiPathNode.Next != null)
                {
                    m_record.NextTaxiVertexId = (int)LatestTaxiPathNode.Next.Value.Id;
                }
                else
                {
                    m_record.NextTaxiVertexId = 0;
                }

                // cooldowns & runes
                PlayerSpells.OnSave();

                // taxi mask
                m_record.TaxiMask = m_taxiNodeMask.Mask;

                if (m_record.Level > 1 &&
                    m_record.Level > Account.HighestCharLevel)
                {
                    // tell auth server about the new highest level
                    Account.HighestCharLevel = m_record.Level;
                }
            }
            catch (Exception e)
            {
                OnSaveFailed(e);
                return(false);
            }

            try
            {
                using (var saveScope = new SessionScope(FlushAction.Never))
                {
                    // Interface settings
                    Account.AccountData.Save();

                    // Items
                    var items = new List <ItemRecord>();
                    m_inventory.SaveAll(items);
                    m_record.UpdateItems(items);

                    // Skills
                    foreach (var skill in m_skills)
                    {
                        skill.Save();
                    }

                    // Pets
                    SaveEntourage();

                    // Quests
                    m_questLog.SaveQuests();

                    // Specs
                    foreach (var spec in SpecProfiles)
                    {
                        if (spec.IsDirty)
                        {
                            spec.Save();
                        }
                    }

                    // Achievements
                    m_achievements.SaveNow();

                    // Auras
                    m_auras.SaveAurasNow();

                    // General Character data
                    m_record.Save();

                    saveScope.Flush();
                }
                m_record.LastSaveTime = DateTime.Now;

                if (DebugUtil.Dumps)
                {
                    var writer = DebugUtil.GetTextWriter(m_client.Account);
                    writer.WriteLine("Saved {0} (Map: {1}).", Name, m_record.MapId);
                }

                return(true);
            }
            catch (Exception ex)
            {
                OnSaveFailed(ex);
                return(false);
            }
            finally
            {
            }
        }