Do datastore transactions need rollback when saving or deleting?

396 views Asked by At

In the example for dataset.runInTransaction (link), there is explicit error handling that occurs on transaction.get(key,callback).

But on other operations, such as transaction.save(entity) or transaction.delete(key), there are no callbacks, so really no way to handle errors. For example:

dataset.runInTransaction(function(transaction, done) {
  transaction.save({
      key: dataset.key(['Company', 123]),
      data: {}
  });
  transaction.delete(dataset.key(['Company', 456]));
  done();
}, function(err, apiResponse) {});

Does this mean there is no need to explicitly rollback the transaction?

2

There are 2 answers

0
JasonS On

I spent a lot of back-and-forth with the gcloud-node contributors on the issue tracker:

  1. https://github.com/GoogleCloudPlatform/gcloud-node/issues/1120
  2. https://github.com/GoogleCloudPlatform/gcloud-node/issues/633

basically all of the edits are done at the same time (once done() is called) so if any fail, the entire transaction will be aborted at that time.

what was confusing is that some operations like transaction.get() do have callbacks. Basically the version of the entity returned by transaction.get() needs to match the version before the .save() or .delete() occurs (which again, takes place when done() is called) if the version doesn't match, the transaction is automatically aborted.

using transaction.rollback() is still helpfull if something in your .get() call doesn't match what your code expects.

0
Ashish Anand On

I was also trying to look up for the same issue but found the below question helpful.

Please have a look here