Skip to content

Instantly share code, notes, and snippets.

@darkiri
Created July 9, 2012 21:22
Show Gist options
  • Save darkiri/3079035 to your computer and use it in GitHub Desktop.
Save darkiri/3079035 to your computer and use it in GitHub Desktop.
Optimistic Concurrency in RavenDB
[Subject("Raven Experiments")]
public class when_storing_two_objects_with_same_id_in_separate_sessions : raven_persistence_context
{
static Exception Exception;
Because of = () => Exception = Catch.Exception(CreateTwoObjectsInTwoSessions);
It should_throw_concurrency_exception = () => Exception.ShouldBeOfType<ConcurrencyException>();
static void CreateTwoObjectsInTwoSessions()
{
using (var session = Store.OpenSession())
{
StoreOnce(session, "first");
}
using (var session = Store.OpenSession())
{
StoreOnce(session, "another first");
}
}
static void StoreOnce(IDocumentSession session, string text)
{
session.Advanced.UseOptimisticConcurrency = true;
session.Store(new MyEvent {Revision = 1, Payload = text});
session.SaveChanges();
}
public class MyEvent
{
public int Id { get { return Revision; } }
public int Revision { get; set; }
public string Payload { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment