Skip to content

Notes on NDB Datastore Transactions

Kevin Thomas edited this page Mar 23, 2021 · 1 revision

What is a transaction?

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.

How to identify and use transactions?

In the Oppia codebase, you can find transactions used in several places. Some examples are updating exploration statistics and user ratings, sending emails, and in MapReduce jobs.

When a function needs to be run as a transaction, use the annotation @transaction_services.run_in_transaction_wrapper above the function.

transactions

Search for this annotation in the codebase for more examples.

The maximum number of models that can be operated on is limited to 25, so for example you can put_multi at max 25 models in one NDB datastore transaction (see source). If you need to operate on more than 25 models, look into core/domain/wipeout_service.py. In general, always write code to be rerunnable so that if one run fails, the subsequent run can take over and complete the task.

Core documentation


Developing Oppia


Developer Reference

Clone this wiki locally