Created
September 18, 2011 03:17
-
-
Save joliver/1224679 to your computer and use it in GitHub Desktop.
RavenDB TransactionScope Issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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