我正在尝试添加一个事务,以防止创建具有相同属性的两个实体。在我的应用程序中,每次看到新的Google用户登录时,我都会创建一个新的播放器。当新的Google用户在几毫秒内完成多个json调用时,我当前的实现偶尔会创建重复的播放器。当我像这里注释的那样添加事务时,我会遇到各种错误。确保我永远不会使用相同的user_id创建两个播放器实体的最简单方法是什么?
def get_player_from_user(self, user_id):
player = Player.all().filter('user_id =', user_id).get()
if not player:
#This can result in duplicate players with the same user_id being created.
player = self.create_new_player(user_id)
#This is what I'm trying to do.
#player = db.run_in_transaction(self.create_new_player, user_id=user_id)
return player
def create_new_player(self,user_id):
#Check one more time for an existing user_id match.
player = Player.all().filter('user_id =', user_id).get()
if player:
return player
player = Player()
player.user_id = user.user_id()
player.put()
return player
答案 0 :(得分:4)
使用用户名(或其他标识符)作为密钥名称,并使用get_or_insert以事务方式创建新实体或返回现有实体。 Sahid的代码不起作用,因为没有交易,竞争条件仍然可能。
答案 1 :(得分:1)
也许你可以使用密钥名称,get_by_key_name比过滤更好。
def create_new_player(self,user_id):
key_name = "player/%s" % user_id
player = Player.get_by_key_name (key_name)
if player is None:
player = Player (key_name=key_name, user_id=user_id)
player.put ()
return player
根据Nick的最后评论,我更新了我的代码, 所以更好的解决方案是:
def create_new_player(self,user_id):
key_name = "player/%s" % user_id
player = Player.get_or_insert (key_name=key_name, user_id=user_id)
return player