title: How does connection pooling work in the following cases specific to MongoDB-MongoClient driver?
body: <p>A connection pool is a pool of db connections that is loaded when the application starts. When a transaction comes in, the driver will pick a connection from the connection pool to serve the transaction. A connection to the database happens when both ends open a socket over tcp for exchanging messages. Will that socket connection be active at the time the connection is in the pool? or a new socket connection will start once picked up from the connection pool?. If the latter then what's really stored in the connection pool?. I have an application configured with the following mongo values:</p>
  <pre><code>KeepAlive=true&amp;poolSize=20&amp;autoReconnect=true&amp;socketTimeoutMS=5000&amp;connectTimeoutMS=30000&amp;maxTimeMS=5000
  </code></pre>
  <p>The application starts with a pool with 20 db connections.</p>
  <p>If the sockettimeoutms is 5000, what will happen to the connections if they are not used within a timeframe of 5 seconds in the pool? or will socketTimeoutMs just be used in a transaction context?</p>
  <p>For connectTimeoutMS I believe it is used at the time the application attempts to create that socket connection. When a transaction comes in, will the server attempt to create a socket connection or will that be already done at the pool level?.</p>
  <p>For maxTimeMS, I attempted to test this value by setting it to 1 ms and trying to send a query to mongo with the following statement:</p>
  <pre><code>    @Autowired
      protected MongoTemplate mongoTemplate;
  
   mongoTemplate.findAll(Myclass.class);
  </code></pre>
  <p>It didn't fail. However it does fail when I set either connectTimeout or socketTimeout to 1ms which is expected.</p>
  <p>Regarding autoReconnect, I didn't any information on it. I believe it is used along with KeepAlive in case the driver detects an issue with the network it will attempt to reconnect.</p>