Does an AppEngine transaction need to get and put in order to be useful?

Two sample code (simplified):


.get outside the transaction (the object from .get passed to the transaction function)

@db.transactional def update_object_1_txn(obj, new_value): obj.prop1 = new_value return obj.put() 

.get inside a transaction

 @db.transactional def update_object2_txn(obj_key, new_value): obj = db.get(obj_key) obj.prop1 = new_value return obj.put() 

Is the first example logically sound? Is the deal useful at all, does it provide anything? I am trying to better understand appengine transactions. Would you choose the second option to prevent concurrent modifications for this object?

+4
source share
1 answer

To answer your question in one word: yes, your second example is a way to do this. Within the boundaries of the transaction, you get some data, change it and fix the new value.

Your first is not wrong because you are not reading obj . Therefore, even if it may not have the same meaning as before, you will not notice. In other words: as written, your examples do not very well illustrate the point of the transaction, which is usually called the "test and set." See a Good Wikipedia article: http://en.wikipedia.org/wiki/Test-and-set

More specifically for GAE, as defined in GAE docs , transaction:

A set of Datastore operations for one or more objects. Each transaction is guaranteed to be atomic, which means that transactions are never partially applied. Either all operations in the transaction are used, or none of them are used.

which tells you that it should not be easy for testing and installation, it can also be useful for providing batch commit of several objects, etc.

+4
source

Source: https://habr.com/ru/post/1495653/


All Articles