Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close connection MySQL Node.js

I'm developing a Node application with ExpressJS and MySQL. I'm working with this module https://github.com/felixge/node-mysql/ and I'm learning its use yet. I get troubles about how to close connections properly.

This is what I do:

app.post('/example', function (req, res) {

    //BD
    var connection = mysql.createConnection({
        host: config.database.host,
        port: config.database.port,
        user: config.database.user,
        password: config.database.password,
        database: config.database.database
    });

    var sql = '';

    if (varExample != null) {

         sql = 'Random query';

         connection.query(sql, function (err, results) {

             //Get results

             connection.end();
         }); 
    }
});

And sometimes I have to call this method several times to insert data in a DB. At that moment I get an error 'Too many connections'.

What is the way to close a connection in these cases?

like image 913
Ulyarez Avatar asked Sep 18 '15 11:09

Ulyarez


People also ask

How do I close a node connection in MySQL?

Closing database connection To force the connection close immediately, you can use the destroy() method. The destroy() method guarantees that no more callbacks or events will be triggered for the connection. Note that the destroy() method does not take any callback argument like the end() method.

How do I close a node JS connection?

The server. close() method stops the HTTP server from accepting new connections.

Do I need to close MySQL connection?

Do I need to close Mysqli connection? Explicitly closing open connections and freeing result sets is optional. However, it's a good idea to close the connection as soon as the script finishes performing all of its database operations, if it still has a lot of processing to do after getting the results.

How do I close a connection in MySQL workbench?

To quit MySQL Workbench select options File->Exit in a manu at the topmost line of MySQL Workbench window.


2 Answers

What you should not do is to open a connection every time you get a request. It is slow to connect everytime, second the driver normally opens a pool of connections for you so should not be a problem. There's no need to close the connection to mysql.

Basically you have to do this

//BD
var connection = mysql.createConnection({
    host: config.database.host,
    port: config.database.port,
    user: config.database.user,
    password: config.database.password,
    database: config.database.database
});
app.post('/example', function (req, res) {
    var sql = '';

    if (varExample != null) {

         sql = 'Random query';

         connection.query(sql, function (err, results) {

            //Get results
         });  
    }
});

EDIT: Add pool option Having a pool of connections is basically what most of us want for a server which has to do many queries. it just changes slightly how you create the connection.

var connection  = mysql.createPool({
  connectionLimit : 10,
  host            : 'example.org',
  user            : 'bob',
  password        : 'secret'
});
like image 65
DevAlien Avatar answered Oct 18 '22 20:10

DevAlien


I realize there is already an accepted answer but what you really should be doing is creating a database pool which the other answers really don't give an example of. You have to set this up a little differently than creating a normal database connection with the library.

-edit You do not have to worry about closing connections.

    var mysql = require('mysql');
    var pool  = mysql.createPool({
      connectionLimit : 10,
      host            : 'example.org',
      user            : 'bob',
      password        : 'secret'
    });

    pool.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
      if (err) throw err;

      console.log('The solution is: ', rows[0].solution);
    });

exports.Pool = pool;
like image 7
tier1 Avatar answered Oct 18 '22 20:10

tier1