mariadb-corporation / mariadb-connector-nodejs

MariaDB Connector/Node.js is used to connect applications developed on Node.js to MariaDB and MySQL databases. MariaDB Connector/Node.js is LGPL licensed.
GNU Lesser General Public License v2.1
366 stars 91 forks source link

Correct way to close connection #198

Closed iamDonkey closed 1 year ago

iamDonkey commented 2 years ago

In the pool based usage What is the correct way to close the connection after query is executed? conn.end() or conn.release() ???

rusher commented 2 years ago

The correct way is to use conn.release() (see documentation https://github.com/mariadb-corporation/mariadb-connector-nodejs/blob/master/documentation/promise-api.md#poolgetconnection--promise)

(To avoid any issue, conn.end when connection comes from pool just is just a reference to conn.release)

iamDonkey commented 2 years ago

thanks.

iamDonkey commented 2 years ago

Despite releasing connection back to pool after idle for sometime i am getting this error. i think all connection in the pool are dead. how to configure to reconnect if connection closed by server for inactivity?


SqlError: (conn=-1, no: 45028, SQLState: HY000) retrieve connection from pool timeout after 2001ms 
    at Object.module.exports.createError (/root/node_modules/mariadb/lib/misc/errors.js:56:10)
    at Pool._requestTimeoutHandler (/root/node_modules/mariadb/lib/pool.js:332:18)
    at listOnTimeout (node:internal/timers:559:17)
    at processTimers (node:internal/timers:502:7) {
  text: 'retrieve connection from pool timeout after 2001ms',
  sql: null,
  fatal: false, 
  errno: 45028,
  sqlState: 'HY000',
  code: 'ER_GET_CONNECTION_TIMEOUT'
}
Total connections:  0
Active connections:  0
Idle connections:  0
iamDonkey commented 2 years ago

do i have to use minimumIdle option in the pool?

rusher commented 2 years ago

minimumIdle is by default set to connectionLimit, so that's not the problem here.

normally, connection in pool can be be wrong state if not used for some time (server close socket after reaching @@wait_timeout value => default to 8 hours), but that's not an issue, because pool will validate those connection, discard them, and recreate new one. Creating new connection usually takes around 50ms, it slow query a little bit, but after beeing unused for 8h, that not a big deal. This can even be avoided setting pool option idleTimeout in order to validate connection in pool regulary.

Your message is very strange, with "Total connections: 0". Connection creation takes some time, but a lot less than 2000ms. Do you use some serverless implementation that makes some time to wake up ?

rusher commented 1 year ago

closing since no answer since 9 month