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); } }
/// <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 { } }
/// <summary> /// Mains the specified args. /// </summary> /// <param name="args">The args.</param> public static void Main(string[] args) { string option; if (args.Length == 0) { Console.WriteLine("Please specify a valid option..."); Console.WriteLine("Valid parameter values are: -update|-bootstrap|-tests [-show_workflow]"); option = Console.ReadLine(); } else { option = args[0]; } Console.WriteLine("Initializing framework..."); var source = new XmlConfigurationSource(@"SchemaConfig.xml"); RedCelularActiveRecordBase<Product>.Initialize(source); __sessionScope = new SessionScope(); switch (option) { case "-update": UpdateSchema(); break; case "-bootstrap": BootstrapDatabase(); break; case "-fill": // The database needs to be already created (bootstrap) FillDatabase(); break; case "-tests": PrepareTestsDatabase(); break; default: Console.WriteLine("Valid parameter values are: -update|-bootstrap|-tests [-show_workflow]"); Console.ReadKey(); return; } __sessionScope.Flush(); }
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); } }
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()); }