Search code examples
ravendboptimistic-lockingoptimistic-concurrency

RavenDB UseOptimisticConcurrency in Config?


Is there a way to set optimistic concurrency to true in Raven.Server.exe.config? Or, can it somehow be applied at the database level? On RavenDB's site, I see a couple of mentions of setting UseOptimisticConcurrency = true, but it looks like it's at the session level within the code:

public void Save<T>(T objectToSave)
{
    using (IDocumentSession session = Database.OpenSession())
    {
        session.Advanced.UseOptimisticConcurrency = true;  // This is the setting
        Guid eTag = (Guid)session.Advanced.GetEtagFor(objectToSave);
        session.Store(objectToSave, eTag);
        session.SaveChanges();
    }
}

I would like to know if it that setting exists somewhere server-wide, so it doesn't need to be specified for each session in code.

Edit: The code above produces the following error. Trying to find out why...

enter image description here

Edit 2: Ok, I'm making progress. If I retrieve the object, and call GetEtagFor() all within the same session, then I get a valid eTag. So I guess my main question now is: Is the correct way to use a session, within a client UI, to open the session once when the app starts, and then close it at the end? And... What's the correct way to store the eTag? The way it's coded above, the eTag is retrieved right before storing, which I would imagine is the wrong way to do it. I'm guessing the eTag should be retrieved when the object is first retrieved. But when we originally get a list of objects, should we have to loop through each and call GetEtagFor() on them? Doesn't seem right...


Solution

  • Bob, No, UseOptimisticConcurrency is something that you need to setup when you open the session. And NO, a single session per the entire application is the wrong thing to do. See this article for more details on session management:

    http://archive.msdn.microsoft.com/mag200912NHibernate

    It talks about NHibernate, but the session management parts applies to ravendb as well.