How to save an entity in GAE datastore only if it does not exist while preventing race conditions

后端 未结 2 1236
渐次进展
渐次进展 2021-01-27 05:29

I am trying to save an entity to Google App Engine Datastore only if it does not exist, if it does the thread should not continue.

Main issue is: I want to prevent any r

2条回答
  •  既然无缘
    2021-01-27 06:19

    Assuming that the both threads are trying to save an entity with the same key, then this code:

    datastore.add(entity);
    

    will do what you want. If an entity already exists in datastore with the same key, then .add() will throw an exception.

提交回复
热议问题