0

I wanted to know when you are done processing some query or transaction do you usually close a connection only? do you close the pool? or do you close both? Does it even matter which one you close? What happens under the hood when you do all three of these cases (if there even is a difference). I’d appreciate it since I’ve been looking at the mariadb documentation and I see there are methods for closing both a pool and a connection and usually you instantiate opening one connection in the pool as far as I know and you need to close it when you are done using it.

https://github.com/mariadb-corporation/mariadb-connector-nodejs/blob/master/documentation/promise-api.md#promise-api

I’d appreciate the guidance! Thank You!

1 Answer 1

0

Yes, you'd "close" (or release) the Connection - the Pool maintains a list of Connections for your application to use (FIFO queuing the request if no Connection is available). It's semantic - but technically you don't actually close the connection; you release it back to the pool:

const pool = mariadb.createPool({ host: 'mydb.com', user:'myUser' });
pool.getConnection()
   .then(conn => {
     console.log("connected");
     conn.release(); //release NOT close
   })
   .catch(console.error);

Closing the Pool using pool.end() will gracefully close all open Connections (not just the current). You'd need to recreate the Pool to get another Connection.

3
  • Thank You for the response! Just one more question Is there a reason to .end() a connection or when you need to forcefully shut down a connection .destroy()? In what situation would we want to .close() a pool? Seems like .release() gets the job done when using it on a api endpoint.
    – Victor
    Jun 16, 2021 at 15:02
  • .release() will return connection back to the pool, AFAIK .end() will do the same - I assume you'd use it if you've manually created the connection as opposed to retrieving it from the pool. I would use pool.close() usually when gracefully shutting down a service.
    – Keir
    Jun 16, 2021 at 16:16
  • Got it! Thank You very much! 😄
    – Victor
    Jun 16, 2021 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.