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?
I spent a lot of back-and-forth with the
gcloud-node
contributors on the issue tracker: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 bytransaction.get()
needs to match the version before the.save()
or.delete()
occurs (which again, takes place whendone()
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.