gpt4 book ai didi

c# - RavenDB 不能很好地处理事务范围

转载 作者:行者123 更新时间:2023-11-30 19:40:07 25 4
gpt4 key购买 nike

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

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

我正在尝试编写一些工作原理相同的代码,无论我选择什么数据库,这只是我试图通过的测试用例的一个示例。

我已经尝试过各种方法,例如 waitfornonstaleresults 和 allownonauthetitative..something,等等。

最佳答案

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

因此,虽然您的代码不会按预期工作,但它应该:

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");

但是任何从索引存储中请求数据(查询)的东西都不会返回,因为数据甚至还没有永久添加到事务存储中,更不用说异步映射到索引存储中了。

关于c# - RavenDB 不能很好地处理事务范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24724614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com