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