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?
Related
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.
I'm creating a web application that uses SQLAlchemy to connect to a MySQL database. This may be a newbie question, but when is the proper time to call Engine.connect()? Should I be:
Calling Engine.connect() every time a web request is made, and closing the connection once the request is processed? OR
Calling Engine.connect() several times in the very beginning of my application to create several connections. Then every time I process a request, I use one of the connections, and never close them?
I'm also not exactly sure I understand the concept of a Connection Pool in general. Does the connection pool maintain a series of open connections to the database, and then hand a connection off whenever Engine.connect() is called? Or, does the pool establish a new connection every time Engine.connect() is called? What happens when Connection.close() is called?
I have written a web server using Delphi and the Indy TIdHttpServer component. I am managing a pool of TAdoConnection connections to a MySql database. When a request comes in I query my pool for available database connections. If one is not available then a new TAdoConnection is created and added to the pool.
Problems occur when a connection becomes "stale" (i.e. it has not been used in quite some time). I think in this instance the query results in the "MySql has gone away" error.
Does anyone have a method for getting around this? Or would I have manage it myself by one of the following:
Writing a thread that will periodically "refresh" all connections.
Keeping track of the last active query, and if too old pass up using the connection and instead free it.
Two suggestions:
store a 'last used' time stamp with every pooled connection, and if a connection is requested check if the connection is too old - in this case, create a new one
add a validateObject() method which issues a no-op SQL query to detect if the connection is still healthy
a background thread which cleans up the pool in regular intervals: removing idle connections allows to reduce the pool size back to a minimum after peak usage
For some suggestions, see this article about the Apache Commons Pool Framework: http://www.javaworld.com/article/2071834/build-ci-sdlc/pool-resources-using-apache-s-commons-pool-framework.html
When you run a basic ColdFusion query, when does ColdFusion actually log out of the database? When does the query actually close? My understanding is that when you have multiple users being authenticated at the same time, it maintains it's connection and uses a new thread for a new user. But I am struggling to find any documentation as to when it actually closes. Is it when the page is finished rendering or is it directly after the query execution?
Any help on this matter would be greatly appreciated. We are running ColdFusion 9 Standard with SQL Server 2008.
My understanding it that, by default, ColdFusion won't log out of the database at a particular time. It uses a connection pool, so when you make a query, coldfusion takes a connection from it's pool of connections (creating a connection if none were present), executes the query, then hands the connection back to the pool, ready for more requests. Connections will eventually be closed when they've been inactive for long enough (20 minutes by default, set by the Timeout setting in ColdFusion DataSource admin).
I think the strict answer to your question is: 20 minutes since the last use of that connection, but that's hard to determine
As a standard procedure, MySql connection is lost after stated number of hours (default:8) of inactivity. To reconnect to the mysql server after identifying such connection lost I simply do connection = DriverManager.getConnection(url, user, password);
I am not using connection pool and as this trick has not been mentioned in previous connection lost related posts, that makes me wonder if my code will generate any side-effects later? (I say so because testing this code after above instance, I found the sessionlistner is not called after session.invalidate() call.)
You'll loose temporary tables and session settings if your connection drops. It sounds like a connection pool would be useful in your situation.
Depending on how you handle connection object(s), it can create small client-side memory leak for connection object that was lost. But probably this effect will be so small that you will never see any problems from it.
To minimize this risk, you can do something as simple as SELECT 1 every few minutes from your connection during idle time, such that your connection is still considered active (unless your client dies off completely).