简体   繁体   中英

Return connection to knex db pool

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

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 (ie 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 ).

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.

PS 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM