From df83eb0e5da00ca68eab8c0edb727b8f15984f51 Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Fri, 19 Sep 2014 13:51:28 -0400 Subject: [PATCH] datastore: transaction: only commit once --- lib/datastore/transaction.js | 12 ++++++++++-- regression/datastore.js | 4 ++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/datastore/transaction.js b/lib/datastore/transaction.js index 9736d6da669..db9db825a9d 100644 --- a/lib/datastore/transaction.js +++ b/lib/datastore/transaction.js @@ -312,12 +312,15 @@ Transaction.prototype.save = function(entities, callback) { callback(err); return; } + if (this.id) { + this.isFinalized = true; + } var autoInserted = (resp.mutation_result.insert_auto_id_key || []); autoInserted.forEach(function(key, index) { keys[insertIndexes[index]] = entity.keyFromKeyProto(key); }); callback(null, isMultipleRequest ? keys : keys[0]); - }); + }.bind(this)); }; /** @@ -356,7 +359,12 @@ Transaction.prototype.delete = function(keys, callback) { } req = new pb.CommitRequest(req); var res = pb.CommitResponse; - this.makeReq('commit', req, res, callback); + this.makeReq('commit', req, res, function(err, response) { + if (this.id) { + this.isFinalized = true; + } + callback(err, response); + }.bind(this)); }; /** diff --git a/regression/datastore.js b/regression/datastore.js index e81a163b0f3..ad9748866ff 100644 --- a/regression/datastore.js +++ b/regression/datastore.js @@ -375,13 +375,13 @@ describe('datastore', function() { url: 'www.google.com' }; ds.runInTransaction(function(t, tDone) { - ds.get(key, function(err, entity) { + t.get(key, function(err, entity) { assert.ifError(err); if (entity) { tDone(); return; } else { - ds.save({ key: key, data: obj }, function(err) { + t.save({ key: key, data: obj }, function(err) { assert.ifError(err); tDone(); return;