@@ -102,8 +102,12 @@ nodeutil.inherits(Transaction, DatastoreRequest);
102102/**
103103 * Commit the remote transaction and finalize the current transaction instance.
104104 *
105+ * If the commit request fails, we will automatically rollback the transaction.
106+ *
105107 * @param {function } callback - The callback function.
106108 * @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}).
107111 * @param {object } callback.apiResponse - The full API response.
108112 *
109113 * @example
@@ -114,7 +118,7 @@ nodeutil.inherits(Transaction, DatastoreRequest);
114118 * });
115119 */
116120Transaction . prototype . commit = function ( callback ) {
117- var that = this ;
121+ var self = this ;
118122
119123 callback = callback || util . noop ;
120124
@@ -179,7 +183,7 @@ Transaction.prototype.commit = function(callback) {
179183 var method = modifiedEntity . method ;
180184 var args = modifiedEntity . args . reverse ( ) ;
181185
182- DatastoreRequest . prototype [ method ] . call ( that , args , util . noop ) ;
186+ DatastoreRequest . prototype [ method ] . call ( self , args , util . noop ) ;
183187 } ) ;
184188
185189 var protoOpts = {
@@ -195,14 +199,29 @@ Transaction.prototype.commit = function(callback) {
195199
196200 this . request_ ( protoOpts , reqOpts , function ( err , resp ) {
197201 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+ } ) ;
199218 return ;
200219 }
201220
202221 // The `callbacks` array was built previously. These are the callbacks that
203222 // handle the API response normally when using the DatastoreRequest.save and
204223 // .delete methods.
205- that . requestCallbacks_ . forEach ( function ( cb ) {
224+ self . requestCallbacks_ . forEach ( function ( cb ) {
206225 cb ( null , resp ) ;
207226 } ) ;
208227
@@ -277,10 +296,10 @@ Transaction.prototype.createQuery = function() {
277296 * });
278297 */
279298Transaction . prototype . delete = function ( entities ) {
280- var that = this ;
299+ var self = this ;
281300
282301 arrify ( entities ) . forEach ( function ( ent ) {
283- that . modifiedEntities_ . push ( {
302+ self . modifiedEntities_ . push ( {
284303 entity : {
285304 key : ent
286305 } ,
@@ -311,7 +330,7 @@ Transaction.prototype.delete = function(entities) {
311330 * });
312331 */
313332Transaction . prototype . rollback = function ( callback ) {
314- var that = this ;
333+ var self = this ;
315334
316335 callback = callback || util . noop ;
317336
@@ -321,7 +340,7 @@ Transaction.prototype.rollback = function(callback) {
321340 } ;
322341
323342 this . request_ ( protoOpts , { } , function ( err , resp ) {
324- that . skipCommit = true ;
343+ self . skipCommit = true ;
325344
326345 callback ( err || null , resp ) ;
327346 } ) ;
@@ -358,7 +377,7 @@ Transaction.prototype.rollback = function(callback) {
358377 * });
359378 */
360379Transaction . prototype . run = function ( callback ) {
361- var that = this ;
380+ var self = this ;
362381
363382 callback = callback || util . noop ;
364383
@@ -373,7 +392,7 @@ Transaction.prototype.run = function(callback) {
373392 return ;
374393 }
375394
376- that . id = resp . transaction ;
395+ self . id = resp . transaction ;
377396
378397 callback ( null , resp ) ;
379398 } ) ;
@@ -491,10 +510,10 @@ Transaction.prototype.run = function(callback) {
491510 * });
492511 */
493512Transaction . prototype . save = function ( entities ) {
494- var that = this ;
513+ var self = this ;
495514
496515 arrify ( entities ) . forEach ( function ( ent ) {
497- that . modifiedEntities_ . push ( {
516+ self . modifiedEntities_ . push ( {
498517 entity : {
499518 key : ent . key
500519 } ,
0 commit comments