2016-06-19 21 views
0

我目前正在使用Google應用引擎的內置分貝。看起來,當我運行put()向數據庫中插入一個元組時,即使該元組還沒有被完全插入,該函數也會返回。下面的代碼:Google App Engine數據庫在返回之前不會執行完畢

new_user = Users(username=username_input, hashed_password=get_hashed_password(username_input, password)) 
new_user.put() 

existing_user = None 
while not existing_user:  
     print "existing_user still not in DB" 

     #tries to get the user that was put into the DB 
     existing_user = db.GqlQuery("SELECT * FROM Users WHERE username=:username_input", username_input=username_input).get() 
print "existing_user in DB" 

當我運行這段代碼,我得到下面的輸出,

existing_user still not in DB 
existing_user still not in DB 
existing_user still not in DB 
existing_user still not in DB 
existing_user still not in DB 
existing_user still not in DB 
existing_user still not in DB 
existing_user still not in DB 
existing_user in DB 

爲什麼會出現這種情況? put()不應該在返回之前將元組放入數據庫嗎?

回答

1

數據存儲「最終一致」。這意味着在更改(插入/更新/刪除)之後,查詢可以(將)在短時間內返回舊數據視圖。使用SDK時,延遲時間爲1秒。在現場環境下,它通常是,比這個少,但偶爾會更多。要獲得數據的一致視圖,您需要直接使用它們的鍵獲取實體,或者將實體歸入父級(將它們放在實體組中),然後使用具有祖先約束的查詢。

您可以閱讀更多herehere

相關問題