Skip to content

Commit

Permalink
Merge pull request #234 from stephenplusplus/transaction-finalizing
Browse files Browse the repository at this point in the history
datastore: transaction: only commit once
  • Loading branch information
stephenplusplus committed Sep 19, 2014
2 parents ddc128c + df83eb0 commit 245fc17
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 10 additions & 2 deletions lib/datastore/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
};

/**
Expand Down Expand Up @@ -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));
};

/**
Expand Down
4 changes: 2 additions & 2 deletions regression/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 245fc17

Please sign in to comment.