Skip to content

Commit 8ca579c

Browse files
committed
allow for client connect after close
1 parent 7ed6dbf commit 8ca579c

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

src/mongo_client.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,19 +346,29 @@ export class MongoClient extends EventEmitter implements OperationParent {
346346
const force = typeof forceOrCallback === 'boolean' ? forceOrCallback : false;
347347

348348
return maybePromise(callback, cb => {
349+
const completeClose = (err?: AnyError) => {
350+
// clear out references to old topology
351+
this.topology = undefined;
352+
this.s.dbCache = new Map();
353+
this.s.sessions = new Set();
354+
355+
cb(err);
356+
};
357+
349358
if (this.topology == null) {
350-
return cb();
359+
completeClose();
360+
return;
351361
}
352362

353363
const topology = this.topology;
354364
topology.close({ force }, err => {
355365
const autoEncrypter = topology.s.options.autoEncrypter;
356366
if (!autoEncrypter) {
357-
cb(err);
367+
completeClose(err);
358368
return;
359369
}
360370

361-
autoEncrypter.teardown(force, err2 => cb(err || err2));
371+
autoEncrypter.teardown(force, err2 => completeClose(err || err2));
362372
});
363373
});
364374
}

src/operations/connect.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ export function connect(
197197
throw new Error('no callback function provided');
198198
}
199199

200+
// Has a connection already been established?
201+
if (mongoClient.topology && mongoClient.topology.isConnected()) {
202+
throw new Error(`'connect' cannot be called when already connected`);
203+
}
204+
200205
let didRequestAuthentication = false;
201206
const logger = new Logger('MongoClient', options);
202207

test/functional/connection.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const test = require('./shared').assert,
33
setupDatabase = require('./shared').setupDatabase,
44
expect = require('chai').expect;
5+
const withClient = require('./shared').withClient;
56

67
describe('Connection', function () {
78
before(function () {
@@ -273,4 +274,31 @@ describe('Connection', function () {
273274
done();
274275
}
275276
});
277+
278+
it('should be able to connect again after close', function () {
279+
return withClient.call(this, (client, done) => {
280+
expect(client.isConnected()).to.be.true;
281+
282+
const collection = () => client.db('testReconnect').collection('test');
283+
collection().insertOne({ a: 1 }, (err, result) => {
284+
expect(err).to.not.exist;
285+
expect(result).to.exist;
286+
287+
client.close(err => {
288+
expect(err).to.not.exist;
289+
290+
client.connect(err => {
291+
expect(err).to.not.exist;
292+
293+
collection().insertOne({ b: 2 }, (err, result) => {
294+
expect(err).to.not.exist;
295+
expect(result).to.exist;
296+
expect(client.topology.isDestroyed()).to.be.false;
297+
done();
298+
});
299+
});
300+
});
301+
});
302+
});
303+
});
276304
});

test/functional/sessions.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ describe('Sessions', function () {
123123
// verify that the `endSessions` command was sent
124124
const lastCommand = test.commands.started[test.commands.started.length - 1];
125125
expect(lastCommand.commandName).to.equal('endSessions');
126-
expect(client.topology.s.sessionPool.sessions).to.have.length(0);
126+
expect(client.topology).to.not.exist;
127127
});
128128
});
129129
});
@@ -143,7 +143,7 @@ describe('Sessions', function () {
143143
// verify that the `endSessions` command was sent
144144
const lastCommand = test.commands.started[test.commands.started.length - 1];
145145
expect(lastCommand.commandName).to.equal('endSessions');
146-
expect(client.topology.s.sessionPool.sessions).to.have.length(0);
146+
expect(client.topology).to.not.exist;
147147
});
148148
});
149149
}

0 commit comments

Comments
 (0)