写入后立即从 RavenDb 读取会返回不一致的数据

发布于 2024-12-13 06:54:29 字数 1036 浏览 2 评论 0原文

我有一个协调过程,其中后台线程定期从外部 Web 服务检索对象 ID 列表,并尝试将丢失的实体添加到嵌入式 RavenDb 数据库中。执行此过程的循环如下:

foreach (var pageId in listOfPageIds)
{
    if ( _contentService.GetPageByPageId(pageId) == null)
    {
        _contentService.AddPage(pageId);
    }
}

GetPageByPageId()AddPage() 的实现如下:

public Page GetPageByPageId(string pageId)
{
    using (var session = DocumentStore.OpenSession())
    {
        return session.Query<Page>().FirstOrDefault(page => page.PageId == pageId);
    }
}

public bool AddPage(string pageId)
{
    var page = GetPageByPageId(pageId);
    if (page != null)
    {
        return false;
    }
    using (var session = DocumentStore.OpenSession())
    {
        var newPage = new Page() {PageId = pageId};
        session.Store(newPage);
        session.SaveChanges();
    }
    return true;
}

问题是,如果列表有重复的 id,一旦它添加第一个 id 并再次检查该 id,结果返回为空。就好像缺少注册新添加实体的最终确定步骤一样。如果我稍后从不同的线程查询该集合,则会返回具有给定 id 的实体。谁能看出这里有什么问题吗?

谢谢,

I have a reconciliation process where by a background thread that periodically retrieves a list of object ids from an external webservice and attempts to add the missing entities to an embedded RavenDb database. The loop that performs this process is the following:

foreach (var pageId in listOfPageIds)
{
    if ( _contentService.GetPageByPageId(pageId) == null)
    {
        _contentService.AddPage(pageId);
    }
}

the implementation of the GetPageByPageId() and AddPage() are as follows:

public Page GetPageByPageId(string pageId)
{
    using (var session = DocumentStore.OpenSession())
    {
        return session.Query<Page>().FirstOrDefault(page => page.PageId == pageId);
    }
}

public bool AddPage(string pageId)
{
    var page = GetPageByPageId(pageId);
    if (page != null)
    {
        return false;
    }
    using (var session = DocumentStore.OpenSession())
    {
        var newPage = new Page() {PageId = pageId};
        session.Store(newPage);
        session.SaveChanges();
    }
    return true;
}

The problem is that if the list has duplicate ids, once it adds the first id and checks for that id again, the result comes back as empty. It is as if a finalization step is missing that would register the newly added entity. If I query the set from a different thread at a later time, the entity with that given id is returned. Can anyone see what the issue is here?

thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终陌 2024-12-20 06:54:29

这是Raven采用的最终一致性模型的结果。写入导致的索引更新是异步发生的,因此不久后执行读取可能会返回过时的结果。您可以更改查询以获得不陈旧的结果,如下所示:

session.Query<Page>().Customize(x => x.WaitForNonStaleResultsAsOfNow()).FirstOrDefault(page => page.PageId == pageId)

还有一些其他选项Ayende这篇博文中涵盖了

This is a result of the eventual consistency model that Raven adopts. Updates to the indexes as a result of writes happen asynchronously and therefore it is possible that performing a read shortly after will return stale results. You can alter your query to get non-stale results like this:

session.Query<Page>().Customize(x => x.WaitForNonStaleResultsAsOfNow()).FirstOrDefault(page => page.PageId == pageId)

There's a couple of other options that Ayende covers in this blog post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文