Skip to content

Instantly share code, notes, and snippets.

@joliver
Created September 18, 2011 03:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joliver/1224679 to your computer and use it in GitHub Desktop.
Save joliver/1224679 to your computer and use it in GitHub Desktop.
RavenDB TransactionScope Issue
using System;
using System.Diagnostics;
using System.Linq;
using System.Transactions;
using Raven.Client;
using Raven.Client.Document;
using Raven.Client.Linq;
public class Program
{
private const TransactionScopeOption ScopeOption = TransactionScopeOption.Required;
private static void Main()
{
using (var store = new DocumentStore { Url = "http://localhost:8080" })
{
store.Initialize();
using (var outer = new TransactionScope(ScopeOption))
{
var id = Guid.NewGuid().ToString();
SaveObject(store, id);
var loaded = LoadObject(store, id);
if (loaded == null)
Debug.WriteLine("Shouldn't be null, but is when TransactionScopeOption is Required.");
else
Debug.WriteLine("Only populated when TransactionScopeOption is Suppress.");
outer.Complete();
}
}
}
private static void SaveObject(IDocumentStore store, string id)
{
using (var inner = new TransactionScope(ScopeOption))
using (var session = store.OpenSession())
{
var save = new SimpleObject(id);
session.Store(save);
session.SaveChanges();
inner.Complete();
}
}
private static SimpleObject LoadObject(IDocumentStore store, string id)
{
// because we're opening a subordinate TransactionScope which is attached to the parent
// it should have the ability to interact with the objects persisted by the parent or
// siblings, much the same way multiple SQL queries can interact with the same data
// provided they are participating in the same DB transaction regardless of isolation level.
using (var inner = new TransactionScope(ScopeOption))
using (var sesion = store.OpenSession())
{
var results = sesion.Query<SimpleObject>()
.Customize(x => x.WaitForNonStaleResults())
.Where(x => x.Id == id)
.ToArray();
inner.Complete();
// when there TransactionScope is suppressed, everything is great
// when TransactionScope is required, the query is unable to find the object persisted
// as part of the sibling transaction.
return results.FirstOrDefault();
}
}
}
public class SimpleObject
{
public string Id { get; set; }
public SimpleObject(string id)
{
this.Id = id;
}
}
using System;
using System.Diagnostics;
using System.Linq;
using System.Transactions;
using Raven.Client;
using Raven.Client.Document;
using Raven.Client.Linq;
public class Program
{
private static void Main()
{
using (var store = new DocumentStore { Url = "http://localhost:8080" })
{
store.Initialize();
using (var outer = new TransactionScope(TransactionScopeOption.Required))
{
var id = Guid.NewGuid().ToString();
SaveObject(store, id);
var loaded = LoadObject(store, id);
if (loaded == null)
Debug.WriteLine("Shouldn't be null, but is when TransactionScopeOption is Required.");
else
Debug.WriteLine("Only populated when TransactionScopeOption is Suppress.");
outer.Complete();
}
}
}
private static void SaveObject(IDocumentStore store, string id)
{
using (var session = store.OpenSession())
{
session.Store(new SimpleObject(id));
session.SaveChanges();
}
}
private static SimpleObject LoadObject(IDocumentStore store, string id)
{
using (var sesion = store.OpenSession())
return sesion.Query<SimpleObject>()
.Customize(x => x.WaitForNonStaleResults())
.Where(x => x.Id == id)
.ToArray()
.FirstOrDefault();
}
}
public class SimpleObject
{
public string Id { get; set; }
public SimpleObject(string id)
{
this.Id = id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment