开发者

AppEngine Persist object delay?

My situation is as followed:

I have a list of Session in my table.

In my code i do a check if the last Session in the 开发者_StackOverflowdatastore has room for an aditional player.

If not create a new Session.

It goes a little something like this:

public Session getEligeableSession(){
    Session session;
    session = ds.getLastSession();
    if(session.numPlayers > 3){
        newSession();
        session = ds.getLastSession();
    }
    return session;
}

This works flawlessly on local but it doesn't on AppEngine's AppSpot.

On AppSpot i just get a filled up session back , i have to conclude from this that my code doesn't wait for the new session to get fully persisted and just get's the old session again.

Is my conclusion right? if so how would i go about solving this graciously.

Searching for this 'delay' has resulted in nothing on Google/SO which makes me believe there is no such a thing(aka it's synchronous) and there is a different fail in my code.(perhaps getting the last object from the list works differently on AppEngine)

Any and all help/advice is much appreciated.

Summary:

Session(Object) has(List) 4 Players(Object)

getLastSession() retrieves the whole list from the datastore and get the last one(size-1).

newSession() creates a new Session object and persists it.

EDIT per request:

public List<Session> getAll(){
    pm = PMF.get().getPersistenceManager();
    String query = "select from " + Session.class.getName();
    @SuppressWarnings("unchecked")
    List<Session> sessions = (List<Session>) pm.newQuery(query).execute();
    return sessions;
}


public Session getLastSession(){
    List<Session> sessions = getAll();
    Session session = sessions.get(sessions.size()-1);
    session.activeGame = session.numPlayers == 4 ?  true : false;
    pm.close();
    return session;
}


    public int insert(Session session){
    pm = PMF.get().getPersistenceManager();
    int status = 0;
    try {
        pm.makePersistent(session);
    } finally {
        pm.close();
    }
    return status;
}

The code seems 'Reasonable', no funny stuff happening afaik. I'm using the master-slave option.


Just a guess here, because I don't know how you implemented your methods, but if you're using two different instances of PersistenceManager in two different servlets (one to store the object, and one to retrieve it), you might be accessing two different servers immediately one after the other, not giving enough time to replicate the data throughout the datastore servers.

Take a look also at the first reply on this thread for an alternative explanation (although, depending on how newSession(); works, it might not be the case):

http://groups.google.com/group/google-appengine/browse_thread/thread/2b4f6ec57031f53c/fc967c034e4a113f

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜