Skip to content

Commit 6ccab2b

Browse files
committed
remove session setter from operation
1 parent 328695b commit 6ccab2b

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

src/operations/execute_operation.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ReadPreference } from '../read_preference';
2-
import { MongoError, isRetryableError, AnyError } from '../error';
2+
import { MongoError, isRetryableError } from '../error';
33
import { Aspect, AbstractOperation } from './operation';
44
import { maxWireVersion, maybePromise, Callback } from '../utils';
55
import { ServerType } from '../sdam/common';
@@ -76,31 +76,28 @@ export function executeOperation<
7676

7777
// The driver sessions spec mandates that we implicitly create sessions for operations
7878
// that are not explicitly provided with a session.
79-
let session: ClientSession;
79+
let session = operation.session;
8080
let owner: symbol;
8181
if (topology.hasSessionSupport()) {
82-
if (operation.session == null) {
82+
if (session == null) {
8383
owner = Symbol();
8484
session = topology.startSession({ owner, explicit: false });
85-
operation.session = session;
8685
} else if (operation.session.hasEnded) {
8786
throw new MongoError('Use of expired sessions is not permitted');
8887
}
8988
}
9089

9190
return maybePromise(callback, cb => {
92-
function executeCallback(err?: AnyError, result?: TResult) {
93-
if (session && session.owner === owner) {
94-
return session.endSession(err2 => cb(err2 || err, result));
95-
}
96-
97-
cb(err, result);
98-
}
99-
10091
try {
101-
executeWithServerSelection(topology, operation, executeCallback);
92+
executeWithServerSelection(topology, session, operation, (err, result) => {
93+
if (session && session.owner && session.owner === owner) {
94+
return session.endSession(err2 => cb(err2 || err, result));
95+
}
96+
97+
cb(err, result);
98+
});
10299
} catch (e) {
103-
if (session && session.owner === owner) {
100+
if (session && session.owner && session.owner === owner) {
104101
session.endSession();
105102
}
106103

@@ -113,8 +110,12 @@ function supportsRetryableReads(server: Server) {
113110
return maxWireVersion(server) >= 6;
114111
}
115112

116-
function executeWithServerSelection(topology: Topology, operation: any, callback: Callback) {
117-
const session = operation.session;
113+
function executeWithServerSelection(
114+
topology: Topology,
115+
session: ClientSession,
116+
operation: any,
117+
callback: Callback
118+
) {
118119
const readPreference = operation.readPreference || ReadPreference.primary;
119120
const inTransaction = session && session.inTransaction();
120121

@@ -196,17 +197,15 @@ function executeWithServerSelection(topology: Topology, operation: any, callback
196197
return;
197198
}
198199

199-
if (operation.hasAspect(Aspect.RETRYABLE)) {
200+
if (session && operation.hasAspect(Aspect.RETRYABLE)) {
200201
const willRetryRead =
201202
topology.s.options.retryReads !== false &&
202-
operation.session &&
203203
!inTransaction &&
204204
supportsRetryableReads(server) &&
205205
operation.canRetryRead;
206206

207207
const willRetryWrite =
208208
topology.s.options.retryWrites === true &&
209-
operation.session &&
210209
!inTransaction &&
211210
supportsRetryableWrites(server) &&
212211
operation.canRetryWrite;
@@ -217,7 +216,7 @@ function executeWithServerSelection(topology: Topology, operation: any, callback
217216
if ((hasReadAspect && willRetryRead) || (hasWriteAspect && willRetryWrite)) {
218217
if (hasWriteAspect && willRetryWrite) {
219218
operation.options.willRetryWrite = true;
220-
operation.session.incrementTransactionNumber();
219+
session.incrementTransactionNumber();
221220
}
222221

223222
operation.execute(server, session, callbackWithRetry);

src/operations/operation.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@ export abstract class AbstractOperation<T> {
7373
return ctor.aspects.has(aspect);
7474
}
7575

76-
set session(session: ClientSession) {
77-
this[kSession] = session;
78-
}
79-
8076
get session(): ClientSession {
8177
return this[kSession];
8278
}

0 commit comments

Comments
 (0)