connection pools

hi

im trying to work with mongodb from java. the problem is that I run out of connections really quick in this one usecase I have.

I open a mongoclient, run the query and then call mongoclient.close()

this is repeated for my need 7000 times. Creating the client 7000 is not performance wise and this is NOT the question.
but after 500 times getting the connection simply stalls and I can see from the monitor that there are 500 open connections. But I call “close()” every time so why is getting the connection the 501th time a issue?
its unused, its closed, it should be back in the pool for then next use.

the connectionstring has added info on max times but that also does nothing to release or close unused connections

mongodb.net/?retryWrites=true&w=majority&appName=Cluster0&maxIdleTimeMS=1000&maxLifeTimeMS=2000

Hi @Jan_Agermose

Create one client for the application and do not close it until application termination.

Please read more on connection fundamentals:
https://mongodb.prakticum-team.ru/docs/drivers/java/sync/current/fundamentals/connection/connect/

And related FAQ item:
https://mongodb.prakticum-team.ru/docs/drivers/java/sync/current/faq/#how-does-connection-pooling-work-in-the-java-driver-

thanks. But thats not the question.

if you have a application where it makes sense (most will) where you

  1. create the mongoclient
  2. loop
  3. query
    
  4. close mongodb client

then thats fine and best performance and so on. this is for any database the samme.

I need to

  1. in loop
  2. create a mongodb client
  3. query once
  4. close the client

so again. the client is created and it will create a connection pool even not just one connection.
BUT BUT BUT I do CLOSE it so it should release all connections.

waaaayyyy better performance on any system if you only create the connection/pool once of cause, so gain thats not the question.

the question is why does is not even work on mongodb. why are the connections/pool not released so I can create a new one if thats what I want or need.

What version of driver is being used?
Have you got a minimal sample of code to reproduce this?

Using mongodb-driver-sync 5.2.0 it works as you’d wish it to.