Return connection to knex db pool
Asked Answered
H

2

5

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

Holp answered 15/3, 2018 at 15:1 Comment(0)
O
5

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

Orangeman answered 16/3, 2018 at 5:35 Comment(2)
your explanation is good.i am using knex connection without transaction.I just put require knex and call query.then .do i need to explicitly close the connection?Strategic
No, knex does this by itself.Hulk
R
3

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

Raleighraley answered 16/3, 2018 at 9:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.