4

I am using Knex.js with MySQL and would like to utilize a connection pool, as the test is under heavy load.

Even though I have set the pool.max to 10, I never see more than 1 connection being used. Here is an example that runs a query every 100ms:

    const db = require("knex")({
      client: "mysql",
      connection: {
        user: "admin",
        password: "xx!",
        database: "xx",
        host: "xx",
        port: 3306,
      },
      debug: false,
      pool: {
        min: 2,
        max: 10,
      },
    });
    
    (async () => {
      setInterval(async () => {
        await db("titles").select(1).limit(1);
    
        console.log("numUsed", db.client.pool.numUsed());
        console.log("numFree", db.client.pool.numFree());
    
        console.log("--");
          }, 100);
        
        })();

When I run this I see the following:

numUsed 2
numFree 0
--
numUsed 2
numFree 0
--
numUsed 3
numFree 0
--
numUsed 2
numFree 1
--
numUsed 1
numFree 2
--
numUsed 1
numFree 3
--
numUsed 0
numFree 5
--
numUsed 1
numFree 4
--
numUsed 0
numFree 5
--
numUsed 0
numFree 5
--
numUsed 0
numFree 5
--
numUsed 0
numFree 5

We can see here the number used DOES increase to a max of 3 then reduces to 0. I dont understand this behavior and cannot find anything documented to explain.

Is there anything I should be doing to manually to force use of a pool resource?

I see in the tarn.js docs (pool used my knex) there are methods to acquire() and release(). My expectation (given it is not documented) is that Knex manages this for me.

Appreciate any guidance.

Ben
  • 6,026
  • 11
  • 51
  • 72

0 Answers0