title: The correct method of utilizing connection pooling
body: <p>I am currently developing a server and one of its functions is retrieving data from the server when <code>database</code>, <code>query</code>, <code>credentials</code>, etc are supplied from the client. I am also using <code>socket.io</code> to stream 100 rows from server at a time.</p>
  
  <p>Since I am not sure how many concurrent requests will be made to the database at once at peak, I have set up <code>connectionPool</code> using <code>mssql</code> node module. Below is succinctly how my code looks like.</p>
  
  <pre><code>socket.on('request:database', (connectionConf) =&gt; {
  
      let query = connectionConf.query;
      let dbAddress = connectionConf.dbAddress;
      let id = connectionConf.id;
      let password = connectionConf.password;
      let dbName = connectionConf.dbName;
      let requestedRows = parseInt(connectionConf.noOfRows);
      let createExcel = connectionConf.excel;
  
      const sqlConfig = {
          user: id,
          password: password,
          server: dbAddress,
          database: dbName,
          pool: {
              max: 1,
              min: 0,
              idleTimeoutMillis: 30000
          },
          options: {
              encrypt: true,
          }
      };
  
      let connectionPool = new sql.ConnectionPool(sqlConfig, err =&gt; 
  
          request.stream = true;
  
          request.query(query);
  
          request.on('recordset', columns =&gt; {
              //run functions with columns
  
              socket.emit('response:headers', //send results);
  
              // Emitted once for each recordset in a query
          });
  
          request.on('row', row =&gt; {
              //Run some functions
              socket.emit('result', //send 100 rows at a time);
  
              // Emitted for each row in a recordset
          });
      )
  });
  </code></pre>
  
  <p>In the above code, <code>request:database</code> <code>socket.io</code> event is triggered every time client makes a query. The problem is that I am not utilizing the pool correctly as I am creating new <code>connectionPool</code> every time client clicks send query button. </p>
  
  <p>So, I cannot create connection pool prior to the button click because they are provided from the client</p>
  
  <blockquote>
    <p>How do I resolve this connection pooling issue so that I can configure db connection with the data that is provided by the client and at the same time reuse same pool with several end point users?</p>
  </blockquote>