I would like to know if I am to use createConnection instead of using createPool, does it mean that the createConnection pool will be automatically created and closed when done with the query?
Many thanks in advance.
What is the different createConnection vs createPool of MySQL NodeJS and in fact, using createPool the right way to go. Please refer here for the answer.
https://codeforgeek.com/nodejs-mysql-tutorial/
Connection Pooling (createPool) is a mechanism to maintain a cache of database connection so that the connection can be reused after releasing it.
So connections get stored, and do not need to get closed.
While using a connection (createConnection), you only have one connection and it lasts until you close it.
Related
my node.js file is interacting with a MySQL database through the MySQL module. For now I basically just create a pool
var connection = mysql.createPool(dataConnection);
then I make simple queries using connection.query().
Sometimes I get an error saying Error: Connection lost: The server closed the connection. From what I've understood, whenever I call the query method a brand new connection is created from the pool, then it is closed immediately after it is done. Hence I am quite puzzled: how can the connection be closed since the server should explicitly create one for this query? And, most importantly, is there a way I can actually avoid this issue (which doesn't happen too often, fortunately, but still).
Thanks for your help!
Noël.
Since you haven't shared a lot of details and code related to connection pool creation, I'll try to ans as best as I can.
var pool = mysql.createPool(dataConnection);
// check if you have configured any timeouts etc for the pool
I'm not sure about the exact code, but it should look something like this:
connection = pool.GetConnection();
result = connection.query();
// now instead of closing the connection, it should be returned/released back to the pool
connection.release();
How do I create a MySQL connection pool while working with NodeJS and Express?
I am developing a server application, which uses mysql for some data storing. Should I create a connection to the mysql on the server's start and use it for all queries, or create connection on each query? Which is better/faster?
If you ask me, It's better to use only one connection, that way you can use session variables without any issues. Besides, with static variables, it's easy to permanently save the connection somewhere in the script.
So this question is a matter of good idea/bad idea. I am using a MySQL connection many times in a short amount of time. I have created my own method calls to update values, insert, delete, etc.
I am reusing the same connection for each of these methods, but I am opening and closing the connection at each call. The problem being that I need to check to make sure that the connection is not open before I try to open it again.
So, the question is: Is there danger in just leaving the MySQL connection open in between method calls? I'd like to just leave it open and possibly improve speed while I am at it.
Thanks for any advice!
Generally speaking, no you shouldn't be closing it if in the same class / library / code scope you're just going to open it again.
This is dependant on the tolling / connection library you're using. if you're using connection pooling some library's will not actually close the connection (immediately) but return it to the pool.
The only comment I'll make about reusing a connection is that if you're using variables that are connection specific those variables will still be valid for the same connection and may cause problems later if another query uses one of them and it has a value from a past query that is no longer reliant - however this would also raise questions about the suitability of the variable in the first place.
Opening a connection is something is within MySQL is fairly light (compared with other databases) however you shouldn't be creating extra work if you can avoid it.
Question 1:
I am using MySQL Connector /J to connect to MySQL. I am creating connection for every request. I need to use connection pool. Whether i need to choose c3p0 or i could use MysqlConnectionPool class provided by the connector library.
Question 2:
I may need to load balace / failover between two MySQL database servers. I could use jdbc:mysql://host,host2/dbname to do the failover automatically. I want to use connection pool and failover in combination. How should i acheive it.
I'd recommend using C3PO or something else. It'll integrate into a Java EE app server better, and it's database agnostic.
Your second question is a good deal more complicated. Load balancing is usually done with an appliance of some kind, like an F5 or ACE, that stands between the client and the load balanced instances. Is that how you're doing it? How do you plan to keep the data in synch if you load balance between the two? If the connections aren't "sticky", you'll expect to find INSERTed data in both instances.
Maybe this reference can help you get started:
http://www.howtoforge.com/loadbalanced_mysql_cluster_debian
I'm new to node.js and have heard of connection pooling and it makes sense to me as the connection is an expensive operation.
I am looking at which node module to use for mysql and I like the look of Sequelize as it is an ORM.
I'm not sure if I need to worry about connection pooling with Sequelize. Do I just instantiate it and reuse it for all clients?
var sequelize = new Sequelize('database', 'username'[, 'password'])
Also, do I need to worry about the number of parallel queries being executed?
For example, if I am looping through a table and executing a query per row. What happens if there are 1000 or more rows?
Do those queries get executed all at once?
If so, is there a limit to the amount you can throw at it?
at the very moment Sequelize is not using pooling. This caused many problems in the past, so it was removed. I will add pooling again in later versions of Sequelize. At the moment, every request opens a new connection. There is also a known bug, which leads to unclosed connections. Due to heavy enhancements in the next version, this will hopefully be fixed. I will also change from the currently used node-mysql to another mysql connector, which is much faster. Once this is done, I try to add pooling again.