问题
I have the following test case that i expect to pass. But it does not pass with RavenDB.
If i create the exact same test with MsSql, it does pass.
var connectionString = "Url=http://localhost:8080";
var store = new DocumentStore();
store.ParseConnectionString(connectionString);
store.Initialize();
using (var scope = new TransactionScope())
using (var session = store.OpenSession())
{
session.Store(dog);
session.SaveChanges();
var dogs = session.Query<Dog>().Customize(x => x.WaitForNonStaleResults()).ToList();
Assert.AreEqual(1, dogs.Count);
scope.Complete();
}
I am trying to write some code that works the same, not matter what database i choose, and this is just an example of a test case i am trying to get to pass.
I have tried various things, such as waitfornonstaleresults, and allownonauthetitative..something, etc etc.
回答1:
To expand on Ayende's answer, unlike MSSQL, the Document Store (Store/Load operations) is separate from the Index Store and is the only thing that is ACID. The Index Store is not transactional, and is updated asynchronously after the update to the document store is finished. In this case, using a distributed (DTC) transaction, after the transaction has committed. This is by design.
Therefore, while your code will not work as you intend, this should:
var connectionString = "Url=http://localhost:8080";
var store = new DocumentStore();
store.ParseConnectionString(connectionString);
store.Initialize();
using (var scope = new TransactionScope())
using (var session = store.OpenSession())
{
Dog dog = new Dog { Id = "dogs/1" };
session.Store(dog);
session.SaveChanges();
var dog2 = session.Load<Dog>("dogs/1");
Assert.AreEqual(dog, dog2);
scope.Complete();
}
You may even be able to use:
Dog[] dogs = session.Advanced.LoadStartingWith<Dog>("dogs");
But anything asking for data from the index store (a Query) will not return because the data hasn't even been permanently added to the transactional store yet, much less asynchronously mapped into the index store.
回答2:
In RavenDB, until the DTC transaction has been committed, the data will not be added to any indexes.
来源:https://stackoverflow.com/questions/24724614/ravendb-does-not-play-nicely-with-transaction-scope