views:

17

answers:

2

I'm writing a site on GAE-Java + Objectify which lets users create their own pages, with unique URL. I haven't been able to figure out a clear way to ensure that when two users try to claim the same url at the same time, only one user gets it.

This is what I'm trying to avoid:

  • User 1 does a check - its available
  • User 2 does a check - its available
  • Meanwhile, User 1 creates page and stores it.
  • User 2 creates a page and overwrites User 1.

Any ideas on how to solve this on GAE?

+1  A: 

Why not just run your code in a transaction? I don't see where the issue is. Do you have a sample of something you've tried and had problems with?

Peter Recore
No... I'm not really able to test this kind of scenario. I don't understand how it would work when user 1 and 2 are running in their own individual transactions.
Sudhir Jonathan
And there are no entity groups involved either...
Sudhir Jonathan
Entity groups are involved even if you don't think they are. Every entity is implicitly in a Entity Group even if you don't explicitly put it in one. and any transaction can only involve entities in one entity group.
Peter Recore
A: 

Found a clearer explanation in the python docs:

Attempts to get the entity of the model's kind with the given key name. If it exists, get_or_insert() simply returns it. If it doesn't exist, a new entity with the given kind, name, and parameters in kwds is created, stored, and returned. The get and subsequent (possible) put are wrapped in a transaction to ensure atomicity. Ths means that get_or_insert() will never overwrite an existing entity, and will insert a new entity if and only if no entity with the given kind and name exists. In other words, get_or_insert() is equivalent to this Python code:

def txn():
  entity = MyModel.get_by_key_name(key_name, parent=kwds.get('parent'))
  if entity is None:
    entity = MyModel(key_name=key_name, **kwds)
    entity.put()
  return entity

return db.run_in_transaction(txn)
Sudhir Jonathan