![](/img/trans.png)
[英]How to save unicode string into datastore google appengine using java
[英]How to I determine if an object exists for a given key in the Google AppEngine datastore using Java?
我正在嘗試將分片計數器示例(code.google.com/appengine/articles/sharding_counters.html)移植到Java。 唯一的問題是Java API沒有類似於Python的“ get_by_key_name”的調用。 這是基本思想:
Transaction tx = pm.currentTransaction();
Key key = KeyFactory.createKey(CounterShard.class.getSimpleName(), counter + randomIndex);
CounterShard shard = pm.getObjectById(CounterShard.class, key);
if (shard == null) { // API does not work this way...
shard = new CounterShard(key, counter);
}
shard.increment();
pm.makePersistent(shard);
tx.commit();
不幸的是,這是在我第一次運行它時拋出JDOObjectNotFoundException。 我可以運行查詢以確定給定名稱的計數器是否存在,但這不是事務性的。 另一個線程可以做同樣的事情,最后兩個線程都將創建一個具有相同鍵的對象。
據我了解,事務(對於Java API)支持的唯一操作是獲取和放置。 那么,如何通過尚不存在的鍵(即沒有“獲取”)鎖定對象,並確保我是第一個也是唯一創建它的對象?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.