4

I am using AWS DocumentDB as my database in my application which is developed in node.js which is MongoDB compatible. I have used MongoClient to connect to my database. My application executes but it makes approx 1000 DB connections for each execution and then it fails with an error:

MongoServerSelectionError: connection <monitor> to xxx.xx.xx.xxx:27017 closed. 

I tried to use client.close() as well to close the connections then it gave that the connection pool is closed. I believe that issue is due to so many database connections.

How can I release the connections in MongoDB? Any help is appreciated.

fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88
  • 1
    Can you give smaple of you code how are making connection, is the connection object created on every request? It should be singleton object – Sohan Mar 26 '21 at 06:24

1 Answers1

1

It appears from your question that what is happening is that you are defining a new MongoClient object each time you invoke your function resulting in the driver creating a new database connection with each function call. Instead, define the client connection outside the AWS Lambda handler function and reuse the connection with each function call.

Joseph Idziorek
  • 4,853
  • 6
  • 23
  • 37