RavenDB 不能很好地处理事务范围 [英] RavenDB does not play nicely with Transaction Scope

查看:56
本文介绍了RavenDB 不能很好地处理事务范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望通过以下测试用例.但它没有通过 RavenDB.
如果我用 MsSql 创建完全相同的测试,它确实通过了.

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.

我尝试了各种方法,例如 waitfornonstaleresults 和 allownonauthetative..something 等.

I have tried various things, such as waitfornonstaleresults, and allownonauthetitative..something, etc etc.

推荐答案

为了扩展 Ayende 的答案,与 MSSQL 不同,文档存储(存储/加载操作)与索引存储分离,并且是唯一ACID.索引存储不是事务性的,并且在对文档存储的更新完成后异步更新.在这种情况下,在事务提交后使用分布式 (DTC) 事务.这是设计使然.

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();
}

您甚至可以使用:

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.

这篇关于RavenDB 不能很好地处理事务范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆