Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objects not saving using Objectify and GAE

I'm trying to save an object and verify that it is saved right after, and it doesn't seem to be working.

Here is my object

import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;

@Entity
public class PlayerGroup {
    @Id public String n;//sharks
    public ArrayList<String> m;//members [39393,23932932,3223]

}

Here is the code for saving then trying to load right after.

        playerGroup = new PlayerGroup();
        playerGroup.n = reqPlayerGroup.n;
        playerGroup.m = reqPlayerGroup.m;
        ofy().save().entity(playerGroup).now();
        response.i = playerGroup;
        PlayerGroup newOne = ofy().load().type(PlayerGroup.class).id(reqPlayerGroup.n).get();

But the "newOne" object is null. Even though I just got done saving it. What am I doing wrong?

--Update-- If I try later (like minutes later) sometimes I do see the object, but not right after saving. Does this have to do with the high replication storage?

like image 457
b-ryce Avatar asked Feb 01 '13 17:02

b-ryce


1 Answers

Had the same behavior some time ago and asked a question on google groups - objectify

Here the answer I got :

You are seeing the eventual consistency of the High-Replication 
Datastore.  There has been a lot of discussion of this exact subject 
on the Objecify list in google groups , including several links to the 
Google documentation on the subject. 

Basically, any kind of query which does not include an ancestor() may 
return results from a stale view of the datastore. 

Jeff 

I also got another good answer to deal with the behavior

For deletes, query for keys and then batch-get the entities. Make sure your gets are set to strong consistency (though I believe this is the default). The batch-get should return null for the deleted entities. When adding, it gets a little trickier. Index updates can take a few seconds. AFAIK, there are three ways out of this: 1; Use precomputed results (avoiding the query entirely). If your next view is the user's recently created entities, keep a list of those keys in the user entity, and update that list when a new entity is created. That list will always be fresh, no query required. Besides avoiding stale indexes, this also speeds up your app. The more you result sets you can reliably manage, the more queries you can avoid.

2; Hide the latency by "enhancing" the query results with the recently added entities. Depending on the rate at which you're adding entities, either inject only the most recent key, or combine this with the solution in 1.

3; Hide the latency by taking the user through some unaffected views before landing on your query-based view. This strategy definitely has a smell over it. You need to make sure those extra steps are relevant to the user, or you'll give a poor experience.

Butterflies, Joakim

You can read it all here:

How come If I dont use async api after I'm deleting an object i still get it in a query that is being done right after the delete or not getting it right after I add one


Another good answer to a similar question : Objectify doesn't store synchronously, even with now

like image 51
Daniel Avatar answered Sep 22 '22 08:09

Daniel