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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With