Skip to content

Instantly share code, notes, and snippets.

@darkiri
Created July 9, 2012 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkiri/3078953 to your computer and use it in GitHub Desktop.
Save darkiri/3078953 to your computer and use it in GitHub Desktop.
Optimistic Concurrency in RavenDB
[Subject("Raven Experiments")]
public class when_updating_same_object_in_separate_sessions : raven_persistence_context
{
static Exception Exception;
Establish context = () => StoreSingleObject(new MyClass {Text = "this one"});
Because of = () => Exception = Catch.Exception(UpdateTwoObjectsInTwoSessions);
It should_throw_concurrency_exception = () => Exception.ShouldBeOfType<ConcurrencyException>();
static void UpdateTwoObjectsInTwoSessions()
{
using (var session = Store.OpenSession())
{
session.Advanced.UseOptimisticConcurrency = true;
session
.Query<MyClass>()
.First()
.Text = "still one";
AtTheSameTimeInAnotherSession();
session.SaveChanges();
}
}
static void AtTheSameTimeInAnotherSession()
{
using (var anotherSession = Store.OpenSession())
{
anotherSession
.Query<MyClass>()
.First()
.Text = "ha ha";
anotherSession.SaveChanges();
}
}
public class MyClass
{
public string Id { get; set; }
public string Text;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment