Exemplo n.º 1
0
        public void PersistThenMergeInSameTxnWithVersion()
        {
            ISession     s      = OpenSession();
            ITransaction tx     = s.BeginTransaction();
            var          entity = new VersionedEntity {
                Id = "test", Name = "test"
            };

            s.Persist(entity);
            s.Merge(new VersionedEntity {
                Id = "test", Name = "test-2"
            });

            try
            {
                // control operation...
                s.SaveOrUpdate(new VersionedEntity {
                    Id = "test", Name = "test-3"
                });
                Assert.Fail("saveOrUpdate() should fail here");
            }
            catch (NonUniqueObjectException)
            {
                // expected behavior
            }

            tx.Commit();
            s.Close();
        }
Exemplo n.º 2
0
        public async Task PersistThenMergeInSameTxnWithVersionAsync()
        {
            using (ISession s = OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    var entity = new VersionedEntity {
                        Id = "test", Name = "test"
                    };
                    await(s.PersistAsync(entity));
                    await(s.MergeAsync(new VersionedEntity {
                        Id = "test", Name = "test-2"
                    }));

                    try
                    {
                        // control operation...
                        await(s.SaveOrUpdateAsync(new VersionedEntity {
                            Id = "test", Name = "test-3"
                        }));
                        Assert.Fail("saveOrUpdate() should fail here");
                    }
                    catch (NonUniqueObjectException)
                    {
                        // expected behavior
                    }

                    await(tx.CommitAsync());
                }
        }
Exemplo n.º 3
0
        public void NoExtraUpdatesOnMergeVersionedWithCollection()
        {
            var parent = new VersionedEntity {
                Id = "parent", Name = "parent"
            };
            var child = new VersionedEntity {
                Id = "child", Name = "child"
            };

            using (ISession s = OpenSession())
                using (var t = s.BeginTransaction())
                {
                    parent.Children.Add(child);
                    child.Parent = parent;
                    s.Persist(parent);
                    t.Commit();
                }

            ClearCounts();

            // parent is now detached, but we have made no changes.  so attempt to merge it
            // into this new session; this should cause no updates...
            VersionedEntity mergedParent;

            using (var s = OpenSession())
                using (var t = s.BeginTransaction())
                {
                    mergedParent = (VersionedEntity)s.Merge(parent);
                    t.Commit();
                }

            AssertUpdateCount(0);
            AssertInsertCount(0);
            Assert.That(parent.Version, Is.EqualTo(mergedParent.Version), "unexpected parent version increment");
            IEnumerator <VersionedEntity> it = mergedParent.Children.GetEnumerator();

            it.MoveNext();
            VersionedEntity mergedChild = it.Current;

            Assert.That(child.Version, Is.EqualTo(mergedChild.Version), "unexpected child version increment");

            ///////////////////////////////////////////////////////////////////////
            // as a control measure, now update the node while it is detached and
            // make sure we get an update as a result...
            mergedParent.Name = "new name";
            mergedParent.Children.Add(new VersionedEntity {
                Id = "child2", Name = "new child"
            });
            using (var s = OpenSession())
                using (var t = s.BeginTransaction())
                {
                    parent = (VersionedEntity)s.Merge(mergedParent);
                    t.Commit();
                }
            AssertUpdateCount(1);
            AssertInsertCount(1);
            ///////////////////////////////////////////////////////////////////////
        }
Exemplo n.º 4
0
        public void MergeStaleVersionFails()
        {
            var entity = new VersionedEntity {
                Id = "entity", Name = "entity"
            };

            using (ISession s = OpenSession())
                using (s.BeginTransaction())
                {
                    s.Persist(entity);
                    s.Transaction.Commit();
                }

            // make the detached 'entity' reference stale...
            using (var s = OpenSession())
                using (s.BeginTransaction())
                {
                    var entity2 = s.Get <VersionedEntity>(entity.Id);
                    entity2.Name = "entity-name";
                    s.Transaction.Commit();
                }

            // now try to reattch it
            ISession s2 = null;

            try
            {
                s2 = OpenSession();
                s2.BeginTransaction();

                s2.Merge(entity);
                s2.Transaction.Commit();
                Assert.Fail("was expecting staleness error");
            }
            catch (StaleObjectStateException)
            {
                // expected outcome...
            }
            finally
            {
                if (s2 != null)
                {
                    s2.Transaction.Rollback();
                    s2.Close();
                }
                Cleanup();
            }
        }
Exemplo n.º 5
0
        public void NoExtraUpdatesOnMergeVersioned()
        {
            var entity = new VersionedEntity {
                Id = "entity", Name = "entity"
            };

            using (ISession s = OpenSession())
                using (var t = s.BeginTransaction())
                {
                    s.Persist(entity);
                    t.Commit();
                }

            ClearCounts();

            // entity is now detached, but we have made no changes.  so attempt to merge it
            // into this new session; this should cause no updates...
            VersionedEntity mergedEntity;

            using (var s = OpenSession())
                using (var t = s.BeginTransaction())
                {
                    mergedEntity = (VersionedEntity)s.Merge(entity);
                    t.Commit();
                }

            AssertUpdateCount(0);
            AssertInsertCount(0);
            Assert.That(entity.Version, Is.EqualTo(mergedEntity.Version), "unexpected version increment");

            ///////////////////////////////////////////////////////////////////////
            // as a control measure, now update the node while it is detached and
            // make sure we get an update as a result...
            entity.Name = "new name";
            using (var s = OpenSession())
                using (var t = s.BeginTransaction())
                {
                    entity = (VersionedEntity)s.Merge(entity);
                    t.Commit();
                }
            AssertUpdateCount(1);
            AssertInsertCount(0);
            ///////////////////////////////////////////////////////////////////////
        }
Exemplo n.º 6
0
        public async Task MergeStaleVersionFailsAsync()
        {
            var entity = new VersionedEntity {
                Id = "entity", Name = "entity"
            };

            using (ISession s = OpenSession())
                using (var t = s.BeginTransaction())
                {
                    await(s.PersistAsync(entity));
                    await(t.CommitAsync());
                }

            // make the detached 'entity' reference stale...
            using (var s = OpenSession())
                using (var t = s.BeginTransaction())
                {
                    var entity2 = await(s.GetAsync <VersionedEntity>(entity.Id));
                    entity2.Name = "entity-name";
                    await(t.CommitAsync());
                }

            // now try to reattach it
            using (var s2 = OpenSession())
                using (var t = s2.BeginTransaction())
                {
                    try
                    {
                        await(s2.MergeAsync(entity));
                        await(t.CommitAsync());
                        Assert.Fail("was expecting staleness error");
                    }
                    catch (StaleObjectStateException)
                    {
                        // expected outcome...
                    }
                    finally
                    {
                        await(t.RollbackAsync());
                        s2.Close();
                        await(CleanupAsync());
                    }
                }
        }
Exemplo n.º 7
0
		public void PersistThenMergeInSameTxnWithVersion()
		{
			ISession s = OpenSession();
			ITransaction tx = s.BeginTransaction();
			var entity = new VersionedEntity {Id = "test", Name = "test"};
			s.Persist(entity);
			s.Merge(new VersionedEntity {Id = "test", Name = "test-2"});

			try
			{
				// control operation...
				s.SaveOrUpdate(new VersionedEntity {Id = "test", Name = "test-3"});
				Assert.Fail("saveOrUpdate() should fail here");
			}
			catch (NonUniqueObjectException)
			{
				// expected behavior
			}

			tx.Commit();
			s.Close();
		}
Exemplo n.º 8
0
		public void NoExtraUpdatesOnMergeVersionedWithCollection()
		{
			ISession s = OpenSession();
			s.BeginTransaction();
			var parent = new VersionedEntity {Id = "parent", Name = "parent"};
			var child = new VersionedEntity {Id = "child", Name = "child"};
			parent.Children.Add(child);
			child.Parent = parent;
			s.Persist(parent);
			s.Transaction.Commit();
			s.Close();

			ClearCounts();

			// parent is now detached, but we have made no changes.  so attempt to merge it
			// into this new session; this should cause no updates...
			s = OpenSession();
			s.BeginTransaction();
			var mergedParent = (VersionedEntity) s.Merge(parent);
			s.Transaction.Commit();
			s.Close();

			AssertUpdateCount(0);
			AssertInsertCount(0);
			Assert.That(parent.Version, Is.EqualTo(mergedParent.Version), "unexpected parent version increment");
			IEnumerator<VersionedEntity> it = mergedParent.Children.GetEnumerator();
			it.MoveNext();
			VersionedEntity mergedChild = it.Current;
			Assert.That(child.Version, Is.EqualTo(mergedChild.Version), "unexpected child version increment");

			///////////////////////////////////////////////////////////////////////
			// as a control measure, now update the node while it is detached and
			// make sure we get an update as a result...
			mergedParent.Name = "new name";
			mergedParent.Children.Add(new VersionedEntity {Id = "child2", Name = "new child"});
			s = OpenSession();
			s.BeginTransaction();
			parent = (VersionedEntity) s.Merge(mergedParent);
			s.Transaction.Commit();
			s.Close();
			AssertUpdateCount(1);
			AssertInsertCount(1);
			///////////////////////////////////////////////////////////////////////
		}
Exemplo n.º 9
0
		public void NoExtraUpdatesOnMergeVersioned()
		{
			ISession s = OpenSession();
			s.BeginTransaction();
			var entity = new VersionedEntity {Id = "entity", Name = "entity"};
			s.Persist(entity);
			s.Transaction.Commit();
			s.Close();

			ClearCounts();

			// entity is now detached, but we have made no changes.  so attempt to merge it
			// into this new session; this should cause no updates...
			s = OpenSession();
			s.BeginTransaction();
			var mergedEntity = (VersionedEntity) s.Merge(entity);
			s.Transaction.Commit();
			s.Close();

			AssertUpdateCount(0);
			AssertInsertCount(0);
			Assert.That(entity.Version, Is.EqualTo(mergedEntity.Version), "unexpected version increment");

			///////////////////////////////////////////////////////////////////////
			// as a control measure, now update the node while it is detached and
			// make sure we get an update as a result...
			entity.Name = "new name";
			s = OpenSession();
			s.BeginTransaction();
			entity = (VersionedEntity) s.Merge(entity);
			s.Transaction.Commit();
			s.Close();
			AssertUpdateCount(1);
			AssertInsertCount(0);
			///////////////////////////////////////////////////////////////////////
		}
Exemplo n.º 10
0
		public void MergeStaleVersionFails()
		{
			ISession s = OpenSession();
			s.BeginTransaction();
			var entity = new VersionedEntity {Id = "entity", Name = "entity"};
			s.Persist(entity);
			s.Transaction.Commit();
			s.Close();

			// make the detached 'entity' reference stale...
			s = OpenSession();
			s.BeginTransaction();
			var entity2 = s.Get<VersionedEntity>(entity.Id);
			entity2.Name = "entity-name";
			s.Transaction.Commit();
			s.Close();

			// now try to reattch it
			s = OpenSession();
			s.BeginTransaction();
			try
			{
				s.Merge(entity);
				s.Transaction.Commit();
				Assert.Fail("was expecting staleness error");
			}
			catch (StaleObjectStateException)
			{
				// expected outcome...
			}
			finally
			{
				s.Transaction.Rollback();
				s.Close();
				Cleanup();
			}
		}