Google AppEngine中的get()
在什麼情況下會拋出EntityNotFoundException
?EntityNotFoundException的可能原因
是否真的只有如果在請求時請求的密鑰在數據存儲區中不存在,或者由於呼叫過程中的其他問題也可能發生,例如超時?
換句話說,如果我開始一個新的事務並在該交易中get()
拋出EntityNotFoundException
,可我是100%肯定的是,在同一事務中put()
將永遠覆蓋一些已經存在的項目,那不知何故錯過了get()
?
添加一些代碼:
private Entity getOrCreate(Key key, String initialData) {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Transaction txn = datastore.beginTransaction();
try {
Entity result;
try {
// Try to read existing entity
result = datastore.get(key);
} catch (EntityNotFoundException e) {
// If entity is not found, create and put a new one
result = new Entity(key);
result.setProperty("data", initialData);
datastore.put(result); // Could this EVER overwrite an existing entity?
}
txn.commit();
return result;
} finally {
if (txn.isActive()) txn.rollback();
}
}
難道這代碼不斷覆蓋由事故現有的實體由於一些機制,導致EntityNotFoundException 其他比這個實體真的不存在?
@MarkusA。讓現在更有意義:)。檢查我編輯的答案,可能會幫助你^^ – Patrice 2014-09-11 17:36:55
:)是的!謝謝! :)我刪除了我的評論,而是編輯了一些我的問題。這就是我最擔心的一個例子,即「最終一致性」案例。不幸的是,我不太清楚「get_by_id」是什麼意思。你的意思是一個特定的鍵而不是查詢的實際「get()」? – 2014-09-11 17:42:39
但基本上你是說沒有其他錯誤機制(讀取超時或其他)會導致錯誤? – 2014-09-11 17:49:22