IBM / node-odbc

ODBC bindings for node
MIT License
143 stars 75 forks source link

[BUG] No new connections being made on empty pool when maxSize is less than incrementSize #397

Open popeliv opened 4 weeks ago

popeliv commented 4 weeks ago

Describe your system

Describe the bug This bug is related to pooling.

On an initialized empty pool (with no active connections), if the incrementSize is larger than the maxSize, if no new connections are being created, no new connections can be created.

For example: If there is only one connection in a pool and that single connection dies (i.e. the socket gets closed), the connection needs to be closed manually with nativeClose and poolSize needs to be decremented manually. We need to call nativeClose instead of close because close will just release that dead connection back into the pool without closing it. In this state, no new connection can be made.

I discovered this with a pool maxSize of 5, initialSize of 1 and without defining the incrementSize (it defaults to 10).

Expected behavior A new connection should be made on an empty pool regardless of the maxSize and incrementSize settings on the pool.

To Reproduce

  1. Connect to database using a pool with options { maxSize: 5, initialSize: 1, reuseConnections: true }
  2. Force close the connection socket from on the database server
  3. Call nativeClose on the connection in the pool and decrement the poolSize
  4. Call connect on the pool to obtain a new connection.

Code

  const query = async (query) => {
    let odbcConnection = await connect()
    for (let tries = 0; tries < maxNumberOfTries; tries++) {
     try {
        const res = await odbcConnection.query(query)
        await odbcConnection.close()
        return res
      } catch (error) {
        if (
          error.odbcErrors?.[0]?.message ===
          "[DataDirect][ODBC Progress OpenEdge Wire Protocol driver]Socket closed."
        ) { 
          await odbcConnection.nativeClose()
          this.pool.poolSize--
          odbcConnection = await connect()
        }
      }
    }
    throw new Error("Failed to query the database")
  }

The main issue here is that it doesn't make sense for incrementSize to be less than maxSize, but the default incrementSize is 10, so any maxSize less than that can cause issues.

Maybe the best way to handle this would be set the default incrementSize to min(maxSize, 10), or change the logic in the if condition to handle this case.