.connect() And re-using connections.

XMLWordPrintableJSON

    • Type: Task
    • Resolution: Works as Designed
    • Priority: Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • 1
    • None
    • Not Needed
    • None
    • None
    • None
    • None
    • None
    • None

      Upon upgrading the driver from version 3.6 -> 4 according to the change documentation (https://github.com/mongodb/node-mongodb-native/blob/4.1/docs/CHANGES_4.0.0.md) one of the removed deprecations is:

      "Remove MongoClient.isConnected - calling connect is a no-op if already connected" 

       

      If I am reading that correctly, you are suggesting that if there is already an open mongo connection when ".connect()" is called again it will return the exact connection that is already open?

      I am raising this as when I have tried to achieve this locally I have not seen this to be the case... I have seen another connection opened.

      Another point to my case is this line (https://github.com/mongodb/node-mongodb-native/blob/4.1/src/operations/connect.ts#L28) inside the connect function, suggests that the connection should be re-used if a connection already exists. However if you were to console "mongoClient.topology" you would receive undefined at this point (The mongoClient does not have a topology object), hence that if block will not be called.

      Please see a little sample below:

      async function getClient2(close) { const client = await MongoClient.connect("mongodb://localhost:27017/", {"useNewUrlParser": true, "useUnifiedTopology": true}); // , {"useUnifiedTopology": true} if(close) { await client.close(); }} async function execute() { await getClient2(); await getClient2(true);}execute();
      

      This code will open 2 separate connections, rather than re-use the first connection.

      Maybe I am misunderstanding something around this, but any information on this would be much appreciated.

      Also apologies if I have not added the correct labels / links / tags.

            Assignee:
            Durran Jordan (Inactive)
            Reporter:
            Matt Hope
            None
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: