Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On the google app engine, how do I implement database Transactions?

I know that the way to handle DB transactionality on the app engine is to give different entities the same Parent(Entity Group) and to use db.run_in_transaction.

However, assume that I am not able to give two entities the same parent. How do I ensure that my DB updates occur in a transaction?

Is there a technical solution? If not, is there a pattern that I can apply?

Note: I am using Python.

like image 482
willem Avatar asked Jan 04 '10 10:01

willem


People also ask

How Google App Engine is used as platform as a service for development of any application?

App Engine is a fully managed, serverless platform for developing and hosting web applications at scale. You can choose from several popular languages, libraries, and frameworks to develop your apps, and then let App Engine take care of provisioning servers and scaling your app instances based on demand.

What is Datastore in Google App Engine?

Datastore is a NoSQL document database built for automatic scaling, high performance, and ease of application development. Datastore features include: Atomic transactions. Datastore can execute a set of operations where either all succeed, or none occur. High availability of reads and writes.

Which of the following do small Datastore operations include in Google App Engine?

The fees and free tier for Google Cloud Datastore are the same as the Datastore fees for App Engine. Small Datastore operations include calls to allocate Datastore IDs or keys-only queries. These operations are free.


2 Answers

As long as the entities belong to the same Group, this is not an issue. From the docs:

All datastore operations in a transaction must operate on entities in the same entity group. 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. For an explanation of entity groups, see Keys and Entity Groups.

There is also a nice article about Transaction Isolation in App Engine.

EDIT: If you need to update entities with different parents in the same transaction, you will need to implement a way to serialize the changes that were made by yourself and rollback manually if an exception is raised.

like image 195
jbochi Avatar answered Nov 01 '22 16:11

jbochi


If you want cross-entity-group transactions, you'll have to implement them yourself, or wait for a library to do them. I wrote an article a while ago about how to implement cross-entity-group transactions in the 'bank transfer' case; it may apply to your use-case too.

like image 27
Nick Johnson Avatar answered Nov 01 '22 15:11

Nick Johnson