@@ -102,8 +102,12 @@ nodeutil.inherits(Transaction, DatastoreRequest);
102
102
/**
103
103
* Commit the remote transaction and finalize the current transaction instance.
104
104
*
105
+ * If the commit request fails, we will automatically rollback the transaction.
106
+ *
105
107
* @param {function } callback - The callback function.
106
108
* @param {?error } callback.err - An error returned while making this request.
109
+ * If the commit fails, we automatically try to rollback the transaction (see
110
+ * {module:datastore/transaction#rollback}).
107
111
* @param {object } callback.apiResponse - The full API response.
108
112
*
109
113
* @example
@@ -114,7 +118,7 @@ nodeutil.inherits(Transaction, DatastoreRequest);
114
118
* });
115
119
*/
116
120
Transaction . prototype . commit = function ( callback ) {
117
- var that = this ;
121
+ var self = this ;
118
122
119
123
callback = callback || util . noop ;
120
124
@@ -179,7 +183,7 @@ Transaction.prototype.commit = function(callback) {
179
183
var method = modifiedEntity . method ;
180
184
var args = modifiedEntity . args . reverse ( ) ;
181
185
182
- DatastoreRequest . prototype [ method ] . call ( that , args , util . noop ) ;
186
+ DatastoreRequest . prototype [ method ] . call ( self , args , util . noop ) ;
183
187
} ) ;
184
188
185
189
var protoOpts = {
@@ -195,14 +199,29 @@ Transaction.prototype.commit = function(callback) {
195
199
196
200
this . request_ ( protoOpts , reqOpts , function ( err , resp ) {
197
201
if ( err ) {
198
- callback ( err , resp ) ;
202
+ // Rollback automatically for the user.
203
+ self . rollback ( function ( err ) {
204
+ if ( err ) {
205
+ err . message = [
206
+ 'The commit was not successful and the transaction could not be' ,
207
+ 'rolled back. You may want to try calling transaction.rollback().' ,
208
+ '\n' ,
209
+ '\n' ,
210
+ err . message
211
+ ] . join ( '' ) ;
212
+ }
213
+
214
+ // Provide the response items from the failed commit to the user. A
215
+ // successful rollback should be transparent.
216
+ callback ( err , resp ) ;
217
+ } ) ;
199
218
return ;
200
219
}
201
220
202
221
// The `callbacks` array was built previously. These are the callbacks that
203
222
// handle the API response normally when using the DatastoreRequest.save and
204
223
// .delete methods.
205
- that . requestCallbacks_ . forEach ( function ( cb ) {
224
+ self . requestCallbacks_ . forEach ( function ( cb ) {
206
225
cb ( null , resp ) ;
207
226
} ) ;
208
227
@@ -277,10 +296,10 @@ Transaction.prototype.createQuery = function() {
277
296
* });
278
297
*/
279
298
Transaction . prototype . delete = function ( entities ) {
280
- var that = this ;
299
+ var self = this ;
281
300
282
301
arrify ( entities ) . forEach ( function ( ent ) {
283
- that . modifiedEntities_ . push ( {
302
+ self . modifiedEntities_ . push ( {
284
303
entity : {
285
304
key : ent
286
305
} ,
@@ -311,7 +330,7 @@ Transaction.prototype.delete = function(entities) {
311
330
* });
312
331
*/
313
332
Transaction . prototype . rollback = function ( callback ) {
314
- var that = this ;
333
+ var self = this ;
315
334
316
335
callback = callback || util . noop ;
317
336
@@ -321,7 +340,7 @@ Transaction.prototype.rollback = function(callback) {
321
340
} ;
322
341
323
342
this . request_ ( protoOpts , { } , function ( err , resp ) {
324
- that . skipCommit = true ;
343
+ self . skipCommit = true ;
325
344
326
345
callback ( err || null , resp ) ;
327
346
} ) ;
@@ -358,7 +377,7 @@ Transaction.prototype.rollback = function(callback) {
358
377
* });
359
378
*/
360
379
Transaction . prototype . run = function ( callback ) {
361
- var that = this ;
380
+ var self = this ;
362
381
363
382
callback = callback || util . noop ;
364
383
@@ -373,7 +392,7 @@ Transaction.prototype.run = function(callback) {
373
392
return ;
374
393
}
375
394
376
- that . id = resp . transaction ;
395
+ self . id = resp . transaction ;
377
396
378
397
callback ( null , resp ) ;
379
398
} ) ;
@@ -491,10 +510,10 @@ Transaction.prototype.run = function(callback) {
491
510
* });
492
511
*/
493
512
Transaction . prototype . save = function ( entities ) {
494
- var that = this ;
513
+ var self = this ;
495
514
496
515
arrify ( entities ) . forEach ( function ( ent ) {
497
- that . modifiedEntities_ . push ( {
516
+ self . modifiedEntities_ . push ( {
498
517
entity : {
499
518
key : ent . key
500
519
} ,
0 commit comments