简体   繁体   中英

How to I determine if an object exists for a given key in the Google AppEngine datastore using Java?

I'm trying to port the Sharding Counters example (code.google.com/appengine/articles/sharding_counters.html) to Java. The only problem is that the Java API does not have a call similar to Python's 'get_by_key_name'. This is the basic idea:

            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();

Unfortunately, this throws a JDOObjectNotFoundException the first time I run it. I could run a query to determine if a counter for a given name exists, but that is not transactional. Another thread could do the same and in the end both would create an object with the same key.

From what I understand, the only operations supported within a transaction (for the Java API) are get and put. So how can I lock an object by key that does not exist yet (ie no 'get') and make sure that I am the first and only one creating it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM