Transactions
The App Engine Datastore supports transactions . A transaction is an operation or set of operations that is atomic—either all of the operations in the transaction occur, or none of them occur. An application can perform multiple operations and calculations in a single transaction.
Contents
- Using transactions
- What can be done in a transaction
- Isolation and consistency
- Uses for transactions
- Transactional task enqueuing
Using transactions
A transaction is a set of Datastore operations on one or more entities. Each transaction is guaranteed to be atomic, which means that transactions are never partially applied. Either all of the operations in the transaction are applied, or none of them are applied. Transactions have a maximum duration of 60 seconds with a 10 second idle expiration time after 30 seconds.
An operation may fail when:
- Too many concurrent modifications are attempted on the same entity group.
- The transaction exceeds a resource limit.
- The Datastore encounters an internal error.
In all these cases, the Datastore API raises an exception.
Transactions are an optional feature of the Datastore; you're not required to use transactions to perform Datastore operations.
An application can execute a set of statements and Datastore operations in a single transaction, such that if any statement or operation raises an exception, none of the Datastore operations in the set are applied. The application defines the actions to perform in the transaction using a Python function. The application starts the transaction using one of the
run_in_transaction
methods, depending on whether the transaction accesses entities within a single entity group or whether the transaction is a
cross-group (XG) transaction
.(For XG transactions, see
Using Cross-Group Transactions
.)
For the common use case of a function that is only used within transactions, use the
@db.transactional
decorator:
from google.appengine.ext import db
class Accumulator(db.Model):
counter = db.IntegerProperty(default=0)
@db.transactional
def increment_counter(key, amount):
obj = db.get(key)
obj.counter += amount
obj.put()
q = db.GqlQuery("SELECT * FROM Accumulator")
acc = q.get()
increment_counter(acc.key(), 5)
If the function is sometimes called without a transaction, then instead of decorating it, call
db.run_in_transaction()
with the function as an argument:
from google.appengine.ext import db
class Accumulator(db.Model):
counter = db.IntegerProperty(default=0)
def increment_counter(key, amount):
obj = db.get(key)
obj.counter += amount
obj.put()
q = db.GqlQuery("SELECT * FROM Accumulator")
acc = q.get()
db.run_in_transaction(increment_counter, acc.key(), 5)
db.run_in_transaction()
takes the function object, and positional and keyword arguments to pass to the function. If the function returns a value,
db.run_in_transaction()
returns that value.
If the function returns, the transaction is committed, and all effects of Datastore operations are applied. If the function raises an exception, the transaction is "rolled back," and the effects are not applied. See the note above about exceptions.
When one transaction function is called from within another transaction,
@db.transactional
and
db.run_in_transaction()
have different default behavior.
@db.transactional
will allow this, and the inner transaction becomes the same transaction as the outer transaction. Calling
db.run_in_transaction()
attempts to "nest" another transaction within the existing transaction; but this behavior is not yet supported, and it raises
db.BadRequestError
. You can specify other behavior; see the function reference on
transaction options
for details.
Using Cross-Group (XG) Transactions
XG transactions , which operate across multiple entity groups, behave like the single-group transactions described above but don't fail if code tries to update entities from more than one entity group. To invoke a XG transaction, use transaction options.
Using
@db.transactional
:
from google.appengine.ext import db
@db.transactional(xg=True)
def make_things():
thing1 = Thing(a=3)
thing1.put()
thing2 = Thing(a=7)
thing2.put()
make_things()
Using
db.run_in_transaction_options
:
from google.appengine.ext import db
xg_on = db.create_transaction_options(xg=True)
def my_txn():
x = MyModel(a=3)
x.put()
y = MyModel(a=7)
y.put()
db.run_in_transaction_options(xg_on, my_txn)
What can be done in a transaction
The Datastore imposes restrictions on what can be done inside a single transaction.
All Datastore operations in a transaction must operate on entities in the same entity group if the transaction is a single group transaction, or on entities in a maximum of five entity groups if the transaction is a cross-group (XG) transaction . This includes querying for entities by ancestor, retrieving entities by key, updating entities, and deleting entities. Notice that each root entity belongs to a separate entity group, so a single transaction cannot create or operate on more than one root entity unless it is an XG transaction.
When two or more transactions simultaneously attempt to modify entities in one or more common entity groups, only the first transaction to commit its changes can succeed; all the others will fail on commit. Because of this design, using entity groups limits the number of concurrent writes you can do on any entity in the groups. When a transaction starts, App Engine uses optimistic concurrency control by checking the last update time for the entity groups used in the transaction. Upon commiting a transaction for the entity groups, App Engine again checks the last update time for the entity groups used in the transaction. If it has changed since our initial check, an exception is thrown. For an explanation of entity groups, see the Datastore Overview page.
An app can perform a query during a transaction, but only if it includes an ancestor filter. An app can also get Datastore entities by key during a transaction. You can prepare keys prior to the transaction, or you can build keys inside the transaction with key names or IDs.
All other Python code is allowed inside a transaction function. You can determine if the current scope is nested in a transaction function using
db.is_in_transaction()
. The transaction function should not have side effects other than the Datastore operations. The transaction function may be called multiple times if a Datastore operation fails due to another user updating entities in the entity group at the same time. When this happens, the Datastore API retries the transaction a fixed number of times. If they all fail,
db.run_in_transaction()
raises a
TransactionFailedError
. You can adjust the number of times the transaction is retried using
db.run_in_transaction_custom_retries()
instead of db.run_in_transaction().
Similarly, the transaction function should not have side effects that depend on the success of the transaction, unless the code that calls the transaction function knows to undo those effects. For example, if the transaction stores a new Datastore entity, saves the created entity's ID for later use, then the transaction fails, the saved ID does not refer to the intended entity because the entity's creation was rolled back. The calling code would have to be careful not to use the saved ID in this case.
Isolation and consistency
Outside of transactions, the Datastore's isolation level is closest to read committed. Inside of transactions, serializable isolation is enforced. Read the serializable isolation wiki and the Transaction Isolation article for more information on isolation levels.
In a transaction, all reads reflect the current, consistent state of the Datastore at the time the transaction started. This does not include previous puts and deletes inside the transaction. Queries and gets inside a transaction are guaranteed to see a single, consistent snapshot of the Datastore as of the beginning of the transaction. Entities and index rows in the transaction's entity group are fully updated so that queries return the complete, correct set of result entities, without the false positives or false negatives described in Transaction Isolation that can occur in queries outside of transactions.
This consistent snapshot view also extends to reads after writes inside transactions. Unlike with most databases, queries and gets inside a Datastore transaction do not see the results of previous writes inside that transaction. Specifically, if an entity is modified or deleted within a transaction, a query or get returns the original version of the entity as of the beginning of the transaction, or nothing if the entity did not exist then.
Uses for transactions
This example demonstrates one use of transactions: updating an entity with a new property value relative to its current value.
def increment_counter(key, amount):
obj = db.get(key)
obj.counter += amount
obj.put()
This requires a transaction because the value may be updated by another user after this code fetches the object, but before it saves the modified object. Without a transaction, the user's request uses the value of
count
prior to the other user's update, and the save overwrites the new value. With a
transaction, the application is told about the other user's update. If the entity is updated during the transaction, then the transaction is retried until all steps are completed without interruption.