RavenDB Concurrent Update Using .NET Client

c#, ravendb

Solution

What you're talking about is optimistic concurrency. If you want to use that, you set

session.Advanced.UseOptimisticConcurrency = true;

By default, it's not set.

Here's a passing test that demonstrates this:

public class ConcurrentUpdates : LocalClientTest
{
    [Fact]
    public void ConcurrentUpdatesWillThrowAConcurrencyException()
    {
        using (var store = NewDocumentStore())
        {
            var originalPost = new Post { Text = "Nothing yet" };
            using (var s = store.OpenSession())
            {
                s.Store(originalPost);
                s.SaveChanges();
            }

            using (var session1 = store.OpenSession())
            using (var session2 = store.OpenSession())
            {
                session1.Advanced.UseOptimisticConcurrency = true;
                session2.Advanced.UseOptimisticConcurrency = true;

                var post1 = session1.Load<Post>(originalPost.Id);
                var post2 = session2.Load<Post>(originalPost.Id);

                post1.Text = "First change";
                post2.Text = "Second change";

                session1.SaveChanges();

                // Saving the second text will throw a concurrency exception
                Assert.Throws<ConcurrencyException>(() => session2.SaveChanges());
            }

            using (var s = store.OpenSession())
            {
                Assert.Equal("First change", s.Load<Post>(originalPost.Id).Text);
            }
        }
    }

    public class Post
    {
        public string Id { get; set; }
        public string Text { get; set; }
    }
}

Problem

I'd like to know what happens given the following scenario using the .net client. ``` using (IDocumentSession session = documentStore.OpenSession()) { thingToUpdate = session.Load<TUpdateThing>(id); // Modify thingToUpdate here // ** Someplace else the object is updated and saved. ** session.SaveChanges(); // What happens here? } ``` Will this automatically throw an error based on the etag having changed, or will this go off and overwrite the changes made by somebody else? I've seen some stuff on this in relation to the http api: http://ravendb.net/docs/http-api/http-api-comcurrency

Original source