2

I have a nodejs app which is doing some mongodb database migration.

All of it starts with

MongoClient.connect(`mongodb://${databaseHostname}:${databasePort}/${options.databaseName}`,
(connectionError, dbConnection) => {
  if (connectionError) {
    return cb(connectionError);
  }

  console.log('Opened connection to', databaseHostname, options.databaseName);
  return cb(null, dbConnection);
});

And ends with

console.log('Close');
dbConnection.close((err) => {
  if (err) console.log(err);

  console.log('Closed');
});

In between there is quite a lot happening, new collections created, documents rearranged etc. But never a new connection opened. The output of the app is:

Opened connection to localhost testDatabase

[Lots of other logs here]

Close
Closed

The database logs are

2016-07-18T16:54:29.400+0200 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:54336 #848 (1 connection now open)
2016-07-18T16:54:29.423+0200 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:54337 #849 (2 connections now open)
2016-07-18T16:54:29.426+0200 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:54338 #850 (3 connections now open)

[Lots of insertions and updates here]

2016-07-18T16:58:06.493+0200 I NETWORK  [conn849] end connection 127.0.0.1:54337 (2 connections now open)
2016-07-18T16:58:06.494+0200 I NETWORK  [conn850] end connection 127.0.0.1:54338 (1 connection now open)

As you can see, one mongodb connection stays open even though dbConnection.close() got called preventing the nodejs app to exit. I am looking at this for quite some time now but do not understand what could cause this behaviour?

Philiiiiiipp
  • 705
  • 1
  • 9
  • 24

1 Answers1

0

if you want to close the connection, have you try consider using .finally() or .then() statement after it have succeeded to connect ?

something like

MongoClient.connect(`mongodb://${databaseHostname}:${databasePort}/${options.databaseName}`,
(connectionError, dbConnection) => {
  if (connectionError) {
    return cb(connectionError);
  }

  console.log('Opened connection to', databaseHostname, options.databaseName);
  return cb(null, dbConnection);
}).then( () => {
dbConnection.close((err) => {
  if (err) console.log(err);
  console.log('Closed');
  });
});

Anyway I recently did some research on mongodb connection, somewhat they said it is not recommended to close your database. Why is it recommended not to close a MongoDB connection anywhere in Node.js code?

and this link might be helping further for your problem.

http://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html

Community
  • 1
  • 1
gema
  • 523
  • 1
  • 7
  • 17
  • Hey, I do think you don't understand the issue. I do not want to close my connection right away since I am doing all the database migrations in between. And you should close the database connection if you want your program to exit properly since it will otherwise just stay open. What you are referring to is that you should not close the connection if you want to use it again e.g. in between queries. – Philiiiiiipp Jul 18 '16 at 16:28
  • Oh I see, perhaps you should try https://github.com/mongodb/node-mongodb-native. use the command db.close . I've found it from the answer here http://stackoverflow.com/a/12368764/3465227 – gema Jul 18 '16 at 17:06