即使现在,Objectify 也不会同步存储

2024-01-01

我的 servlet 应该执行以下操作:当用户在某个场所注册时,我检查他当前是否在某个地方注册(即使是同一个场所),如果是,则注销他并再次注册他。

我有以下代码,我对其进行了简化以显示我的问题:

    Date tempDate = new Date();


    Visit v = ofy().load().type(Visit.class)
            .filter(Visit.USER_ID, 5L)
            .filter(Visit.EXIT_DATE, null).first().get();

    if(v != null)
        exitVenue(5L, 7L, tempDate);

    Visit visit = new Visit(5L, 7L, tempDate);      
    ofy().save().entity(visit).now();

    Date tempDate2 = new Date();


    Visit v2 = ofy().load().type(Visit.class)
            .filter(Visit.USER_ID, 5L)
            .filter(Visit.EXIT_DATE, null).first().get();

    if(v2 != null)
        exitVenue(5L, 7L, tempDate2);

    Visit visit2 = new Visit(5L, 7L, tempDate2);        
    ofy().save().entity(visit2).now();
}


public void exitVenue(Long userID, Long venueID, Date exitDate) {

    Visit visit = ofy().load().type(Visit.class)
            .filter(Visit.USER_ID, userID)
            .filter(Visit.VENUE_ID, venueID)
            .filter(Visit.EXIT_DATE, null).first().get();

    if(visit == null){
        log.info("ERROR : User " + userID + " exited venue " + venueID + ", but Visit was not found");
        return;
    }
    visit.setExitDate(exitDate);

    ofy().save().entity(visit).now();
}

问题是,当我第二次执行所有这些操作时,它并不总是发现数据存储中已经存在访问(每隔一次测试,等等)。我很困惑,'now()' 不是应该立即存储它然后才继续运行吗?

非常感谢您的帮助, 担


发生这种情况是因为 GAE 数据存储最终一致 https://developers.google.com/appengine/docs/java/datastore/structuring_for_strong_consistency:当您创建/更新实体时,该方法立即返回,但是索引仍在异步构建 https://developers.google.com/appengine/articles/life_of_write。由于查询依赖于索引,因此您不会立即看到更改。

此外,这与 objectify 无关 - 这是底层数据存储的属性。同步写入(.now())等待提交阶段完成,而异步则在此之前立即返回。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

即使现在,Objectify 也不会同步存储 的相关文章

随机推荐