@@ -23,6 +23,7 @@ function command(server, ns, cmd, options, callback) {
23
23
const pool = server . s . pool ;
24
24
const readPreference = getReadPreference ( cmd , options ) ;
25
25
const shouldUseOpMsg = supportsOpMsg ( server ) ;
26
+ const session = options . session ;
26
27
27
28
let finalCmd = Object . assign ( { } , cmd ) ;
28
29
if (
@@ -37,7 +38,7 @@ function command(server, ns, cmd, options, callback) {
37
38
} ;
38
39
}
39
40
40
- const err = decorateWithSessionsData ( finalCmd , options . session , options ) ;
41
+ const err = decorateWithSessionsData ( finalCmd , session , options ) ;
41
42
if ( err ) {
42
43
return callback ( err ) ;
43
44
}
@@ -60,19 +61,16 @@ function command(server, ns, cmd, options, callback) {
60
61
? new Msg ( bson , cmdNs , finalCmd , commandOptions )
61
62
: new Query ( bson , cmdNs , finalCmd , commandOptions ) ;
62
63
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
+ }
73
70
74
- return callback . apply ( null , arguments ) ;
75
- } ;
71
+ return callback . apply ( null , arguments ) ;
72
+ }
73
+ : callback ;
76
74
77
75
try {
78
76
pool . write ( message , commandOptions , commandResponseHandler ) ;
0 commit comments