1

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?

  • While I also wonder if calling .rollback() is required on errored transactions, you are mistaken about .save() not having a callback. See https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.27.0/datastore/transaction?method=save – JasonS Feb 01 '16 at 22:04
  • I believe transaction.save does not, but dataset.save does take a callback as last argument. – hector rovira Feb 11 '16 at 01:33
  • you are right, i filed an issue with their docs and they are fixing it. – JasonS Feb 11 '16 at 20:38

2 Answers2

1

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

Please have a look here

Community
  • 1
  • 1
0

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.

JasonS
  • 7,443
  • 5
  • 41
  • 61