Reputation: 113
I'm using knex version 3.10.10, in my node app, connecting to MySQL DB. My configuration of knex in the app is using the pool option configuration.
1) Is there a need to EXPLICITLY return a connection to the pool after I fired a query? If yes - how
2) Is there a need to EXPLICITLY perform a check on a pool's connection, before firing the query?
Thanks in advance
Upvotes: 5
Views: 19121
Reputation: 2382
There is no such info in the documentation but based on the source code you can access knex
pool like this
const knex = require('knex')(config);
const pool = knex.client.pool;
console.log(pool);
knex
uses tarn pool under the hood, so you can check out it's methods there.
P.S. I don't know where did you get that knex
version (3 point something) but the current version of it on this answer moment is 0.14.4
Upvotes: 3
Reputation: 1460
No. There is no need to do either.
Knex handles a connection pool for you. You can adjust the pool size if you need to by using the setting: pool: { min: 0, max: 7 }
within your connection setup, and the documentation also includes a link to the library that Knex uses for pool handling if you care about the gory details.
The knex documentation has a little info on this here: link
Each connection will be used by Knex for the duration of a query or a transaction, then released back to the pool.
BUT, if you implement transactions (i.e. multiple SQL statements to be saved or cancelled as a unit) without using Promises, then you will need to explicitly commit/rollback the transaction to properly complete the transaction, which will also release the connection back to the pool when the transaction is complete. (see more on Knex Transactions: here).
Upvotes: 5