Skip to content
This repository was archived by the owner on Feb 4, 2022. It is now read-only.

Commit 764cdec

Browse files
daprahamianmbroadst
authored andcommitted
updating to be spec compliant
1 parent 31b984f commit 764cdec

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

lib/sessions.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,9 @@ function endTransaction(session, commandName, callback) {
452452

453453
if (isUnknownTransactionCommitResult(e)) {
454454
e.errorLabels.push('UnknownTransactionCommitResult');
455+
456+
// Per Mongos Pinning, must unpin session
457+
session.transaction.unpinServer();
455458
}
456459
}
457460
} else {

lib/topologies/mongos.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ function pickProxy(self, session) {
507507
self.index = (self.index + 1) % connectedProxies.length;
508508
}
509509

510-
if (transaction) {
510+
if (transaction && transaction.isActive) {
511511
transaction.pinServer(proxy);
512512
}
513513

lib/transactions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class Transaction {
129129
const nextStates = stateMachine[this.state];
130130
if (nextStates && nextStates.indexOf(nextState) !== -1) {
131131
this.state = nextState;
132-
if (this.state === TxnState.NO_TRANSACTION) {
132+
if (this.state === TxnState.NO_TRANSACTION || this.state === TxnState.STARTING_TRANSACTION) {
133133
this.unpinServer();
134134
}
135135
return;

lib/wireprotocol/command.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,24 @@ function command(server, ns, cmd, options, callback) {
6060
? new Msg(bson, cmdNs, finalCmd, commandOptions)
6161
: new Query(bson, cmdNs, finalCmd, commandOptions);
6262

63+
const commandResponseHandler = function(err) {
64+
if (
65+
options.session &&
66+
options.session.transaction &&
67+
err &&
68+
err instanceof MongoError &&
69+
err.hasErrorLabel('TransientTransactionError')
70+
) {
71+
options.session.transaction.unpinServer();
72+
}
73+
74+
return callback.apply(null, arguments);
75+
};
76+
6377
try {
64-
pool.write(message, commandOptions, callback);
78+
pool.write(message, commandOptions, commandResponseHandler);
6579
} catch (err) {
66-
callback(err);
80+
callback(commandResponseHandler);
6781
}
6882
}
6983

0 commit comments

Comments
 (0)