I am trying to add a transaction to keep from creating two entities with the same attribute. In my application, I am creating a new Player each time I see a new Google user logged in. My current implementation occasionally creates duplicate players when multiple json calls are made by a new Google user within a few milliseconds. When I add the transaction like the one commented out here, I get various errors. What is the easiest way to ensure that I never create two player entities with the same 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