1977
1977

Reputation: 2910

Is there optimistic locking in Google Datastore node api?

I need to implement optimistic on my google datastore kind.

I am using my own key rather than a generated one and using the google datastore node.js api.

As insert, update, upsert all seem to de delegated to the save() method ( as per the docs) I can easily end up overwriting data if I am not careful.

So basically I have to try a 'read' before doing any insert or update and then after that I have to be careful to 'overwrite' the correct properties before calling save().

If I manage to read and then properly 'overwrite' the correct fields I still need to be sure that I am the latest writer to that entity so I would need some sort of optimistic locking with a version/timestamp field. I have read somewhere that this is in built in but its not so explicit in the official docs at https://googlecloudplatform.github.io/google-cloud-node/#/docs/datastore/0.7.1/datastore?method=insert

can someone shed some light on this ?

Upvotes: 0

Views: 721

Answers (1)

Dan McGrath
Dan McGrath

Reputation: 42018

Cloud Datastore transactions use optimistic locking.

Process A:

  • T1 -> Start Transaction
  • T2 -> Read Entity X
  • T4 -> Write Entity X
  • T5 -> Commit Transaction

Process B:

  • T3 -> Write Entity X

In the above scenario, since Cloud Datastore uses optimistic locking, Process A's transaction will fail since entity X was written between the Read and the Write.

Note: insert, upsert, update all map to save, but have the method explicitly passed along via the optional method string so the correct type of write is performed: source

Upvotes: 1

Related Questions