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

Commit 9d79a55

Browse files
daprahamianmbroadst
authored andcommitted
only wrap callback in command handler if in a transaction
1 parent c041747 commit 9d79a55

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

lib/wireprotocol/command.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function command(server, ns, cmd, options, callback) {
2323
const pool = server.s.pool;
2424
const readPreference = getReadPreference(cmd, options);
2525
const shouldUseOpMsg = supportsOpMsg(server);
26+
const session = options.session;
2627

2728
let finalCmd = Object.assign({}, cmd);
2829
if (
@@ -37,7 +38,7 @@ function command(server, ns, cmd, options, callback) {
3738
};
3839
}
3940

40-
const err = decorateWithSessionsData(finalCmd, options.session, options);
41+
const err = decorateWithSessionsData(finalCmd, session, options);
4142
if (err) {
4243
return callback(err);
4344
}
@@ -60,19 +61,16 @@ function command(server, ns, cmd, options, callback) {
6061
? new Msg(bson, cmdNs, finalCmd, commandOptions)
6162
: new Query(bson, cmdNs, finalCmd, commandOptions);
6263

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-
}
64+
const inTransaction = session && (session.inTransaction() || isTransactionCommand(finalCmd));
65+
const commandResponseHandler = inTransaction
66+
? function(err) {
67+
if (err && err instanceof MongoError && err.hasErrorLabel('TransientTransactionError')) {
68+
session.transaction.unpinServer();
69+
}
7370

74-
return callback.apply(null, arguments);
75-
};
71+
return callback.apply(null, arguments);
72+
}
73+
: callback;
7674

7775
try {
7876
pool.write(message, commandOptions, commandResponseHandler);

0 commit comments

Comments
 (0)