即使现在,Objectify也不会同步存储 [英] Objectify doesn't store synchronously, even with now

查看:78
本文介绍了即使现在,Objectify也不会同步存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


我有下面的代码,为了显示我的问题,我简化了它:

  Date tempDate = new Date(); 


访问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);

访问访问=新访问(5L,7L,tempDate);
ofy()。save()。entity(visit).now();

日期tempDate2 = new Date();


访问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);

访问visit2 =新访问(5L,7L,tempDate2);
ofy()。save()。entity(visit2).now();


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

访问visit = ofy()。load()。类型(Visit.class)
.filter(Visit.USER_ID,userID)
.filter(Visit.VENUE_ID,venueID)
.filter(Visit.EXIT_DATE,null).first()。得到();

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

问题是,当我第二次执行所有这些操作时,并不总是发现数据存储中已经有访问(每隔一秒测试一次)。而我很困惑不是'现在()'是否假设马上存储它,然后才能继续运行?



非常感谢你的帮助,
Dan

解决方案

这是因为GAE Datastore 最终一致:当您创建/更新实体时,该方法立即返回,但索引仍在异步构建中。由于查询依赖于索引,因此您不会立即看到更改。

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


My servlet should perform the following : when a user registers at a venue, I check whether he's currently registered somewhere (even if it is the same venue) if so, unregister him and to register him once again.

I have the following code, which I've simplified in order to show my problem :

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

the problem is that when I preform all this operation for the second time, it not always finds that there is already a visit in the datastore (every second test, or so). And I'm confused isn't 'now()' suppose to store it right away and only then to continue running ?

Thank you very much for your help, Dan

解决方案

This is happening because GAE Datastore is eventually consistent: when you create/update an entity, the method returns immediately, but indexes are still being built asynchronously. Since queries rely on indexes you do not see the changes immediately.

Also, this has nothing to do with objectify - this is a property of the underlying datastore. The synchronous write (.now()) waits for the commit phase to finish , while asynchronous returns immediately even before that.

这篇关于即使现在,Objectify也不会同步存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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