File tree Expand file tree Collapse file tree 4 files changed +48
-5
lines changed Expand file tree Collapse file tree 4 files changed +48
-5
lines changed Original file line number Diff line number Diff line change @@ -346,19 +346,29 @@ export class MongoClient extends EventEmitter implements OperationParent {
346
346
const force = typeof forceOrCallback === 'boolean' ? forceOrCallback : false ;
347
347
348
348
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
+
349
358
if ( this . topology == null ) {
350
- return cb ( ) ;
359
+ completeClose ( ) ;
360
+ return ;
351
361
}
352
362
353
363
const topology = this . topology ;
354
364
topology . close ( { force } , err => {
355
365
const autoEncrypter = topology . s . options . autoEncrypter ;
356
366
if ( ! autoEncrypter ) {
357
- cb ( err ) ;
367
+ completeClose ( err ) ;
358
368
return ;
359
369
}
360
370
361
- autoEncrypter . teardown ( force , err2 => cb ( err || err2 ) ) ;
371
+ autoEncrypter . teardown ( force , err2 => completeClose ( err || err2 ) ) ;
362
372
} ) ;
363
373
} ) ;
364
374
}
Original file line number Diff line number Diff line change @@ -197,6 +197,11 @@ export function connect(
197
197
throw new Error ( 'no callback function provided' ) ;
198
198
}
199
199
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
+
200
205
let didRequestAuthentication = false ;
201
206
const logger = new Logger ( 'MongoClient' , options ) ;
202
207
Original file line number Diff line number Diff line change 2
2
const test = require ( './shared' ) . assert ,
3
3
setupDatabase = require ( './shared' ) . setupDatabase ,
4
4
expect = require ( 'chai' ) . expect ;
5
+ const withClient = require ( './shared' ) . withClient ;
5
6
6
7
describe ( 'Connection' , function ( ) {
7
8
before ( function ( ) {
@@ -273,4 +274,31 @@ describe('Connection', function () {
273
274
done ( ) ;
274
275
}
275
276
} ) ;
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
+ } ) ;
276
304
} ) ;
Original file line number Diff line number Diff line change @@ -123,7 +123,7 @@ describe('Sessions', function () {
123
123
// verify that the `endSessions` command was sent
124
124
const lastCommand = test . commands . started [ test . commands . started . length - 1 ] ;
125
125
expect ( lastCommand . commandName ) . to . equal ( 'endSessions' ) ;
126
- expect ( client . topology . s . sessionPool . sessions ) . to . have . length ( 0 ) ;
126
+ expect ( client . topology ) . to . not . exist ;
127
127
} ) ;
128
128
} ) ;
129
129
} ) ;
@@ -143,7 +143,7 @@ describe('Sessions', function () {
143
143
// verify that the `endSessions` command was sent
144
144
const lastCommand = test . commands . started [ test . commands . started . length - 1 ] ;
145
145
expect ( lastCommand . commandName ) . to . equal ( 'endSessions' ) ;
146
- expect ( client . topology . s . sessionPool . sessions ) . to . have . length ( 0 ) ;
146
+ expect ( client . topology ) . to . not . exist ;
147
147
} ) ;
148
148
} ) ;
149
149
}
You can’t perform that action at this time.
0 commit comments