Node JS: How to release a connection from the pool in MySQL2?

Viewed 14

Please check my below code

const mysql = require('mysql2');
const errorCodes = require('source/error-codes');
const PropertiesReader = require('properties-reader');

const prop = PropertiesReader('properties.properties');

const con = mysql.createPool({
  connectionLimit : 10,
  host: prop.get('server.host'),
  user: prop.get("server.username"),
  password: prop.get("server.password"),
  port: prop.get("server.port"),
  database: prop.get("server.dbname")
});

exports.getCountryList = (event, context, callback) => {
  // allows for using callbacks as finish/error-handlers
  context.callbackWaitsForEmptyEventLoop = false;
  const sql = "select * from country_list";
  con.query(sql, function (err, result) {
    if (err) {
      log.console(err);
      var response = errorCodes.internal_server_error;
      callback(null, response);
    }
    else {
      var response = {
        "statusCode": 200,
        "headers": {
          "Content-Type": "application/json"
        },
        "body": JSON.stringify(result),
        "isBase64Encoded": false
      };
      callback(null, response)
    }
  });
};

I am creating a connection pool, getting a connection from it, and executing the SQL statement. Does this connection get released automatically and immediately after the job? If not how can I release this connection?

At the same time, the way I am taking a connection from the pool is accurate?

0 Answers
Related