Tiwari
Tiwari

Reputation: 1024

Concurent Access to datastore in app engine

i want to know if db.run_in_transaction() acts as a lock for Data store operations and helps in case of concurrent access on same entity.

Does in following code it is guarantied that a concurrent access will not cause a race and instead of creating new entity it will not do a over-write

Is db.run_in_transaction() correct/best way to do so

in following code i m trying to create new unique entity with following code

def txn(charmer=None):
    new = None
    key = my_magic() + random_part()
    sk = Snake.get_by_name(key)
    if not sk:
       new = Snake(key_name=key, charmer= charmer)
       new.put()
    return new
db.run_in_transaction(txn, charmer) 

Upvotes: 1

Views: 240

Answers (2)

Sudhir Jonathan
Sudhir Jonathan

Reputation: 17516

What you've done is right and sort of duplicates the Model.get_or_insert, like Robert already explained.

I don't know if this can be called a 'lock'... the way this works is optimistic concurrency - the operation will execute assuming that no one else is trying to do the same thing at the same time, and if someone is, it will give you an exception. You'll need to figure out what you want to do in that case. Maybe ask the user to choose a new name?

Upvotes: 0

Robert Kluin
Robert Kluin

Reputation: 8292

That is a safe method. Should the same name get generated twice, only one entity would be created.

It sounds like you have already looked at the transactions documentation. There is also a more detailed description.

Check out the docs (specifically the equivalent code) on Model.get_or_insert, it answers exactly the question you are asking:

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.

Upvotes: 4

Related Questions