Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there is any problems if we don't close the mysql connection in mysql node js module

If we don't close or end mysql connection in node js, is it effects in feature or not.

I am doing like this

var mysql      = require('mysql');
var connection = mysql.createConnection(...);

connection.query('SELECT 1', function(err, rows) {
  // connected! (unless `err` is set)
});

Am not ending mysql connection any where in my code. My question is Is it necessary to close the mysql connection. If we don't close the mysql connection will i face any other problems in future.

Please help me am new to nodejs

EDIT1 I will not face any problems like unable to connect, too many connections to open etc, means any resource related issues? right.

EDIT2

At which instant mysql connection will be close if we don't end it manually or by using end function?

like image 742
web spider26 Avatar asked May 31 '12 07:05

web spider26


People also ask

Is it possible to Access MySQL database from Node JS?

Almost every popular programming language, like PHP, Java, C# etc provides drivers for connections with a MySql database. As MySQL is one of the most popular open source database in world and efficient as well, there's need to be support for it in Node.js and thanks to this module, you'll be able to access from Node.js without any kind of problem.

What is node-MySQL?

node-mysql : A node.js module implementing the MySQL protocol. This is a node.js driver for mysql. It is written in JavaScript, does not require compiling. It provides all most all connection/query from MySQL. Node-mysql is probably one of the best modules used for working with MySQL database and the module is actively maintained.

What is NodeJS and MySQL binding?

Node.js and MySQL is one of the necessary binding needed for any web application. MySQL is one of the most popular open source database in world and efficient as well. Almost every popular programming language like Java or PHP provides driver to access and perform operations with MySQL.

How to fix ‘connected to MySQL server?

Observe the ‘Connected to MySQL Server!’ message in the terminal. If you have the latest MySQL server installed, you might end up getting an error saying the following. To tackle this issue, create a new user in your MySQL server with ‘mysql_native_password’ authentication mechanisum. Here is how you can do it quickly.


1 Answers

You should close the connection.

The point at which you do it depends on what your program does with that connection.

If the program is long lived and only needs a single connection but uses that connection continuously then you can just leave that one connection open, but you should be be prepared to re-open the connection if required - e.g. if the server gets restarted.

If the program just opens one connection, does some stuff, and then doesn't use the connection for some time you should close the connection while you're not using it.

If the program is short lived, i.e. it makes a connection, does some stuff, and then exits you can get away without closing the connection because it'll get closed automagically when your program exits.

like image 143
Alnitak Avatar answered Sep 28 '22 14:09

Alnitak