Skip to content

Commit ca0d96c

Browse files
callmehiphopstephenplusplus
authored andcommitted
spanner: make getTransaction public (#2344)
1 parent cbbbf6c commit ca0d96c

5 files changed

Lines changed: 103 additions & 74 deletions

File tree

packages/spanner/src/database.js

Lines changed: 72 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,77 @@ Database.prototype.getSchema = function(callback) {
399399
});
400400
};
401401

402+
/**
403+
* Get a read/write ready Transaction object.
404+
*
405+
* @param {object=} options - [Transaction options](https://cloud.google.com/spanner/docs/timestamp-bounds).
406+
* @param {number} options.timeout - Specify a timeout for the transaction. The
407+
* transaction will be ran in its entirety, however if an abort error is
408+
* returned the transaction will be retried if the timeout has not been met.
409+
* Default: `60000` (milliseconds)
410+
* @param {boolean} options.readOnly - Specifies if the transaction is
411+
* read-only. Default: `false`.
412+
* @param {number} options.exactStaleness - Executes all reads at the timestamp
413+
* that is `exactStaleness` old.
414+
* @param {date} options.readTimestamp - Execute all reads at the given
415+
* timestamp.
416+
* @param {boolean} options.returnTimestamp - If `true`, returns the read
417+
* timestamp.
418+
* @param {boolean} options.strong - Read at the timestamp where all previously
419+
* committed transactions are visible.
420+
* @param {function} callback - The callback function.
421+
* @param {?error} callback.err - An error returned while getting the
422+
* transaction object.
423+
* @param {module:spanner/transaction} transaction - The transaction object.
424+
*
425+
* @example
426+
* database.getTransaction(function(err, transaction) {});
427+
*
428+
* //-
429+
* // If the callback is omitted, we'll return a Promise.
430+
* //-
431+
* database.getTransaction().then(function(data) {
432+
* var transaction = data[0];
433+
* });
434+
*/
435+
Database.prototype.getTransaction = function(options, callback) {
436+
var self = this;
437+
438+
if (is.fn(options)) {
439+
callback = options;
440+
options = null;
441+
}
442+
443+
if (!options || !options.readOnly) {
444+
this.pool_.getWriteSession(function(err, session, transaction) {
445+
callback(err, transaction);
446+
});
447+
return;
448+
}
449+
450+
this.pool_.getSession(function(err, session) {
451+
if (err) {
452+
callback(err);
453+
return;
454+
}
455+
456+
options = extend({}, options);
457+
delete options.readOnly;
458+
459+
var transaction = self.pool_.createTransaction_(session, options);
460+
461+
transaction.begin(function(err) {
462+
if (err) {
463+
transaction.end();
464+
callback(err);
465+
return;
466+
}
467+
468+
callback(null, transaction);
469+
});
470+
});
471+
};
472+
402473
/**
403474
* Execute a SQL statement on this database.
404475
*
@@ -811,7 +882,7 @@ Database.prototype.runTransaction = function(options, runFn) {
811882

812883
options = extend({}, options);
813884

814-
this.getTransaction_(options, function(err, transaction) {
885+
this.getTransaction(options, function(err, transaction) {
815886
if (err) {
816887
runFn(err);
817888
return;
@@ -1004,66 +1075,6 @@ Database.prototype.getSession_ = function(callback) {
10041075
this.pool_.getSession(callback);
10051076
};
10061077

1007-
/**
1008-
* Get a read/write ready Transaction object.
1009-
*
1010-
* @private
1011-
*
1012-
* @param {object=} options - Transaction options.
1013-
* @param {boolean} options.readOnly - Specifies if the transaction is read
1014-
* only.
1015-
* @param {function} callback - The callback function.
1016-
* @param {?error} callback.err - An error returned while getting the
1017-
* transaction object.
1018-
* @param {module:spanner/transaction} transaction - The transaction object.
1019-
*
1020-
* @example
1021-
* database.getTransaction(function(err, transaction) {});
1022-
*
1023-
* //-
1024-
* // If the callback is omitted, we'll return a Promise.
1025-
* //-
1026-
* database.getTransaction().then(function(data) {
1027-
* var transaction = data[0];
1028-
* });
1029-
*/
1030-
Database.prototype.getTransaction_ = function(options, callback) {
1031-
var self = this;
1032-
1033-
if (is.fn(options)) {
1034-
callback = options;
1035-
options = null;
1036-
}
1037-
1038-
if (!options || !options.readOnly) {
1039-
this.pool_.getWriteSession(function(err, session, transaction) {
1040-
callback(err, transaction);
1041-
});
1042-
return;
1043-
}
1044-
1045-
this.pool_.getSession(function(err, session) {
1046-
if (err) {
1047-
callback(err);
1048-
return;
1049-
}
1050-
1051-
options = extend({}, options);
1052-
delete options.readOnly;
1053-
1054-
var transaction = self.pool_.createTransaction_(session, options);
1055-
1056-
transaction.begin(function(err) {
1057-
if (err) {
1058-
callback(err);
1059-
return;
1060-
}
1061-
1062-
callback(null, transaction);
1063-
});
1064-
});
1065-
};
1066-
10671078
/**
10681079
* Create a Session object.
10691080
*

packages/spanner/src/transaction.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ Transaction.prototype.request = function(config, callback) {
325325
delete reqOpts.gaxOptions;
326326

327327
config.method(reqOpts, gaxOptions, function(err, resp) {
328-
if (!err || err.code !== ABORTED) {
328+
if (!self.runFn_ || !err || err.code !== ABORTED) {
329329
callback(err, resp);
330330
return;
331331
}

packages/spanner/test/database.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ describe('Database', function() {
798798

799799
describe('runTransaction', function() {
800800
it('should get a Transaction object', function(done) {
801-
database.getTransaction_ = function() {
801+
database.getTransaction = function() {
802802
done();
803803
};
804804

@@ -808,7 +808,7 @@ describe('Database', function() {
808808
it('should execute callback with error', function(done) {
809809
var error = new Error('Error.');
810810

811-
database.getTransaction_ = function(options, callback) {
811+
database.getTransaction = function(options, callback) {
812812
callback(error);
813813
};
814814

@@ -831,7 +831,7 @@ describe('Database', function() {
831831
return fakeDate;
832832
};
833833

834-
database.getTransaction_ = function(options, callback) {
834+
database.getTransaction = function(options, callback) {
835835
assert.deepEqual(options, OPTIONS);
836836
callback(null, TRANSACTION);
837837
};
@@ -855,7 +855,7 @@ describe('Database', function() {
855855
timeout: 1000
856856
};
857857

858-
database.getTransaction_ = function(options, callback) {
858+
database.getTransaction = function(options, callback) {
859859
callback(null, TRANSACTION);
860860
};
861861

@@ -1045,7 +1045,7 @@ describe('Database', function() {
10451045
});
10461046
});
10471047

1048-
describe('getTransaction_', function() {
1048+
describe('getTransaction', function() {
10491049
describe('write mode', function() {
10501050
it('should get a session from the pool', function(done) {
10511051
var error = new Error('Error.');
@@ -1058,7 +1058,7 @@ describe('Database', function() {
10581058
}
10591059
};
10601060

1061-
database.getTransaction_(function(err, transaction_) {
1061+
database.getTransaction(function(err, transaction_) {
10621062
assert.strictEqual(err, error);
10631063
assert.strictEqual(transaction_, transaction);
10641064
done();
@@ -1079,7 +1079,7 @@ describe('Database', function() {
10791079
}
10801080
};
10811081

1082-
database.getTransaction_(OPTIONS, assert.ifError);
1082+
database.getTransaction(OPTIONS, assert.ifError);
10831083
});
10841084

10851085
it('should return an error if could not get session', function(done) {
@@ -1091,7 +1091,7 @@ describe('Database', function() {
10911091
}
10921092
};
10931093

1094-
database.getTransaction_(OPTIONS, function(err) {
1094+
database.getTransaction(OPTIONS, function(err) {
10951095
assert.strictEqual(err, error);
10961096
done();
10971097
});
@@ -1121,7 +1121,7 @@ describe('Database', function() {
11211121
}
11221122
};
11231123

1124-
database.getTransaction_(OPTIONS, assert.ifError);
1124+
database.getTransaction(OPTIONS, assert.ifError);
11251125
});
11261126

11271127
it('should begin a transaction', function(done) {
@@ -1141,7 +1141,7 @@ describe('Database', function() {
11411141
}
11421142
};
11431143

1144-
database.getTransaction_(OPTIONS, function(err, transaction) {
1144+
database.getTransaction(OPTIONS, function(err, transaction) {
11451145
assert.ifError(err);
11461146
assert.strictEqual(transaction, TRANSACTION);
11471147
done();
@@ -1150,11 +1150,15 @@ describe('Database', function() {
11501150

11511151
it('should return an error if transaction cannot begin', function(done) {
11521152
var error = new Error('err');
1153+
var endCalled = false;
11531154

11541155
var SESSION = {};
11551156
var TRANSACTION = {
11561157
begin: function(callback) {
11571158
callback(error);
1159+
},
1160+
end: function() {
1161+
endCalled = true;
11581162
}
11591163
};
11601164

@@ -1167,8 +1171,9 @@ describe('Database', function() {
11671171
}
11681172
};
11691173

1170-
database.getTransaction_(OPTIONS, function(err) {
1174+
database.getTransaction(OPTIONS, function(err) {
11711175
assert.strictEqual(err, error);
1176+
assert.strictEqual(endCalled, true);
11721177
done();
11731178
});
11741179
});

packages/spanner/test/session-pool.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ describe('SessionPool', function() {
940940

941941
sessionPool.requestStream(CONFIG)
942942
.on('data', function(data) {
943-
assert.strictEqual(data, responseData);
943+
assert.deepEqual(data, responseData);
944944
done();
945945
});
946946

packages/spanner/test/transaction.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,10 @@ describe('Transaction', function() {
485485
return true;
486486
};
487487

488+
transaction.runFn_ = function() {
489+
done(new Error('Should not have been called.'));
490+
};
491+
488492
transaction.request(config, function() {
489493
done(new Error('Should not have been called.'));
490494
});
@@ -519,6 +523,15 @@ describe('Transaction', function() {
519523
done(new Error('Should not have been called.'));
520524
});
521525
});
526+
527+
it('should return the aborted error if no runFn', function(done) {
528+
transaction.runFn_ = null;
529+
530+
transaction.request(config, function(err) {
531+
assert.strictEqual(err, abortedError);
532+
done();
533+
});
534+
});
522535
});
523536
});
524537

0 commit comments

Comments
 (0)